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

public class MusicManager : MonoBehaviour {

    //orged by G,R,B,Y
    public List<AudioClip> ten0;
    //public List<AudioClip> ten1;
    //public List<AudioClip> ten2;

    private AudioSource audioG;
    private AudioSource audioR;
    private AudioSource audioY;
    private AudioSource audioB;
    private float time = 0;
    private int numTon = 0;
    private bool nextTonG = false;
    private bool nextTonR = false;
    private bool nextTonY = false;
    private bool nextTonB = false;
    private bool pickRandom = false;

    // Use this for initialization
    void Start () {
        AudioSource[] audios = GetComponents<AudioSource>();
        audioG = audios[0];
        audioR = audios[1];
        audioY = audios[2];
        audioB = audios[3];
        Debug.Log(audioB);
    }
	
	// Update is called once per frame
	void Update () {

        if (!audioG.isPlaying && nextTonG)
        {
           
            if (pickRandom)
            {
                audioG.clip = ten0[Random.Range(0, 4)];
            }
            else
            {
                audioG.clip = ten0[numTon/*Random.Range(8, 12)*/];
            }

            if (time>= 3.6f)
            {
                audioG.time = 0;
            }
            else
            {
                audioG.time = time;
            }
            audioG.Play();
        }
        
        if (!audioR.isPlaying && nextTonR)
        {
            if (pickRandom)
            {
                audioR.clip = ten0[Random.Range(4, 8)];
            }
            else
            {
                audioR.clip = ten0[numTon + 4/*Random.Range(8, 12)*/];
            }

            if (time >= 3.6f)
            {
                audioR.time = 0;
            }
            else
            {
                audioR.time = time;
            }
            audioR.Play();
        }
        
        if (!audioY.isPlaying && nextTonY)
        {
            if (pickRandom)
            {
                audioY.clip = ten0[Random.Range(8, 12)];
            }
            else
            {
                audioY.clip = ten0[numTon + 8/*Random.Range(8, 12)*/];
            }
            
            if (time >= 3.6f)
            {
                audioY.time = 0;
            }
            else
            {
                audioY.time = time;
            }
            audioY.Play();
        }
        
        if (!audioB.isPlaying && nextTonB)
        {
        
            if (pickRandom)
            {
                audioB.clip = ten0[Random.Range(12, 16)];
            }
            else
            {
                audioB.clip = ten0[numTon + 12/*Random.Range(8, 12)*/];
            }
            if (time >= 3.6f)
            {
                audioB.time = 0;
            }
            else
            {
                audioB.time = time;
            }
            audioB.Play();
        }

        if (GameObject.FindGameObjectWithTag("Finish").GetComponent<WinningCondition>().win)
        {
            audioB.Stop();
            audioG.Stop();
            audioR.Stop();
            audioY.Stop();
        }
        
        refreshTime();
        
    }

    public void FireWave(GameObject wave)
    {
        int pickedColor = wave.GetComponent<WavePropertys>().colorPick;
        pickRandom = false;
        CancelInvoke();
        Invoke("refreshRandomPick",3.5f);

        if (pickedColor == 1 && !audioG.isPlaying)//green
        {
            audioR.Stop();
            audioY.Stop();
            audioB.Stop();
            
            nextTonR = false;
            
            nextTonY = false;
            
            nextTonB = false;
            nextTonG = true;

        }
        else if (pickedColor == 2 && !audioR.isPlaying)//red
        {
            audioG.Stop();
            audioY.Stop();
            audioB.Stop();
            nextTonG = false;
            
            
            nextTonY = false;
            
            nextTonB = false;
            nextTonR = true;

        }
        else if (pickedColor == 3 && !audioY.isPlaying)//yellow
        {
            audioG.Stop();
            audioR.Stop();
            audioB.Stop();
            nextTonG = false;
            
            nextTonR = false;
            
            
            nextTonB = false;
            nextTonY = true;

        }
        else if (pickedColor == 4 && !audioB.isPlaying)//blue
        {
            audioG.Stop();
            audioR.Stop();
            audioY.Stop();
            nextTonG = false;
            
            nextTonR = false;
            
            nextTonY = false;
            nextTonB = true;

        }
    }

    void refreshTime()
    {
        if (audioG.isPlaying)
        {
            time = audioG.time;
            char num = audioG.clip.name[audioG.clip.name.Length - 1];
            int.TryParse(num.ToString(),out numTon);
       }
        else if (audioR.isPlaying)
        {
            time = audioR.time;
            char num = audioG.clip.name[audioG.clip.name.Length - 1];
            int.TryParse(num.ToString(), out numTon);
        }
        else if (audioY.isPlaying)
        {
            time = audioY.time;
            char num = audioG.clip.name[audioG.clip.name.Length - 1];
            int.TryParse(num.ToString(), out numTon);
        }
        else if (audioB.isPlaying)
        {
            time = audioB.time;
            char num = audioG.clip.name[audioG.clip.name.Length - 1];
            int.TryParse(num.ToString(), out numTon);
        }
    }

    void refreshRandomPick()
    {
        pickRandom = true;
    }

    public AudioSource[] getAudios()
    {
        return new AudioSource[] {audioG , audioB , audioR , audioY };
    }

}
