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

public class Player : MonoBehaviour
{
    public Animator PlayerAnimator;
    public Transform Weapon;
    public Animator WeaponAnimator;
    public Transform Arrow;
    public AudioClip[] soundSamples1;
    public AudioClip[] soundSamples2;
    private AudioSource audioSource;

    private float speed = 8;

    //for hitting
    public GameObject targetObject;
    public Vector2 hitDir;
    public GameObject HitIcon;

    //freeze action to avoid conflicted sound effects
    public bool isfrozen;
    
    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        HitIcon.SetActive(false);
        isfrozen = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (!isfrozen)
        {


            Vector3 ms = Input.mousePosition;
            ms = Camera.main.ScreenToWorldPoint(ms);//获取鼠标相对位置
                                                    //对象的位置
            Vector3 pos = this.transform.position;
            float angle; //计算鼠标位置与对象位置之间的角度
            Vector2 targetDir = ms - pos;
            hitDir = targetDir.normalized;
            angle = Vector2.Angle(targetDir, Vector3.up);
            if (ms.x > pos.x)
            {
                angle = -angle;
            }
            Arrow.transform.eulerAngles = new Vector3(0, 0, angle);

            if (Input.GetMouseButtonDown(0))
            {
                WeaponAnimator.Play("hit");
                if (targetObject != null)
                {
                    StartCoroutine(HitCoroutine());
                    PlayRandomSound(soundSamples2);
                    var foodObj = targetObject.GetComponent<FoodObject>();
                    if (foodObj != null)
                    {
                        foodObj.isHitted = true;
                        foodObj.ResetHitSpeed();
                        foodObj.movingDir = hitDir;
                    }
                    GameController.Instance.Shake();
                }
                else
                {
                    PlayRandomSound(soundSamples1);
                }
            }


            var h = Input.GetAxis("Horizontal");
            var v = Input.GetAxis("Vertical");
            this.transform.Translate(new Vector3(h, v, 0) * speed * Time.deltaTime);

            if (h == 0 && v == 0)
            {
                PlayerAnimator.Play("idle");
            }
            else
            {
                PlayerAnimator.Play("run");
            }

            if (h < 0)
            {
                PlayerAnimator.transform.localScale = Vector3.one;
            }
            else if (h > 0)
            {
                PlayerAnimator.transform.localScale = new Vector3(-1, 1, 1);
            }
        }
    }

    //private void OnCollisionExit2D(Collision2D collision)
    //{
    //    if (collision.gameObject.GetComponent<FoodObject>() != null)
    //    {
    //        Debug.Log("player collides with food");
    //        //set target object
    //        targetObject = null;
    //    }
    //}

    //private void OnCollisionEnter2D(Collision2D collision)
    //{
    //    if (collision.gameObject.GetComponent<FoodObject>() != null)
    //    {
    //        Debug.Log("player collides with food");
    //        //set target object
    //        targetObject = collision.gameObject;
    //    }
    //}

    public void TriggerExit2D(Collider2D collision)
    {
        if (collision.GetComponent<FoodObject>() != null)
        {
            Debug.Log("player no longer colliding with food");
            //set target object back to null
            targetObject = null;
        }
    }

    public void TriggerEnter2D(Collider2D collision)
    {
        if (collision.GetComponent<FoodObject>() != null)
        {
            Debug.Log("player collides with food");
            //set target object
            targetObject = collision.gameObject;
        }

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.GetComponent<FoodObject>() != null)
        {
            Debug.Log("player collides with food");
            //set target object
            targetObject = collision.gameObject;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.GetComponent<FoodObject>() != null)
        {
            Debug.Log("player collides with food");
            //set target object
            targetObject = null;
        }
    }

    void PlayRandomSound(AudioClip[] audioClips)//
    {
        if (audioClips.Length > 0)
        {
            int randomIndex = Random.Range(0, audioClips.Length);
            audioSource.PlayOneShot(audioClips[randomIndex]);
        }
    }

    IEnumerator HitCoroutine()
    {
        HitIcon.SetActive(true);
        
        yield return new WaitForSeconds(0.5f);
        HitIcon.SetActive(false);
    }
}
