using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HudController : MonoBehaviour {
	public EasyFontTextMesh secs, citzensLabel, gameWinLabel;
	// Use this for initialization
	
	public int timer = 300;
	public int savedPeople = 0;
	public static HudController instance;
	public bool menuShown = false;	
	public Transform hasPieces, baseMenu, screenHud, gameOverBase, gameWinBase, holdCell, hudKey;
	public EasyFontTextMesh[] questionLabels;
	public ButtonValue[] questionValues;
	
	public List<int> questions = new List<int>();
	
	
	public Transform[] positions;
	public bool HasKey = false;
	public bool isGameOver = false;
	int citzens = 2;
	
	void Awake () {
		instance = this;
	}
	
	void Start () {
		InvokeRepeating("DecTime",1f,1f);
	}
	
	// Update is called once per frame
	void DecTime () {
		if (!isGameOver){
			timer--;
			secs.Text = "survive " + timer+ " secs";
			
			if (timer ==0)
				GameOver();
		}
	}
	
	
	public void GameOver() {
		if (!isGameOver){
			isGameOver = true;
			if (Player.instance.footSteps.isPlaying)
				Player.instance.footSteps.Stop();
			
			gameOverBase.gameObject.SetActive(true);
			
			Invoke ("LoadMenu", 5f);
		}
	}
	
	public void GameWin () {
		if (!isGameOver){
			isGameOver = true;
			if (Player.instance.footSteps.isPlaying)
				Player.instance.footSteps.Stop();
			
			gameWinBase.gameObject.SetActive(true);
			gameWinLabel.Text = "Survivors:"+savedPeople+"";
			
			int score = PlayerPrefs.GetInt ("SCORE");
			if (savedPeople > score) {
				score = savedPeople;
				PlayerPrefs.SetInt ("SCORE", score);
			}
			Invoke ("LoadMenu", 5f);
		}	
	
	}
	
	void LoadMenu() {
		Application.LoadLevel("MainMenu");
	}
	
	
	int GenQuestion1(){
		int x = Random.Range (0,3);
		string s = "Scream like crazy";
		switch(x){
			case 0: s = "Scream like crazy"; break;
			case 1: s = "Run for your life"; break;
			case 2: s = "Go to the Gate"; break;		
		}
		questionLabels[0].Text = s;
		questionValues[0].value = x;
		return x;
	}
	
	int GenQuestion2(){
		int x = Random.Range (1,4);
		string s = "Scream like crazy";
		switch(x){
		case 1: s = "Dont go there! Noooo"; break;
		case 3: s = "Go find cover"; break;		
		case 2: s = "The shipping is waiting"; break;		
		}
		questionLabels[1].Text = s;
		questionValues[1].value = x;
		return x;
	}
	
	int GenQuestion3(){
		int x = Random.Range (0,5);
		string s = "Scream like crazy";
		switch(x){
		
		case 1: s = "Go Hide"; break;
		case 2: s = "The shipping is waiting"; break;
		case 3: s = "Who are you?"; break;			
		case 4: s = "Are you afraid?"; break;		
		}
		questionLabels[2].Text = s;
		questionValues[2].value = x;
		return x;
	}
	
	public void ShowMenu (){
		print ("showmenu");
		menuShown = true;
		questions.Clear();
		questions.Add(GenQuestion1());
		questions.Add(GenQuestion2());
		questions.Add(GenQuestion3());
		
		baseMenu.gameObject.SetActive(true);
		screenHud.gameObject.SetActive(false);
		if (Player.instance.footSteps.isPlaying)
			Player.instance.footSteps.Stop();
	}
	
	public void HideMenu (){
		menuShown = false;
		baseMenu.gameObject.SetActive(false);
		screenHud.gameObject.SetActive(true);
	}
	
	public void ExecButton(int option) {
		HideMenu();
		switch(option) {
		case 0 :
			HitAim.instance.currNpc.animation.Play ("Crazy");
			AudioManager.instance.Play (Sounds.Crazy,HitAim.instance.currNpc);
			break;
		case 1 : 
			print ("Random");	
			Vector3 v = positions[Random.Range (0, positions.Length)].position;			
			HitAim.instance.currNpc.GetComponent<NavMeshAgent>().SetDestination(v);		
			HitAim.instance.currNpc.animation.Play ("Crazy");
			AudioManager.instance.Play (Sounds.Crazy,HitAim.instance.currNpc);
			break;
		case 3 : 
			print ("Random");	
			Vector3 v3 = positions[Random.Range (0, positions.Length)].position;			
			HitAim.instance.currNpc.GetComponent<NavMeshAgent>().SetDestination(v3);		
			AudioManager.instance.Play (Sounds.Yes,HitAim.instance.currNpc);
			break;
		case 2 : 
			print ("Gate");
			Vector3 v2 = positions[0].position;			
			HitAim.instance.currNpc.GetComponent<NavMeshAgent>().SetDestination(v2);
			AudioManager.instance.Play (Sounds.Yes,HitAim.instance.currNpc);			
			break;
		case 4 : 
			AudioManager.instance.Play (Sounds.Yes,HitAim.instance.currNpc);
			break;
		}
	}
	
	public void DecCitzen() {
		if (!isGameOver) {
			
			GameObject[] o = GameObject.FindGameObjectsWithTag("Npc");
			
			citzens = o.Length;
			
			citzensLabel.Text = "Survivors left:"+citzens;
			if (citzens == 0) {
				GameOver();
	//			gameOber;
			}
		}
	}
	
	public void GetKey () {
	
		hudKey.gameObject.SetActive(true);
		HasKey = true;
	
	}
	
}
