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

public class GameManager : MonoBehaviour {

    public static GameManager instance;
    public float gameTime;
    public Text playerWinText, countDownText;
    public GameObject button;
    public bool vsIA = false;
    public AnimationCurve animationCurve;
    private Vector3 cameraPosition;
    public bool shaking = false;
    private float shakingTime = 0f;
    private bool countDown = false;
    private float countDownTimer = 4f;

    public GameObject RayoManager;
    public GiroPlato giroPlatoScript;
	public bool threeTrue = true;
	public bool twoTrue = true;
	public bool oneTrue = true;
	public bool goTrue = true;

    public int NumOfPlayers;
    public bool gameFinished = false;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
//            DontDestroyOnLoad(gameObject);
        }
        else {
            Destroy(gameObject);
        }
			
		TheAudioManager.instance.PlayMusicInGame ();

    }

    // Use this for initialization
    void Start () {
        //playerWinText.enabled = false;
        cameraPosition = Camera.main.transform.position;
        CuentaAtras();
        threeTrue = true;
        twoTrue = true;
        oneTrue = true;
        goTrue = true;

        NumOfPlayers =  SceneManager.GetActiveScene().buildIndex;

}

// Update is called once per frame
void Update () {
        gameTime += Time.deltaTime;
        if(shaking == true)
        {
            shakingTime += Time.deltaTime;
            Camera.main.transform.position = cameraPosition + Random.insideUnitSphere.normalized * 0.2f * animationCurve.Evaluate(shakingTime);
        }

        if(countDown == true)
        {
            countDownTimer -= Time.deltaTime;

            if (countDownTimer < 0f)
            {
                countDownText.text = "Go!";
                Invoke("HideCountDown", 1f);
				if (!TheAudioManager.instance.sfx.isPlaying && goTrue) {
					TheAudioManager.instance.PlayCounterGo ();
					goTrue = false;
				}

            }
            else if (countDownTimer < 1)
            {
                countDownText.text = 1.ToString();
				if (!TheAudioManager.instance.sfx.isPlaying && threeTrue) {
					TheAudioManager.instance.PlayCounterNumber ();
					threeTrue = false;
				}
            }
            else if(countDownTimer < 2)
            {
                countDownText.text = 2.ToString();
				if (!TheAudioManager.instance.sfx.isPlaying && twoTrue) {
					TheAudioManager.instance.PlayCounterNumber ();
					twoTrue = false;
				}
            }
            else if (countDownTimer < 3)
            {
                countDownText.text = 3.ToString();
				if (!TheAudioManager.instance.sfx.isPlaying && oneTrue) {
					TheAudioManager.instance.PlayCounterNumber ();
					oneTrue = false;
				}
            }
        }
    }

    public void PlayerWin(int playerWhoDie)
    {
        if (gameFinished == false)
        {
            button.SetActive(true);
            gameFinished = true;
            playerWinText.enabled = true;
            //winnerTimeText.enabled = true;

            if (NumOfPlayers == 2)
            {
                if (playerWhoDie == 1)
                {
                    playerWinText.text = "Player 2 wins!";
                }

                else if (playerWhoDie == 2)
                {
                    playerWinText.text = "Player 1 wins!";
                }

                // winnerTimeText.text = gameTime.ToString();
            }

            else if (NumOfPlayers == 1)
            {
				if (playerWhoDie == 1)
				{
					playerWinText.text = "Player CPU wins!";
				}

				else if (playerWhoDie == 2)
				{
					playerWinText.text = "Player 1 wins!";
				}
            }
            else
            {
                playerWinText.text = "You lose!";
                //winnerTimeText.text = gameTime.ToString();
            }
        }       
        
    }

    public void Restart()
    {
		if (TheAudioManager.instance.sfxLongElectricity.isPlaying) {
			TheAudioManager.instance.sfxLongElectricity.Stop ();
		}
		SceneManager.LoadScene (0);
    }

    public void ShakeCamera()
    {
        shaking = true;
        shakingTime = 0f;
        Invoke("Shaking", 0.4f);
    }

    public void StopShake()
    {
        shaking = false;
    }

    public void CuentaAtras()
    {
        countDown = true;
        countDownText.enabled = true;
        countDownText.text = "";
    }

    public void HideCountDown()
    {
        countDown = false;
        countDownText.enabled = false;
        RayoManager.SetActive(true);
        giroPlatoScript.enabled = true;

}
    
}
