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

public class UICtrl : MonoBehaviour {

	public static UICtrl instance;

	public GameObject PrepareUI;
	public GameObject InGameUI;
	public GameObject EndGameUI;
	public Text ingameTutorial;
	public Text ingameScore;
	public Text endgameScore;

	public Image machine;
	public Image machineDisp;
	public Sprite[] machineState;
	public Sprite[] machineContent;
	public GameObject machineText;

	int loopCount = 0;
	int currentIndex = 0;
	float TutorialTime = 8;

	// Use this for initialization
	void Awake () {
		instance = this;
	}
	
	// Update is called once per frame
	void Update () {
		if(InGameUI.activeSelf){
			ingameScore.text = GameManager.instance.currentScore()+"";
		}
		if(ingameTutorial.enabled&&TutorialTime>0){
			TutorialTime-= Time.deltaTime;
			if(TutorialTime<=0){
				ingameTutorial.enabled = false;
			}
		}
	}

	public void EnableInGameUI(int gameMode){
		ingameScore.text = "0";
		InGameUI.SetActive (true);
		ingameTutorial.enabled = true;
		TutorialTime = 8;
		switch(gameMode){
		case 0:
			ingameTutorial.text = "CLICK on a COMET and HOLD to generate gravity force";
			break;
		case 1:
			ingameTutorial.text = "CLICK to use your JET";
			break;
		case 2:
			ingameTutorial.text = "CLICK to SHOOT";
			break;
		}

	}

	public void EnableGameOverUI(int score){
		endgameScore.text = score+"";
		InGameUI.SetActive (false);
		EndGameUI.SetActive (true);
	}

	public void CloseGameOverUI(){
		EndGameUI.SetActive (false);
	}

	public void DisplayPrepareUI(){
		machineDisp.gameObject.SetActive (false);
		PrepareUI.SetActive (true);
		machineText.SetActive (true);
		machine.sprite = machineState [0];
	}

	public void ClosePrepareUI(){
		PrepareUI.SetActive (false);
	}

	IEnumerator StartRandomContent(){
		loopCount = 0;
		while(loopCount<10){
			yield return new WaitForSeconds(0.15f);
			currentIndex++;
			if(currentIndex==machineContent.Length){
				currentIndex = 0;
			}
			machineDisp.sprite = machineContent[currentIndex];
			loopCount++;
		}
		loopCount = 0;
		while(loopCount<6){
			yield return new WaitForSeconds(0.05f);
			currentIndex++;
			if(currentIndex==machineContent.Length){
				currentIndex = 0;
			}
			machineDisp.sprite = machineContent[currentIndex];
			loopCount++;
		}
		loopCount = 0;
		while(loopCount<12){
			yield return new WaitForSeconds(0.04f);
			currentIndex++;
			if(currentIndex==machineContent.Length){
				currentIndex = 0;
			}
			machineDisp.sprite = machineContent[currentIndex];
			loopCount++;
		}
		int result = GameManager.instance.GetRandomMode ();
		machineDisp.sprite = machineContent[result];
		yield return new WaitForSeconds (3);
		ClosePrepareUI ();
		GameManager.instance.StartGameMode (result);
	}

	public void PrepareActive(){
		if(machineText.activeSelf){
			machineText.SetActive(false);
			machineDisp.gameObject.SetActive(true);
			machine.sprite = machineState[1];
			SoundFXCtrl.instance.PlaySound(2,1);
			StartCoroutine(StartRandomContent());
		}
	}

}
