using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour
{
    public Transform HandTransform;
    public float Cooldown = 0;
    public AudioClip Sound;
    public float SoundVolume;
    public float SoundPitchChange;
    public Entity Owner;

    [SerializeField]
    public Vector3 dP;
    [SerializeField]
    public Quaternion dR;

    private AudioSource audioSource;

    private SpriteRenderer spriteRenderer; 
    public SpriteRenderer SpriteRenderer
    {
        get
        {
            return spriteRenderer; 
        }
    }

    public virtual void Start()
    {
        this.audioSource = this.GetComponent<AudioSource>();

        this.spriteRenderer = this.GetComponent<SpriteRenderer>(); 
        dR = this.transform.rotation * Quaternion.Inverse(this.HandTransform.rotation); 
    }

    public virtual void UpdatePosition(Transform hand)
    {
        float scale = Player.Instance.Controller.collisions.faceDir; 
        Vector3 handPos = hand.position + (scale > 0 ? hand.right : hand.right) * 0.05f;
        Quaternion newDr = Quaternion.AngleAxis(180.0f, Vector3.right) * dR;
        
        this.transform.rotation = hand.rotation * (scale > 0 ? dR : newDr); 
        dP = this.HandTransform.position - this.transform.position;
        this.transform.position = handPos - dP;
    }

    public virtual void Use(Vector2 aimPos)
    {
        if (audioSource != null)
        {
            if(SoundPitchChange != 0.0f)
            {
                audioSource.pitch = 1.0f + Random.Range(-SoundPitchChange, +SoundPitchChange);
            }
            
            audioSource.PlayOneShot(this.Sound, this.SoundVolume);
        }
    }

}
