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

public class CooldownProcessor : MonoBehaviour {

    private CooldownTracker[] trackers;
	// Use this for initialization
	void Start () {
        trackers = FindObjectsOfType<CooldownTracker>();

        foreach(CooldownTracker ct in trackers)
        {
            ct.NextChargeTime = Time.time + ct.Cooldown;
            ct.UsedCharges = 0;
            ct.AvailableCharges = 0;
        }
	}
	
	// Update is called once per frame
	void Update () {
		foreach(CooldownTracker ct in trackers)
        {
            if (ct.AvailableCharges < ct.MaxCharges)
            {
                if (ct.NextChargeTime < Time.time)
                {
                    // Time for charging
                    ct.AvailableCharges++;
                    ct.NextChargeTime = Time.time + ct.Cooldown;
                }
            }

            if (ct.UsedCharges > 0)
            {
                if (ct.AvailableCharges == ct.MaxCharges)
                {
                    // Anytime we activate the ability while we are at full charges, we have to trigger a full cooldown cycle
                    ct.NextChargeTime = Time.time + ct.Cooldown;
                }

                ct.AvailableCharges -= ct.UsedCharges;
                ct.UsedCharges = 0;
            }
        }
	}
}
