using UnityEngine;
using System.Collections.Generic;

public class SoulChamber : MonoBehaviour {

	static public Color32[] soulColors = {
		new Color32(255, 255, 255, 255),
		new Color32(249, 66, 244, 255),
		new Color32(0, 255, 44, 255),
		new Color32(255, 142, 0, 255),
		new Color32(255, 0, 0, 255),
		new Color32(255, 255, 0, 255),
		new Color32(0, 255, 234, 255),
		new Color32(19, 0, 255, 255),
		new Color32(0, 0, 0, 255),
	};

	public int circleNumber = 1;

	public static Dictionary<int, List<SoulChamber>> chambers;
	public static List<SoulChamber> allChambers;

	int _base_capacity = 10;
	public int capacity {
		get { return _base_capacity + StatManager.current.bonusChamberCapacity; }
	}

	int _numSouls = 0;
	public int numSouls {
		get { return _numSouls; }
		set {
			Game.current.totalSouls += value-_numSouls;
			_numSouls = value;
		}
	}

	float cooldownRate = 5f;
	float cooldown = 5f;

	public FloatingText floatingText;

	public MeshRenderer progressWheel;

	public GameObject fullness;

	// Use this for initialization
	void Start () {
		if(chambers == null) {
			chambers = new Dictionary<int, List<SoulChamber>>();
			allChambers = new List<SoulChamber>();
		}

		if(chambers.ContainsKey(circleNumber) == false)
			chambers[circleNumber] = new List<SoulChamber>();

		chambers[circleNumber].Add(this);
		allChambers.Add (this);

		fullness = this.transform.Find ("Fullness").gameObject;
	}

	void OnDestroy() {
		// TODO: Release souls?
		numSouls = 0;

		if(chambers != null && chambers.ContainsKey(circleNumber))
			chambers[circleNumber].Remove(this);

		if(allChambers != null)
			allChambers.Remove(this);
	}

	// Update is called once per frame
	void Update () {
		progressWheel.material.SetFloat("_Cutoff", Mathf.Max(0.01f, cooldown/cooldownRate));
		fullness.GetComponent<TextMesh>().text = Mathf.FloorToInt(100f*numSouls/capacity) + "%";

		if(numSouls == 0)
			return;

		cooldown -= Time.deltaTime;

		if(cooldown < 0) {
			cooldown = cooldownRate;

			int amt = (int)(numSouls * circleNumber * (1f + StatManager.current.bonusSoulIncome));

			Game.current.totalMoney += amt;

			floatingText.StartFloat("$" + amt);

		}


	}

	void OnMouseDown() {
		cooldown -= 0.25f;
	}
}
