using UnityEngine;
using System.Collections;

public class GUIController : MonoBehaviour {
	public bool isCaught;
	public bool lootDialog = false;
	public GameObject boulder;
	private PlayerController player;
	private string[] itemNames = new string[]{"None", "Weapon", "Cloak", "Food"};
	private bool dialog;

	private string[] texts = new string[]{
		"Hey I found this sword thing. Wanna trade?",
		"Any chance you could throw that cloak over the boulder so that I can get through?",
		"Thanks Friend",
		"No idea. I've never been that far before."
	};
	private string[] btnTxt1 = new string[]{
		"OOOH! Shiney.",
		"Yeah, no problem.",
		"Now move the boulder or GET CUT!"
	};
	private string[] btnTxt2 = new string[]{
		"Nah man, I like my cloak thing.",
		"No can do demon bro.",
		"How do I get past this boulder?"
	};
	private int txtIndex;
	private int btn1Index;
	private int btn2Index;
	// Use this for initialization
	void Start () {
		player = GameObject.Find("Player").GetComponent<PlayerController>();
		dialog = false;
		isCaught = false;

		txtIndex = 0;
		btn1Index = 0;
		btn2Index = 0;
	}
	
	// Update is called once per frame
	void Update () {

	}

	void OnGUI(){
		int room = player.GetCurrentRoom();
		if( player.GetCurrentRoom() == 1 ){ EnterFirstRoom(); }

		int txtH = Mathf.CeilToInt( ((float)Screen.height)*0.1f );
		int scrH = Mathf.CeilToInt( ((float)Screen.height)*0.9f);

		int txtW = Mathf.CeilToInt( ((float)Screen.width)*0.2f );
		//GUI.TextArea(new Rect(0f, 0f, txtW, txtH), "Health: " + player.GetHealth().ToString() );

		int scrW = Mathf.CeilToInt( ((float)Screen.width)*0.7f );
		txtW = Mathf.CeilToInt( ((float)Screen.width)*0.1f );
		GUI.TextArea(new Rect(scrW, 0f, txtW, txtH), "Inventory");

		scrW = Mathf.CeilToInt( ((float)Screen.width)*0.8f );
		GUI.TextArea(new Rect(scrW, 0f, txtW, txtH), itemNames[player.GetItemType()]);
		Rect alertRect = new Rect(Screen.width*0.35f, Screen.height*0.35f, Screen.width*0.3f, Screen.height*0.3f);
		if(isCaught){
			GUI.TextArea(alertRect, "You were grabbed by the demons");
		}

		if(lootDialog & room == 2 ){
			GUI.TextArea(alertRect, "The NPC dropped a cloak. You picked it up.");
		}
		if(dialog){ DoDialog(); }
	}

	void EnterFirstRoom(){
		int txtH = Mathf.CeilToInt( ((float)Screen.height)*0.1f );
		int scrH = Mathf.CeilToInt( ((float)Screen.height)*0.9f);
		GUI.TextArea(new Rect(0f,scrH,Screen.width,txtH), "You can take only one item.");
	}

	void DoDialog(){
		Rect txtWindow = new Rect(Screen.width*0.35f, Screen.height*0.35f, Screen.width*0.3f, Screen.height*0.3f);
		Rect btn1 = new Rect(Screen.width*0.35f, Screen.height*0.7f, Screen.width*0.3f, Screen.height*0.15f);
		Rect btn2 = new Rect(Screen.width*0.35f, Screen.height*0.85f, Screen.width*0.3f, Screen.height*0.15f);
		if( player.GetCurrentRoom() == 3 ){
			GUI.TextArea(txtWindow, texts[txtIndex]);
			if( GUI.Button(btn1, btnTxt1[btn1Index] ) ){ 
				if(btn1Index == 0){ player.SetItemType("Sword"); btn2Index = 2; btn1Index = 2; txtIndex = 2; }
				else if(btn1Index == 1){ player.SetItemType("Food"); Destroy (boulder); dialog=false; }
				else if(btn1Index == 2){ Destroy(boulder); dialog = false; }
			}
			if( GUI.Button(btn2, btnTxt2[btn2Index] ) ){ 
				if(btn2Index == 0){ btn2Index++; btn1Index++; txtIndex++; }
				else if(btn2Index == 1){ player.SetItemType("Cloak");  boulder.GetComponent<BoxCollider>().isTrigger = true; dialog = false; }
				else if(btn2Index == 2){ txtIndex=3; }
			}
		}
	}

	public void SetDialog(){ dialog = true; }
}
