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

public class SceneFader : MonoBehaviour
{
    public float fadeSpeed = 1.5f;

    public string state = "Done";

    public GUIStyle textStyle;
    public GUIStyle whomStyle;

    private States states;

    public Texture2D white;

    public Font font;
    
    public float letterTime;
    
    private Text title;
    
	private UnityEngine.UI.Image fader;
	
	private bool fadeCheck;

    void Start ()
    {   
    	fadeCheck = true;
        states = GameObject.Find("Player").GetComponent<States>();
        GUI.depth = -100;
		fader = GetComponent<Image>();
		title = GameObject.Find ("Title").GetComponent<Text>();
        fader.color = Color.black;
		StartCoroutine ("FadeToClear");
		StartCoroutine ("Intro2");
    }
    
    public IEnumerator Intro ()
    {
    	yield return new WaitForSeconds(5f);
    	title.text += "G";
    	yield return new WaitForSeconds(letterTime);
		title.text += "o";
		yield return new WaitForSeconds(letterTime);
		title.text += " t";
		yield return new WaitForSeconds(letterTime);
		title.text += "o";
		yield return new WaitForSeconds(letterTime);
		title.text += " s";
		yield return new WaitForSeconds(letterTime);
		title.text += "l";
		yield return new WaitForSeconds(letterTime);
		title.text += "e";
		yield return new WaitForSeconds(letterTime);
		title.text += "e";
		yield return new WaitForSeconds(letterTime);
		title.text += "p";
		yield return new WaitForSeconds(letterTime);
		title.text += ".";
		yield return new WaitForSeconds(letterTime * 2);
		title.text = "";
    	yield return null;
    }
    
	public IEnumerator Intro2 ()
	{
		yield return new WaitForSeconds(1f);
		title.text += "G";
		yield return new WaitForSeconds(letterTime);
		title.text += "o";
		yield return new WaitForSeconds(letterTime);
		title.text += " t";
		yield return new WaitForSeconds(letterTime);
		title.text += "o";
		yield return new WaitForSeconds(letterTime);
		title.text += " s";
		yield return new WaitForSeconds(letterTime);
		title.text += "l";
		yield return new WaitForSeconds(letterTime);
		title.text += "e";
		yield return new WaitForSeconds(letterTime);
		title.text += "e";
		yield return new WaitForSeconds(letterTime);
		title.text += "p";
		yield return new WaitForSeconds(letterTime);
		title.text += ".";
		yield return new WaitForSeconds(letterTime * 2);
		title.text = "";
		yield return null;
	}
    
    public IEnumerator FadeToClear ()
    {
    	while (fader.color.a > 0.1f && fadeCheck == true){
    		fader.color = Color.Lerp(fader.color, Color.clear, fadeSpeed * Time.deltaTime);
    		yield return null;
    	}
    	fader.color = Color.clear;
		yield return null;
	}
	
	public IEnumerator FadeToBlack ()
	{
		fadeCheck = false;
		fadeCheck = true;
		while (fader.color.a < 0.9f && fadeCheck == true){
    		fader.color = Color.Lerp(fader.color, Color.black, fadeSpeed * Time.deltaTime);
    		yield return null;
    	}
    	fader.color = Color.black;
		yield return null;
	}
	
	public IEnumerator FadeToWhite ()
    {
        fader.color = Color.clear;
        //fader.texture = white;
        while (fader.color.a < 0.97f){
            fader.color = Color.Lerp(fader.color, Color.white, 0.03f * Time.deltaTime);
            yield return null;
        }
        fader.color = Color.white;
        yield return null;
    }

    void OnGUI ()
    {
        GUI.skin.font = font;
        switch (state)
        {
            case "Start":
                if (GUI.Button(new Rect(50, 50, 150, 50), "Enter", textStyle))
                {
                    //guiTexture.color = Color.black;
                    //StartCoroutine(FadeToClear ());
                    state = "Done";
                    GameObject.Find("StartCamera").SetActive(false);
                    states.cursorFlag = true;
                    states.controllerFlag = true;
                    states.mouselookFlag = true;
                }
                if (GUI.Button(new Rect(50, 125, 150, 50), "Exit", textStyle))
                {
                    Application.Quit ();
                }
                if (GUI.Button(new Rect(50, 200, 150, 50), "Whom", textStyle))
                {
                    state = "Whom";
                }
                break;
            case "Whom":
                DrawOutline(new Rect(Screen.width / 2f - 100, Screen.height / 2f - 80, 200, 20), "Tech", whomStyle, Color.black, Color.white);
                DrawOutline(new Rect(Screen.width / 2f - 100, Screen.height / 2f - 60, 200, 20), "@An0nx", whomStyle, Color.black, Color.white);
                DrawOutline(new Rect(Screen.width / 2f - 100, Screen.height / 2f - 20, 200, 20), "Art", whomStyle, Color.black, Color.white);
                DrawOutline(new Rect(Screen.width / 2f - 100, Screen.height / 2f, 200, 20), "@Nicholobe", whomStyle, Color.black, Color.white);
                DrawOutline(new Rect(Screen.width / 2f - 100, Screen.height / 2f + 40, 200, 20), "Poetry", whomStyle, Color.black, Color.white);
                DrawOutline(new Rect(Screen.width / 2f - 100, Screen.height / 2f + 60, 200, 20), "@chalicefaehse", whomStyle, Color.black, Color.white);
                if (GUI.Button(new Rect(Screen.width / 2f - 25, Screen.height / 2f + 100, 50, 25), "Back"))
                {
                    state = "Start";
                }
                break;
            case "Done":
                break;
            default:
                state = "Start";
                break;
        }
    }

    void DrawOutline (Rect location, string text, GUIStyle style, Color outColour, Color inColour)
    {
        
        GUIStyle origStyle = style;
        style.normal.textColor = outColour;
        location.x--;
        GUI.Label(location, text, style);
        location.x +=2;
        GUI.Label(location, text, style);
        location.x--;
        location.y--;
        GUI.Label(location, text, style);
        location.y +=2;
        GUI.Label(location, text, style);
        location.y--;
        style.normal.textColor = inColour;
        GUI.Label(location, text, style);
        style = origStyle;
    }
}