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

public class GameStates : MonoBehaviour {

	public static GameStates instance;

	public CanvasGroup introSplash;
	public CanvasGroup deadSplash;
	public FlightController2D Bat;

	public Text score;

	[System.Serializable]
	public class SaveStates{
		public Transform posTrans;
		public bool reached = false;
	}

	public List<SaveStates> states = new List<SaveStates>();
	

	void Awake()
	{
		instance = this;	

		IntroSplash();
	}

	public void IntroSplash()
	{
		introSplash.alpha = 1;

		//StartCoroutine(FadeCanvas(introSplash, false));
	}

	void Update()
	{
		if(introSplash.alpha == 1)
		if(Input.GetKey(KeyCode.Space))
		{
			StartCoroutine(FadeCanvas(introSplash, false));
		}

		int complete = -1;
		for(int i = 0 ; i < states.Count; i++)
		{
			if(states[i].reached)
			{
				complete ++;
			}		
		}

		score.text = complete + "/5";

	}

	public float fadeIncrement = .01f;

	public IEnumerator FadeCanvas(CanvasGroup cg, bool state)
	{
		switch(state)
		{
		case true:
			cg.interactable = true;
			cg.blocksRaycasts = true;
			cg.alpha = 1;
			// while(cg.alpha < 1)
			// {
			// 	cg.alpha += fadeIncrement;
				
			 	yield return null;
			// }
		break;
		case false:
			cg.interactable = false;
			cg.blocksRaycasts = false;
			cg.alpha = 0;
			// while(cg.alpha > 0)
			// {
			// 	cg.alpha -= fadeIncrement;
			 	yield return null;
			// }
		break;	
		}

	}

	 public void BatDeath()
	 {
		 print("Bat Death");
		StartCoroutine(FadeCanvas(deadSplash, true));	 	
	 }

	 public void RestartGame()
	 {
		 StartCoroutine(FadeCanvas(deadSplash, false));
		 foreach(SaveStates s in states)
		 {
			 if(s.reached)
			 {
				Bat.gameObject.transform.position = s.posTrans.position;
			 }
		 }

		 Bat.GetComponentInChildren<BatCollisionHandler>().Restart();
	 }

	 public void Reset()
	 {
		 SceneManager.LoadScene(0);
	 }
}
