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

public class UpgradePanel : MonoBehaviour {

	public GameObject upgradeButtonPrefab;

	// Use this for initialization
	void Start () {
		foreach(Upgrade u in upgrades) {
			Upgrade u2 = u;
			GameObject btn = ((GameObject)Instantiate(upgradeButtonPrefab));
			btn.transform.SetParent(this.transform, false);
			btn.GetComponent<Button>().onClick.AddListener( () => { ProcessUpgrade(btn, u2); } );
			btn.GetComponentInChildren<Text>().text =  "<color=green>$" + string.Format("{0:n0}", u2.cost) + "</color> " + u2.text;
			btn.GetComponent<UpgradeButton>().myCost = u2.cost;
/*			Tooltip t = btn.AddComponent<Tooltip>();
			t.text = u.text;
			t.cost = u.cost;
*/		}
	}
	
	// Update is called once per frame
	void Update () {

	}




	void ProcessUpgrade(GameObject btn, Upgrade u) {
		Debug.Log ("Processing upgrade: " + u.text);
		if(Game.current.totalMoney < u.cost) {
			Debug.Log ("No monies!");
			return;
		}

		Game.current.totalMoney -= u.cost;
		u.OnUnlock(u);
		Destroy(btn);
	}



	class Upgrade {
		public string text = "Upgrade Text";
		public int cost = 100;
		public Sprite icon = null;
		public Action<Upgrade> OnUnlock = null;

		public Upgrade(string t, int c, Sprite i, Action<Upgrade> a) {
			text = t;
			cost = c;
			icon = i;
			OnUnlock = a;
		}
	}

	Upgrade[] upgrades = {
		new Upgrade("+2 Damage Per Bullet",  200, null, (u) => { AddFlatDamage(u, 2); } ),
		new Upgrade("+4 Damage Per Bullet",  400, null, (u) => { AddFlatDamage(u, 4); } ),
		new Upgrade("+6 Damage Per Bullet",  800, null, (u) => { AddFlatDamage(u, 6); } ),
		new Upgrade("+8 Damage Per Bullet", 1600, null, (u) => { AddFlatDamage(u, 8); } ),
		new Upgrade("+10 Damage Per Bullet", 3200, null, (u) => { AddFlatDamage(u, 10); } ),
		new Upgrade("+12 Damage Per Bullet", 6400, null, (u) => { AddFlatDamage(u, 12); } ),
		new Upgrade("+14 Damage Per Bullet", 12800, null, (u) => { AddFlatDamage(u, 14); } ),
		new Upgrade("+16 Damage Per Bullet", 25600, null, (u) => { AddFlatDamage(u, 16); } ),
		new Upgrade("+5 Soul Capacity for Chambers",  500, null, (u) => { BonusChamberCapacity(u, 5); } ),
		new Upgrade("+5 Soul Capacity for Chambers",  5000, null, (u) => { BonusChamberCapacity(u, 5); } ),
		new Upgrade("+5 Soul Capacity for Chambers",  50000, null, (u) => { BonusChamberCapacity(u, 5); } ),
		new Upgrade("+100% Faster Soul Intake from Portal",  100, null, (u) => { PortalSpeedBoost(u, 1.0f); } ),
		new Upgrade("+100% Faster Soul Intake from Portal",  2500, null, (u) => { PortalSpeedBoost(u, 1.0f); } ),
		new Upgrade("+100% Faster Soul Intake from Portal",  20000, null, (u) => { PortalSpeedBoost(u, 1.0f); } ),
		new Upgrade("+9001% Faster Soul Intake from Portal",  1000000, null, (u) => { Application.LoadLevel("_SCENE_VICTORY_"); } ),
		new Upgrade("+25% Turret Fire Rate",  1000, null, (u) => { AddTurretFireRate(u, 0.25f); } ),
		new Upgrade("+50% Turret Fire Rate",  10000, null, (u) => { AddTurretFireRate(u, 0.5f); } ),
		new Upgrade("+100% Turret Fire Rate",  100000, null, (u) => { AddTurretFireRate(u, 1.0f); } ),
		new Upgrade("Souls Generate 100% more money!",  1000, null, (u) => { BonusSoulIncome(u, 1f); } ),
		new Upgrade("Souls Generate 100% more money!",  25000, null, (u) => { BonusSoulIncome(u, 1f); } ),
		new Upgrade("Souls Generate 100% more money!",  100000, null, (u) => { BonusSoulIncome(u, 1f); } ),
		new Upgrade("Souls Generate 100% more money!",  250000, null, (u) => { BonusSoulIncome(u, 1f); } ),
		new Upgrade("+1 Range for Boost Towers",  1000, null, (u) => { AddBoostRange(u, 1f); } ),
		new Upgrade("+1 Range for Boost Towers",  50000, null, (u) => { AddBoostRange(u, 1f); } ),
		new Upgrade("+1 Range for Boost Towers",  250000, null, (u) => { AddBoostRange(u, 1f); } ),
		new Upgrade("+25% Turret Range",  300, null, (u) => { AddTurretRange(u, 0.25f); } ),
		new Upgrade("+25% Turret Range",  3000, null, (u) => { AddTurretRange(u, 0.25f); } ),
		new Upgrade("+25% Turret Range",  30000, null, (u) => { AddTurretRange(u, 0.25f); } ),
		new Upgrade("+25% Turret Range",  300000, null, (u) => { AddTurretRange(u, 0.25f); } ),
		new Upgrade("+0.5 Portal Regeneration per 10secs",  10000, null, (u) => { StatManager.current.portalRegen += 0.5f; } ),
		new Upgrade("+0.5 Portal Regeneration per 10secs",  100000, null, (u) => { StatManager.current.portalRegen += 0.5f; } ),
	};

	public bool UpgradeAvailable() {
		foreach(UpgradeButton ub in transform.GetComponentsInChildren<UpgradeButton>()) {
			if(ub.myCost <= Game.current.totalMoney)
				return true;
		}

		return false;
	}

	static void AddFlatDamage(Upgrade u, int amt) {
		Debug.Log ("AddFlatDamage -- Unlocked by: " + u.text);
		
		StatManager.current.bonusFlatDamage += amt;
	}
	
	static void AddTurretRange(Upgrade u, float amt) {
		Debug.Log ("AddTurretRange -- Unlocked by: " + u.text);
		
		StatManager.current.bonusTurretRange += amt;
	}
	
	static void AddBoostRange(Upgrade u, float amt) {
		Debug.Log ("AddBoostRange -- Unlocked by: " + u.text);

		Booster.RemoveAllBoosts();
		StatManager.current.bonusBoostRange += amt;
		Booster.ApplyAllBoosts();
	}
	
	static void AddTurretFireRate(Upgrade u, float amt) {
		Debug.Log ("AddTurretFireRate -- Unlocked by: " + u.text);
		
		StatManager.current.bonusFireRate += amt;
	}

	static void BonusSoulIncome(Upgrade u, float amt) {
		Debug.Log ("BonusSoulIncome -- Unlocked by: " + u.text);
		
		StatManager.current.bonusSoulIncome += amt;
	}

	static void BonusChamberCapacity(Upgrade u, int amt) {
		Debug.Log ("BonusChamberCapacity -- Unlocked by: " + u.text);
		
		StatManager.current.bonusChamberCapacity += amt;
	}

	static void PortalSpeedBoost(Upgrade u, float amt) {
		Debug.Log ("PortalSpeedBoost -- Unlocked by: " + u.text);
		
		StatManager.current.bonusPortalSpeed += amt;

	}
}
