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

public class King : MonoBehaviour
{
    [Serializable]
    public struct ToothAtTheSameLocation
    {
        public List<GameObject> Tooth;
    }
    public bool need_teeth;
    public int missing_teeth;
    public Animator KingAnimator;
    public GameObject AngryIcons;
    // Start is called before the first frame update
    public List<ToothAtTheSameLocation> ToothList;
    public GameObject SonicWaveGo;

    private List<int> badToothIndexList = new List<int>();
    private Coroutine angryCoroutine;
    public AudioClip[] soundSamples1;//chew
    public AudioClip[] soundSamples2;//angry
    public AudioClip[] soundSamples3;//gold teeth
    public AudioClip[] soundSamples4;//teeth
    private AudioSource audioSource;
    private Coroutine setToothStateCoroutine;
    private void Awake()
    {
        SonicWaveGo.SetActive(false);
    }
    void Start()
    {
        need_teeth = true;
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        KingAnimator.SetBool("IsTeethMissing", badToothIndexList.Count != 0);
    }

    //private void OnCollisionEnter2D(Collision2D collision)
    //{
    //    var foodObj = collision.collider.gameObject.GetComponent<FoodObject>();
    //    if (foodObj != null)
    //    {
    //        Debug.Log("king collided with food");
    //        //food disappears
    //        Destroy(collision.collider.gameObject);
    //        //score or king reacts

            
    //        GameController.Instance.EatFood(foodObj.foodType);
    //    }
    //}

    public void SetBadToothNumber(int number, bool isImmediate = false)
    {
        badToothIndexList.Clear();
        for(int i = 0; i < ToothList.Count; i++)
        {
            badToothIndexList.Add(i);
        }

        for (int i = 0; i < ToothList.Count - number; i++)
        {
            var index = UnityEngine.Random.Range(0, badToothIndexList.Count);
            badToothIndexList.RemoveAt(index);
        }

        if (setToothStateCoroutine != null)
        {
            StopCoroutine(setToothStateCoroutine);
        }
        setToothStateCoroutine = StartCoroutine(SetToothState(isImmediate));
    }

    IEnumerator SetToothState(bool isImmediate = false)
    {
        if (!isImmediate)
        {
            KingAnimator.Play("ouch");
            yield return new WaitForSeconds(1f);
        }
        for (int i = 0; i < ToothList.Count; i++)
        {
            var tooth = ToothList[i].Tooth;
            if (badToothIndexList.Contains(i))
            {
                foreach (var item in tooth)
                {
                    var teeth = item.GetComponent<Teeth>();
                    if (teeth != null)
                    {
                        teeth.ChangeState(TeethStateType.Bad, isImmediate);
                    }
                }
            }
            else
            {
                foreach (var item in tooth)
                {
                    var teeth = item.GetComponent<Teeth>();
                    if (teeth != null)
                    {
                        teeth.ChangeState(TeethStateType.Good);
                    }
                }
            }
        }
    }

    public void AddATeeth()
    {
        if (badToothIndexList.Count == 0) return;
        int index = badToothIndexList[badToothIndexList.Count - 1];
        badToothIndexList.RemoveAt(badToothIndexList.Count - 1);
        var tooth = ToothList[index].Tooth;
        foreach (var item in tooth)
        {
            var teeth = item.GetComponent<Teeth>();
            if (teeth != null)
            {
                teeth.ChangeState(TeethStateType.Good);
                teeth.SetColor(TeethColorType.White);
            }
        }
    }

    public void AddAGoldenTeeth()
    {
        if (badToothIndexList.Count == 0) return;
        int index = badToothIndexList[badToothIndexList.Count - 1];
        badToothIndexList.RemoveAt(badToothIndexList.Count - 1);
        var tooth = ToothList[index].Tooth;
        foreach (var item in tooth)
        {
            var teeth = item.GetComponent<Teeth>();
            if (teeth != null)
            {
                teeth.ChangeState(TeethStateType.Good);
                teeth.SetColor(TeethColorType.Golden);
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        var foodObj = collision.GetComponent<FoodObject>();
        if (foodObj != null)
        {
            Debug.Log("king collided with food");
            //food disappears
            Destroy(collision.gameObject);
            //score or king reacts


            GameController.Instance.EatFood(foodObj.foodType);

            if (angryCoroutine != null)
            {
                StopCoroutine(angryCoroutine);
                AngryIcons.SetActive(false);
            }

            if (GameController.Instance.CheckTargetFood(foodObj.foodType))
            {
                if(foodObj.foodType == FoodType.Suger)
                {
                    KingAnimator.Play("chew");
                    PlayRandomSound(soundSamples1);
                }
                else
                {
                    if (foodObj.foodType == FoodType.Teeth)
                    {
                        KingAnimator.Play("teeth");
                        PlayRandomSound(soundSamples4);
                        AddATeeth();
                    }
                    else if (foodObj.foodType == FoodType.GoldenTeeth)
                    {
                        KingAnimator.Play("lol");
                        PlayRandomSound(soundSamples3);
                        AddAGoldenTeeth();

                        var gos = GameObject.FindGameObjectsWithTag("Food");
                        for (int i = 0; i < gos.Length; i++)
                        {
                            var food = gos[i].GetComponent<FoodObject>();
                            if (food != null && food != foodObj)
                            {
                                food.ReceiveSonicAttack();
                            }
                        }
                        SonicWaveGo.SetActive(true);
                    }
                }
            }
            else
            {
                StartCoroutine(AngryCoroutine());
            }
        }
    }
    void PlayRandomSound(AudioClip[] audioClips)//
    {
        if (audioClips.Length > 0)
        {
            int randomIndex = UnityEngine.Random.Range(0, audioClips.Length);
            audioSource.PlayOneShot(audioClips[randomIndex]);
        }
    }

    IEnumerator AngryCoroutine()
    {
        AngryIcons.SetActive(true);
        PlayRandomSound(soundSamples2);
        yield return new WaitForSeconds(1);
        AngryIcons.SetActive(false);
    }
}
