/* 
This file contains utility functions for adding data to mobs.
They are used to track, store, and reduce cooldowns for whatever you might-
want to store and execute.

Note that if a mob is currently casting an irons spell, and you have another irons spell go off cooldown-
the new spell will overwrite the previous ones cooldown. 
You can prevent this using a check like this:  V (! means not)                 V (returns if the mob is casting)
if (offCooldown(mob, "SONIC_BOOM_COOLDOWN") && !mob.irons_spellbooks$magicData.casting) { }
*/

/** Checks if mobCooldowns data exists for the mob, creates it if it doesn't 
 * @param {Internal.PathfinderMob} mob */
function cooldownsExist(mob) {
    if (mob.persistentData["mobCooldowns"] == null) {
        mob.persistentData["mobCooldowns"] = {}
    }
}

/**
 * Reduces all of the mobs stored cooldowns by 1 each tick till 0
 * Should be called in your entity every tick
 * Add cooldowns with addCooldown(mobEntity, cooldownName, cooldownTicks)
 * @param {Internal.PathfinderMob} mob */
function reduceCooldowns(mob) {
    cooldownsExist(mob)
    let mobCooldowns = mob.persistentData["mobCooldowns"]

    for (let [key, cdValue] of Object.entries(Object.assign({}, mobCooldowns))) {
        if (cdValue > 0) {
            mobCooldowns[key] = cdValue - 1
        }
    }
}

/** Checks if a cooldown is off
 * @param {Internal.PathfinderMob} mob 
 * @param {Number} cooldownName 
 * @returns {Boolean} */
function offCooldown(mob, cooldownName) {
    cooldownsExist(mob)
    let cooldown = mob.persistentData["mobCooldowns"][cooldownName]

    if (cooldown == null) {
        mob.persistentData["mobCooldowns"][cooldownName] = 0
        return true
    } else if (cooldown <= 0) {
        return true
    } else {
        return false
    }
}

/** Adds a new cooldown timer to a mob
 * @param {Internal.PathfinderMob} mob 
 * @param {String} cooldownName 
 * @param {Number} cooldownTicks */
function addCooldown(mob, cooldownName, cooldownTicks) {
    cooldownsExist(mob)
    let mobCooldowns = mob.persistentData["mobCooldowns"]
    mobCooldowns[cooldownName] = cooldownTicks
}