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

public class Game : MonoBehaviour {

	static public Game current;

	public enum MouseMode { POINTER, BUILDING };
	public MouseMode mouseMode = MouseMode.POINTER;

/*	public enum BuildingIndex {
		circle_1,
		circle_2,
		circle_3,
		circle_4,
		circle_5,
		circle_6,
		circle_7,
		circle_8,
		circle_9,
		sniper,
		machine_gun
	};
*/
	// Array of 9+ buildings
	public GameObject[] buildingPrefabs;
	public AudioClip[] buildingSounds;
	GameObject currentlyBuilding;
	int currentBuildingIndex=0;

	KeyCode[] keybinds = {
		KeyCode.Alpha1,
		KeyCode.Alpha2,
		KeyCode.Alpha3,
		KeyCode.Alpha4,
		KeyCode.Alpha5,
		KeyCode.Alpha6,
		KeyCode.Alpha7,
		KeyCode.Alpha8,
		KeyCode.Alpha9,
		KeyCode.Q,
		KeyCode.W,
		KeyCode.E,
		KeyCode.R,
		KeyCode.T,
		KeyCode.Y,
	};

	int _totalSouls=0;
	public int totalSouls {
		get { return _totalSouls; }
		set {
			if(value > _totalSouls)
				scoredSouls += value - _totalSouls;

			_totalSouls = value;

			scoreBoard_Souls.text = "Stored Souls: " + _totalSouls;
		}
	}
	int _totalMoney=0;
	public int totalMoney {
		get { return _totalMoney; }
		set {
			if(value > _totalMoney)
				scoredMoney += value - _totalMoney;

			_totalMoney = value;

			if(_totalMoney > 999999999)
				_totalMoney = 999999999;

			scoreBoard_Money.text = "Soul Bucks: $" + _totalMoney;
		}
	}

	public Text scoreBoard_Souls;
	public Text scoreBoard_Money;

	public int scoredSouls=0;
	public int scoredMoney=0;

	public GameObject gameOverWindow;

	// Use this for initialization
	void Start () {
		current = this;

		Time.timeScale = 1;

		scoreBoard_Souls = GameObject.Find ("score_souls").GetComponent<Text>();
		scoreBoard_Money = GameObject.Find ("score_money").GetComponent<Text>();

		totalSouls = 0;
		totalMoney = 1000;
	}
	
	// Update is called once per frame
	void Update () {

		if(Time.timeScale == 0) {
			CancelBuilding();
			return;
		}
	
		if(Input.GetKeyDown(KeyCode.Escape)) {
			CancelBuilding();
		}

		if(Input.GetKeyDown(KeyCode.Z) && Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.LeftShift)) {
			totalMoney += 1000000;
		}

		for(int i=0; i < keybinds.Length; i++) {
			if(Input.GetKeyDown(keybinds[i])) {
				CancelBuilding();
				mouseMode = MouseMode.BUILDING;

				StartBuilding(i);
				break;
			}
		}
	}

	public bool CanAffordCurrentBuilding() {
		return StatManager.BuildingCost( currentBuildingIndex ) <= totalMoney;
	}

	public void StartBuilding( int buildingIndex ) {
		CancelBuilding();

		Vector3 mousePos = Camera.main.ScreenToWorldPoint( Input.mousePosition );
		mousePos.z = 0;
		currentlyBuilding = (GameObject)Instantiate( buildingPrefabs[buildingIndex], mousePos, Quaternion.identity );
		currentBuildingIndex = buildingIndex;

		AudioSource src = GetComponent<AudioSource>();
		src.Stop();
		src.clip = buildingSounds[buildingIndex];
		src.Play();
	}

	public void CancelBuilding() {
		mouseMode = MouseMode.POINTER;

		RangeCircle.TurnOff();

		if(currentlyBuilding != null)
			Destroy (currentlyBuilding);
	}

	public void BuildingWasPlaced() {
		mouseMode = MouseMode.POINTER;
		currentlyBuilding = null;

		totalMoney -= StatManager.BuildingCost(currentBuildingIndex);

	}

	public void Game_Lost() {
		Time.timeScale = 0;
		gameOverWindow.SetActive(true);
	}

	public void Game_Won() {

	}

	public void Restart() {
		Application.LoadLevel( Application.loadedLevel );
		SoulChamber.allChambers = null;
		SoulChamber.chambers = null;
	}

	public void Exit() {
		Application.Quit();
	}
}
