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

public class GameManager : MonoBehaviour {

    public GameObject tile;
    public GameObject waveG;
    public GameObject waveR;
    public GameObject waveB;
    public GameObject waveY;
    public GameObject endPrefab;
    public GameObject wallPrefab;
    public float spaceBetweenTiles = 1.1f;
    public int multiplier= 3;
    [HideInInspector]
    public int iMax;
    [HideInInspector]
    public int jMax = 7;
    public int waveColorPicker = 4;
    public List<AudioClip> gTileAudio;
    public List<AudioClip> rTileAudio;
    public List<AudioClip> bTileAudio;
    public List<AudioClip> yTileAudio;

    private GameObject waveSpawn;
    private GameObject endSpawn;
    private MusicManager mM;
    private AudioSource introMusic;
    private int counter = 0; 

    private bool cdForG = true;
    private bool cdForR = true;
    private bool cdForB = true;
    private bool cdForY = true;
    private bool nextFire = true;
    public float cdTimeForFire = 10f;
    public float timeBetweenFire = 2f;


    void Awake()
    {
        iMax = 7 * multiplier;
    }

    // Use this for initialization
    void Start() {



        for (int i = 0; i < iMax; i++)
        {
            Color previousColor = new Color();
            for (int j = 0; j < jMax; j++)
            {
                GameObject newTile = Instantiate(tile, new Vector3(i * spaceBetweenTiles, 0.1f, j * spaceBetweenTiles), Quaternion.Euler(new Vector3(-90, 0, 0)));

                previousColor = ColorPick(newTile, previousColor);

                

            }
        }

        generateSpawns();
        Instantiate(endPrefab,endSpawn.transform.position,Quaternion.Euler(-90f,0,90f));

        Vector3 position = new Vector3(3.28f,4.45f,3.1f);
        for (int i = 0; i < multiplier; i++)
        {
            
            Instantiate(wallPrefab, position, Quaternion.Euler(0f,0f,90f));
            position.x += 11.54f - 3.28f;

            
        }

        mM = GetComponent<MusicManager>();
        introMusic = GetComponents<AudioSource>()[4];
        
    }

    // Update is called once per frame
    void Update() {

        if (Input.GetButtonDown("ContrA"))
        {
            if (cdForG && nextFire)
            {
                introMusic.Stop();
                waveColorPicker = 1;
                GameObject newWave = Instantiate(waveG, waveSpawn.transform.position, Quaternion.identity);
                Destroy(newWave, (spaceBetweenTiles + 0.2f) * iMax / newWave.GetComponent<WaveMover>().speed);
                cdForG = false;
                nextFire = false;
                Invoke("coolDownG",cdTimeForFire);
                Invoke("nextFireCd",timeBetweenFire);

                mM.FireWave(newWave);
            }
            
        }
        else if (Input.GetButtonDown("ContrB"))
        {
            if (cdForR && nextFire)
            {
                introMusic.Stop();
                waveColorPicker = 2;
                GameObject newWave = Instantiate(waveR, waveSpawn.transform.position, Quaternion.identity);
                Destroy(newWave, (spaceBetweenTiles + 0.2f) * iMax / newWave.GetComponent<WaveMover>().speed);
                cdForR = false;
                nextFire = false;
                Invoke("coolDownR", cdTimeForFire);
                Invoke("nextFireCd", timeBetweenFire);

                mM.FireWave(newWave);
            }
            
        }
        else if (Input.GetButtonDown("ContrY"))
        {
            if (cdForY && nextFire)
            {
                introMusic.Stop();
                waveColorPicker = 3;
                GameObject newWave = Instantiate(waveY, waveSpawn.transform.position, Quaternion.identity);
                Destroy(newWave, (spaceBetweenTiles + 0.2f) * iMax / newWave.GetComponent<WaveMover>().speed);
                cdForY = false;
                nextFire = false;
                Invoke("coolDownY", cdTimeForFire);
                Invoke("nextFireCd", timeBetweenFire);

                mM.FireWave(newWave);
            }
            
        }
        else if (Input.GetButtonDown("ContrX"))
        {
            if (cdForB && nextFire)
            {
                introMusic.Stop();
                waveColorPicker = 4;
                GameObject newWave = Instantiate(waveB, waveSpawn.transform.position, Quaternion.identity);
                Destroy(newWave, (spaceBetweenTiles + 0.2f) * iMax / newWave.GetComponent<WaveMover>().speed);
                cdForB = false;
                nextFire = false;
                Invoke("coolDownB", cdTimeForFire);
                Invoke("nextFireCd", timeBetweenFire);

                mM.FireWave(newWave);
            }
            
        }

    }

    Color ColorPick(GameObject newTile, Color priviousColor)
    {
        Color pickedColor;
        TilePropertys tilePropertys = newTile.GetComponent<TilePropertys>();
        int picker = Random.Range(1, 5);


        if (picker == 1)
        {
            pickedColor = Color.green;
            tilePropertys.color = pickedColor;
            newTile.GetComponent<AudioSource>().clip = gTileAudio[counter];
            counter++;
        }
        else if (picker == 2)
        {
            pickedColor = Color.red;
            tilePropertys.color = pickedColor;
            newTile.GetComponent<AudioSource>().clip = rTileAudio[counter];
            counter++;
        }
        else if (picker == 3)
        {
            pickedColor = Color.yellow;
            tilePropertys.color = pickedColor;
            newTile.GetComponent<AudioSource>().clip = yTileAudio[counter];
            counter++;
        }
        else
        {
            pickedColor = Color.blue;
            tilePropertys.color = pickedColor;
            newTile.GetComponent<AudioSource>().clip = bTileAudio[counter];
            counter++;
        }

        if (priviousColor == pickedColor)
        {
            counter--;
            return ColorPick(newTile, priviousColor);
        }

        if (counter == 22)
        {
            counter = 0;
        }
        return pickedColor;
    }

    void generateSpawns()
    {
        waveSpawn = Instantiate(new GameObject(), new Vector3(-2f, 1.5f, (jMax - 1) * spaceBetweenTiles / 2), Quaternion.identity);
        endSpawn = Instantiate(new GameObject(), new Vector3((iMax * spaceBetweenTiles)+3.7f, -1.48f, (jMax - 1) * spaceBetweenTiles / 2), Quaternion.identity);
    }

    void coolDownG()
    {
        cdForG = true;
    }

    void coolDownR()
    {
        cdForR = true;
    }

    void coolDownB()
    {
        cdForB = true;
    }

    void coolDownY()
    {
        cdForY = true;
    }

    void nextFireCd()
    {
        nextFire = true;
    }

}

