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

public class Globals : MonoBehaviour {

	public bool first;
	public int monies;
	public int difficulty;

	// Use this for initialization
	void Awake () 
	{
		Globals[] g = FindObjectsOfType(typeof(Globals)) as Globals[];
		if (g.Length > 1)
		{
			Destroy(this);
			return;
		}

		DontDestroyOnLoad(this);
		Reset();
	}

	public void Reset()
	{
		GameObject halp = Instantiate(Resources.Load("Prefabs/messagebox")) as GameObject;
		halp.name = "Halp";
		halp.transform.Find ("Text").GetComponent<UnityEngine.UI.Text>().text = 
			"Good news! You're a truck driver, and you've been given some very lucrative contracts to \n" +
			"drive some trailers around the city.\n" +
			"\n" +
			"Bad news! You're a monster truck driver. There's no way you can attach the trailers \n" +
			"to your vehicle. You'll just have to improvise, I guess.\n" +
			"\n" +
			"Arrow keys or WASD to move around, ESC to abandon your current contract. Alt+F4 to quit.\n" +
			"\n" +
			"Get the trailers to their rectangles before the timer runs out.";

		halp.transform.Find("Button").Find ("Text").GetComponent<UnityEngine.UI.Text>().text = "Got it";

		difficulty = 0;
		monies = 500;

		halp.transform.Find("Button").GetComponent<UnityEngine.UI.Button>().onClick.AddListener
			(() => { Application.LoadLevel(Application.loadedLevel); } );
	}

	public void failBox()
	{
		int dMonies = 100*(int)Mathf.Pow(3.5f,difficulty-2);

		GameObject halp = Instantiate(Resources.Load("Prefabs/messagebox")) as GameObject;
		halp.transform.Find ("Text").GetComponent<UnityEngine.UI.Text>().text = 
			"You failed to complete the contract in time." +
			"You are fined $"+dMonies.ToString()+".";
		
		halp.transform.Find("Button").Find ("Text").GetComponent<UnityEngine.UI.Text>().text = "Aww";

		monies -= dMonies;
		difficulty--;

		if (difficulty < 0)
			difficulty = 0;
		if (monies < 0)
			monies = 0;

		halp.transform.Find("Button").GetComponent<UnityEngine.UI.Button>().onClick.AddListener
			(() => { Application.LoadLevel(Application.loadedLevel); } );
	}

	public void winBox()
	{
		int dMonies = 200*(int)Mathf.Pow(3.5f,difficulty);

		GameObject halp = Instantiate(Resources.Load("Prefabs/messagebox")) as GameObject;
		halp.transform.Find ("Text").GetComponent<UnityEngine.UI.Text>().text = 
			"You completed the contract. Congratulations!\n\n" +
			"You earned $" + dMonies.ToString() + ".";
			
		
		halp.transform.Find("Button").Find ("Text").GetComponent<UnityEngine.UI.Text>().text = "Next level";
		
		monies += dMonies;
		difficulty += 2;

		halp.transform.Find("Button").GetComponent<UnityEngine.UI.Button>().onClick.AddListener
			(() => { Application.LoadLevel(Application.loadedLevel); } );
	}
}
