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

public class Wave : MonoBehaviour
{
    public Action notification;
    public GameObject food;
    public Transform table;
    public int numberFood;
    public float distanceSpawnX;
    public float distanceSpawnY;
    public float goldProb;
    public float badProb;

    public Food.FoodType FoodProp { get; set; }
    private float waveDuration;
    private bool finish;
    private List<GameObject> foods;
    private int foodCounter;

    public void activate()
    {
        foodCounter = 0;
        finish = false;
        foods = new List<GameObject>();
        StartCoroutine(timeStart());
        handleWave();
    }

    IEnumerator timeStart()
    {
        yield return new WaitForSeconds(waveDuration);
        notification();
        finish = true;
        Array.ForEach<PointOfEffectController>(FindObjectsOfType<PointOfEffectController>(),
            player => player.ReleaseAllFood());
        for (int i = 0; i < this.foods.Count; i++)
        {
            GameObject.Destroy(foods[i]);
        }
    }

    public void setDuration(float wd)
    {
        this.waveDuration = wd;
    }

    void handleWave()
    {
        if (!finish)
        {
            handlePlate();
        }
    }

    void handlePlate()
    {
        GameObject f = null;
        this.foodCounter = this.numberFood;
        bool thatsgold = false;
        bool thatsbad = false;
        if (UnityEngine.Random.value < this.goldProb)
            thatsgold = true;
        if (UnityEngine.Random.value < this.badProb)
            thatsbad = true;
        for (int i=0; i<this.numberFood; i++)
        {
            f = Instantiate(food);
            f.GetComponent<Food>().FoodProperty = this.FoodProp;
            if (thatsgold)
            {
                f.GetComponent<Food>().FoodHealthProperty = Food.FoodHealth.gold;
                thatsgold = false;
            }
            else if (thatsbad)
            {
                f.GetComponent<Food>().FoodHealthProperty = Food.FoodHealth.bad;
                thatsbad = false;
            }
            else
            {
                f.GetComponent<Food>().FoodHealthProperty = Food.FoodHealth.standard;
            }
            f.GetComponent<Food>().SetType();
            float posx = UnityEngine.Random.Range(-distanceSpawnX, distanceSpawnX);
            float posy = UnityEngine.Random.Range(-distanceSpawnY, distanceSpawnY);
            f.transform.position = new Vector3(table.position.x + posx, table.position.y + posy);
            f.GetComponent<Food>().id = i;
            f.GetComponent<Food>().setWave(gameObject);
            foods.Add(f);
        }

        
    }


    public void subCounter(GameObject oob)
    {
        this.foodCounter--;
        int index = foods.FindIndex(
                    x => x.GetComponent<Food>().id == oob.GetComponent<Food>().id);
        
        foods.RemoveAt(index);
        if (foodCounter == 1 && foods[0].GetComponent<Food>().FoodHealthProperty == Food.FoodHealth.bad)
        {
            this.foodCounter--;
            Destroy(foods[0]);
            foods.RemoveAt(0);
        }
        
        Destroy(oob);

        if (foodCounter == 0 && !finish)
        {
            this.handlePlate();
        }
    }

    


}
