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

public class GameData : MonoBehaviour {

	public Text t;
	public float rTime;
	public static float remainingTime;
	public int numTri;
	public static int numOfTribees;
	public GameObject winLevel; 
	public GameObject loseLevel; 
	public GameObject esir;

	public GameObject shaman;

	public GameObject[] wins;
	public static bool died=false;

	bool picShown=false;

	public string[] fTemp;
	public string[] kkTemp;
	public GameObject EndMTemp;
	public string[] sTemp;
	public string[] wTemp;

	public static string[] fireDTexts;
	public static string[] kuyuDTexts;
	public static string[] winDTexts;
	public static string[] shamanDTexts;
	public static GameObject EndMessage;

	public static void randomFireText()
	{
		EndMessage.GetComponentInChildren<Text> ().text = fireDTexts [Random.Range(0, fireDTexts.Length)];
		EndMessage.SetActive (true);
	}
	public static void randomKuyuText()
	{
		EndMessage.GetComponentInChildren<Text> ().text = kuyuDTexts [Random.Range(0, kuyuDTexts.Length)];
		EndMessage.SetActive (true);
	}
	public static void randomWinText()
	{
		EndMessage.GetComponentInChildren<Text> ().text = winDTexts [Random.Range(0, winDTexts.Length)];
		EndMessage.SetActive (true);
	}
	public static void randomShamanText()
	{
		EndMessage.GetComponentInChildren<Text> ().text = shamanDTexts [Random.Range(0, shamanDTexts.Length)];
		EndMessage.SetActive (true);
	}

	void FixedUpdate()
	{
		if (Input.anyKey && died) {
			SceneManager.LoadScene (SceneManager.GetActiveScene().name);
		}
	}

	void decreaseTime()
	{
		if ((remainingTime>=1 && numOfTribees>0))
			remainingTime--;
		//Debug.Log (remainingTime);
		t.text = "Take PosItIon for RItual  "+remainingTime;

		if (remainingTime == 0 && !picShown) {
			Invoke ("makeDied", 2.5f);
			loseLevel.SetActive (true);
			Invoke ("showShamanPic", 1.2f);
		}
			
			//SceneManager.LoadScene (SceneManager.GetActiveScene().name);

		if (numOfTribees == 0) {

			winLevel.SetActive (true);
			esir.SetActive (false);
			Invoke ("winOperations", 1.5f);

		}
			
	}

	// Use this for initialization
	void Awake () {
		remainingTime = rTime;
		numOfTribees = numTri;
		InvokeRepeating ("decreaseTime", 0, 1);

		fireDTexts = fTemp;
		kuyuDTexts = kkTemp;
		EndMessage = EndMTemp;
		shamanDTexts = sTemp;
		winDTexts = wTemp;
	}

	void showShamanPic()
	{
		if (!picShown) {
			randomShamanText ();
			shaman.SetActive (true);
		}

		
		picShown = true;
	}

	void makeDied()
	{
		died = true;
	}

	void winOperations()
	{


		if (wins != null && !picShown) {
			picShown = true;
			randomWinText ();
			wins [Random.Range (0, 2)].SetActive (true);
			Invoke ("LoadNextLvl", 3f);
		}

	}

	void LoadNextLvl()
	{
		int index = SceneManager.GetActiveScene ().buildIndex;
		SceneManager.LoadScene (++index);
	}
		

}
