import { Entity, system } from "@minecraft/server"

class EntityAbilitySystem {
    constructor() {
        this.Start()
    }
    /**
     * @type {[{id: String, cooldownTicks:Number, cooldownType: string}]} AnimalsOnCooldown
     */
    AnimalsOnCooldown=[]
    
    /**
     *@public
    * Sets the cooldown for an entity ability
     * @param {string} entityId 
       * the unique id of the entity so it can be checked against later
     * @param {number} cooldownTicks 
     *  Duration in ticks this cooldown will last for
     * @param {string} cooldownType 
     * What Ability this relates to so multiple cooldowns can be active at once
     */
    SetCooldown(entityId,cooldownTicks,cooldownType) {
        this.AnimalsOnCooldown.push({id:entityId,cooldownTicks:cooldownTicks,cooldownType:cooldownType});

    }
    /**
     * @public
     * Checks if an ability is on cooldown
     * @param {string} entityId 
     * Unique id used to identify entity
     * @param {string} coolDownType 
     * Abiltiy to check
     */
    AbilityReady(entityId,coolDownType) {
        const findAbility = this.AnimalsOnCooldown.find((cooldownInfo)=> {if (cooldownInfo.id == entityId && cooldownInfo.cooldownType == coolDownType) return true});
        return findAbility == undefined ? true : false
    }
/**
 * @private
 */
    Start() {
        system.runInterval(()=> {
         for (let index=0; index < this.AnimalsOnCooldown.length;index++) {
            const cooldownInfo=this.AnimalsOnCooldown[index];
            if (cooldownInfo.cooldownTicks != 0) {
                cooldownInfo.cooldownTicks--;
            }
            else {
                this.AnimalsOnCooldown.splice(index,1)
            }
         }
        })
    }


}

const entityAbilitySystem = new EntityAbilitySystem();
export default entityAbilitySystem;