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

public class BlinkScript : MonoBehaviour {

	public float timer = 0f;
	public int blink = 0;
	public Sprite closed;
	public Sprite orig;
	public int level = 0;
	public int maxlevel = 2;
	public int selected = 0;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetKeyDown (KeyCode.Escape)) {
			if (level == 0)
			{
				Application.Quit();
			}
			else
			{
				GameObject.Find("Restart_Text").GetComponent<Text>().text = "PRESS ENTER";
				GameObject.Find("LevelName").GetComponent<Text>().enabled = false;
				GameObject.Find("left").GetComponent<SpriteRenderer>().enabled = false;
				GameObject.Find("right").GetComponent<SpriteRenderer>().enabled = false;
				level = 0;
			}
		}

		if (Input.GetKeyDown (KeyCode.Return)) {

			if (level > 0)
			{
				selected = 1;
				GameObject.Find("LevelName").GetComponent<Text>().text = "L O A D I N G    P L E A S E    W A I T";
				GameObject.Find("left").GetComponent<SpriteRenderer>().enabled = false;
				GameObject.Find("right").GetComponent<SpriteRenderer>().enabled = false;
				switch (level)
				{
					case 1: Application.LoadLevel("Tutorial"); break;
					case 2: Application.LoadLevel("Level1"); break;
				}
			}
			else
			{
				GameObject.Find("Restart_Text").GetComponent<Text>().text = "PICK A LEVEL";
				GameObject.Find("LevelName").GetComponent<Text>().enabled = true;
				GameObject.Find("left").GetComponent<SpriteRenderer>().enabled = true;
				GameObject.Find("right").GetComponent<SpriteRenderer>().enabled = true;
				level = 1;
			}
		}

		if (level > 0 && selected == 0) {
			if (Input.GetKeyDown(KeyCode.LeftArrow))
			{
				level -= 1;
				if (level == 0)
				{
					level = maxlevel;
				}
			}
			if (Input.GetKeyDown(KeyCode.RightArrow))
			{
				level += 1;
				if (level > maxlevel)
				{
					level = 1;
				}
			}

			switch (level)
			{
			case 1: GameObject.Find("LevelName").GetComponent<Text>().text = "C I T Y    B L O C K"; break;
			case 2: GameObject.Find("LevelName").GetComponent<Text>().text = "S U B U R B ' S    E D G E"; break;
			}
		}
	
		timer += Time.deltaTime;

		if (timer >= 2.2f) {
			blink = 1;
		}
		if (timer >= 2.3f) {
			blink = 0;
			timer = 0;
		}

		if (blink == 1) {
			gameObject.GetComponent<SpriteRenderer>().sprite = closed;
		}
		if (blink == 0) {
			gameObject.GetComponent<SpriteRenderer>().sprite = orig;
		}

	}
}
