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

public class SplashManager : MonoBehaviour
{
	[SerializeField]
	int FADE_FRAMES;

	bool animating;
	bool fadeDone;

    bool getRekt;

	// Use this for initialization
	void Start ()
	{
		animating = false;
		fadeDone = false;
        getRekt = false;
		StartCoroutine(FadeIn());

	}
	
	// Update is called once per frame
	void FixedUpdate ()
	{
        if (!animating && Input.anyKeyDown)
        {
			animating = true;
			StartCoroutine(FadeAndLoad());
		}


        if(getRekt)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
		/*
		if (!animating && fadeDone && Input.anyKey)
		{
			SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
		}
		//*/
    }

	IEnumerator FadeAndLoad()
	{
		Debug.Log("Start FadeAndLoad");
		//animating = true;
		for (int i = 0; i < FADE_FRAMES; ++i)
		{
			Debug.Log("I = " + i + "Numbers = " + (1.0f - ((float)i / FADE_FRAMES)));
			gameObject.GetComponent<Image>().color = new Color(1.0f - ((float)i/FADE_FRAMES), 1.0f - ((float)i /FADE_FRAMES), 1.0f - ((float)i / FADE_FRAMES), 1.0f);
			yield return null;
		}
		yield return new WaitForFixedUpdate();
		//animating = false;
		//fadeDone = true;

		yield return new WaitForSeconds(0.5f);
		StartCoroutine(LoadAsync());
	}

	IEnumerator FadeIn()
	{
		//animating = true;
		for (int i = 0; i < FADE_FRAMES; ++i)
		{
			gameObject.GetComponent<Image>().color = new Color((float)i / FADE_FRAMES, (float)i / FADE_FRAMES, (float)i / FADE_FRAMES, 1.0f);
			yield return null;
		}
		yield return new WaitForFixedUpdate();
		//animating = false;
	}

	IEnumerator LoadAsync()
	{
        getRekt = false;
		yield return new WaitForSeconds(0.2f);
        getRekt = true;
		
	}
}
