using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public float roundDuration;
    public float numberWaves;
    public GameObject wave;
    public AudioManager audiomanager;

    private List<GameObject> waves;
    private float timeStart;
    private int waveNumber;
    public Text Count;
    public int PlayersCount = 4;
    public bool debugMode;

    void Start()
    {
        if (debugMode)
            Init();
    }

    public void Init ()
    {
        waveNumber = 0;
        waves = new List<GameObject>();
        for (int i = 0; i < numberWaves; i++)
        {
            waves.Add(Instantiate(wave));
            waves[i].GetComponent<Wave>().setDuration(this.roundDuration);
            if (i==0)
                waves[i].GetComponent<Wave>().FoodProp = Food.FoodType.rigatone;
            else if (i == 1)
                waves[i].GetComponent<Wave>().FoodProp = Food.FoodType.sushi;
            else if (i == 2)
                waves[i].GetComponent<Wave>().FoodProp = Food.FoodType.gambero;
            else if (i == 3)
                waves[i].GetComponent<Wave>().FoodProp = Food.FoodType.scarafaggio;
        }
        for (int i=0; i<waves.Count; i++)
        {
            waves[i].GetComponent<Wave>().notification += goToNext;
        }
        audiomanager.updateMusic(waveNumber);
        
        waves[waveNumber].GetComponent<Wave>().activate();
        

	}
    
	void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
            PlayersCount = 1;
        else
        if (Input.GetKeyDown(KeyCode.Alpha2))
            PlayersCount = 2;
        else
        if (Input.GetKeyDown(KeyCode.Alpha3))
            PlayersCount = 3;
        else
        if (Input.GetKeyDown(KeyCode.Alpha4))
            PlayersCount = 4;


    }

    IEnumerator CountDown()
    {
        Count.text = "" + 3;
        yield return new WaitForSeconds(1.0f);
        Count.text = ""+2;
        yield return new WaitForSeconds(1.0f);
        Count.text = "" + 1;
        yield return new WaitForSeconds(1.0f);
        Count.text = ""+0;
        GameObject.FindGameObjectWithTag("Table").GetComponent<Table>().UpdateSprite(waveNumber);
        foreach (GameObject g in GameObject.FindGameObjectsWithTag("plate"))
            g.GetComponent<Plate>().UpdateSprite(waveNumber);
        waveNumber++;

        waves[waveNumber].GetComponent<Wave>().activate();
        Count.text = "";
    }

    void goToNext()
    {
        if (waveNumber<3)
        {

            //StartCoroutine(CountDown());
            GameObject.FindGameObjectWithTag("Table").GetComponent<Table>().UpdateSprite(waveNumber);
            foreach (GameObject g in GameObject.FindGameObjectsWithTag("plate"))
                g.GetComponent<Plate>().UpdateSprite(waveNumber);
            waveNumber++;
            audiomanager.updateMusic(waveNumber);
            waves[waveNumber].GetComponent<Wave>().activate();
        }
        else
        {
            Invoke("restart", 5.0f);
            
        }
        
    }

    void restart()
    {
        SceneManager.LoadScene("StartScene");
    }

}
