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

public class UIBehavior : MonoBehaviour {

	public GameObject titleScreen;
	public GameObject startAsset;
	public GameObject endAsset;
	public GameObject keyboardAsset;
	private bool enterKeyboard = false;

	private Vector3 keyboardStartPos;
	private Vector3 keyboardEndPos;
	private float lerpTime = 0.8f;
	private float currentLerpTime;

	private bool doGameOver = false;
	private bool reloadScene = false;


	// Use this for initialization
	void Start () {
		keyboardStartPos = keyboardAsset.transform.position;
		Vector3 pos = keyboardStartPos;
		pos.y = 7f;
		keyboardEndPos = pos;
	}
	
	// Update is called once per frame
	void Update () {

		if (!Dictionaries.dictionaries.startGame)
		{
			if (Input.anyKeyDown)
			{
				titleScreen.SetActive(false);
				startAsset.SetActive(false);
				StartCoroutine(StartGameDelay(2f));
				enterKeyboard = true;
			}
		}

		if (enterKeyboard)
		{
			currentLerpTime += Time.deltaTime;
//			if (currentLerpTime > lerpTime)
//			{
//				currentLerpTime = 0;
//			}
			
			float t = currentLerpTime / lerpTime;
			t = t*t*t * (t * (6f*t - 15f) + 10f);
			Vector3 vect = Vector3.Lerp(keyboardStartPos, keyboardEndPos, t);
			keyboardAsset.transform.position = vect;

			if (t >= 1f)
			{
				enterKeyboard = false;
			}
		}

		if (Dictionaries.dictionaries.gameOver && !doGameOver)
		{
			doGameOver = true;
			Vector3 pos = keyboardStartPos;
			keyboardStartPos = keyboardEndPos;
			keyboardEndPos = pos;
			enterKeyboard = true;
			StartCoroutine(GameOverDelay(2f));

		}
		if (reloadScene)
		{
			if (Input.anyKeyDown)
			{
				Application.LoadLevel(Application.loadedLevel);
			}
		}
	
	}

	public IEnumerator StartGameDelay(float time)
	{
		yield return new WaitForSeconds (time);

		Dictionaries.dictionaries.startGame = true;

	}

	public IEnumerator GameOverDelay(float time)
	{
		yield return new WaitForSeconds (time);

		titleScreen.SetActive(true);
		endAsset.SetActive(true);
		endAsset.GetComponentInChildren<Text>().text = Dictionaries.dictionaries.score.ToString();
		reloadScene = true;
		

	}
}
