let WizardAttackGoal = Java.loadClass("io.redspace.ironsspellbooks.entity.mobs.goals.WizardAttackGoal")
EntityJSEvents.addGoalSelectors('caverns_and_chasms:mime', event => {
    event.arbitraryGoal(1, (e) => {
        return new WizardAttackGoal(e,3, 40) // Parameters: entity, movement speed modifier, cast interval
            .setSpells(
                [], // Attack
                [], // Defense
                [], // Movement
                [] // Support
            )
    })
})

EntityEvents.hurt('caverns_and_chasms:mime', event => {
    const player = event.source.player
    const mob = event.entity

    if (!player) return
    mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 1)
}
)

global.mimArrow = mob => {

    let { age, target, target: { x: targetX, y: targetY, z: targetZ } } = mob
    if ((!target || !mob.hasLineOfSight(target))) return

    if (offCooldown(mob, "SLASH_COOLDOWN") && !mob.casting) {
        //mob.server.tell(`${mob.displayName.string} is casting fortify`)
        if (mob.casting) { // If mob is casting, cancel the cast, since some spells cannot be interrupted normally like sonic boom
            mob.cancelCast()
        }
        addCooldown(mob, "SLASH_COOLDOWN", 5 * 20) // Cooldown for 5 seconds
            mob.server.runCommandSilent(`particle irons_spellbooks:unstable_ender ${mob.x} ${mob.y} ${mob.z} 0.5 0.5 0.5 0.2 300 normal`)
            mob.server.runCommandSilent(`particle minecells:charge ${mob.x} ${mob.y + 2} ${mob.z} 0 0 0 0.1 100 normal`)
            mob.server.runCommandSilent(`playsound irons_spellbooks:spell.summon_swords.cast master @a ${mob.x} ${mob.y} ${mob.z} 5 0`)
            mob.server.scheduleInTicks(12, t => {
                mob.initiateCastSpell(SpellRegistry.SHADOW_SLASH.get(), 1)
                }
        )
    }

}

global.mimFall = mob => {

    let { age, target, target: { x: targetX, y: targetY, z: targetZ } } = mob
    if ((!target || !mob.hasLineOfSight(target))) return

    if (offCooldown(mob, "FALL_COOLDOWN") && !mob.casting) {
        //mob.server.tell(`${mob.displayName.string} is casting fortify`)
        if (mob.casting) { // If mob is casting, cancel the cast, since some spells cannot be interrupted normally like sonic boom
            mob.cancelCast()
        }
        addCooldown(mob, "FALL_COOLDOWN", 22 * 20) // Cooldown for 22 seconds
            mob.server.runCommandSilent(`particle irons_spellbooks:unstable_ender ${mob.x} ${mob.y} ${mob.z} 0.5 0.5 0.5 0.2 300 normal`)
            mob.server.runCommandSilent(`particle minecells:charge ${mob.x} ${mob.y + 2} ${mob.z} 0 0 0 0.1 100 normal`)
            mob.server.runCommandSilent(`playsound irons_spellbooks:spell.summon_swords.charge master @a ${mob.x} ${mob.y} ${mob.z} 5 1`)
            mob.server.scheduleInTicks(30, t => {
                mob.initiateCastSpell(SpellRegistry.ARCANE_SHACKLE_SPELL.get(), 1)
                    }
        )
        }
    }



global.mimTelly = mob => {

    let { age, target, target: { x: targetX, y: targetY, z: targetZ } } = mob
    if ((!target || !mob.hasLineOfSight(target))) return

    if (offCooldown(mob, "TELEPORT_COOLDOWN") && !mob.casting) {
        //mob.server.tell(`${mob.displayName.string} is casting barrage`)
        if (mob.casting) { // If mob is casting, cancel the cast, since some spells cannot be interrupted normally like sonic boom
            mob.cancelCast()
        }
        addCooldown(mob, "TELEPORT_COOLDOWN", 20 * 30) // Cooldown for 30 seconds
        mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 1)

        mob.server.scheduleInTicks(20, t => {
            mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 1)
        })

        mob.server.scheduleInTicks(60, t => {
            mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 1)
        })

        mob.server.scheduleInTicks(120, t => {
            mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 1)
        })
    }

}

EntityJSEvents.addGoalSelectors('caverns_and_chasms:mime', event => {
    event.customGoal(
        'mime_shoot',
        1,
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        true,
        mob => { },
        mob => { },
        true,
       /** @param {Internal.BeeEntityJS} mob */ mob => {
            try {
                reduceCooldowns(mob)
                global.mimArrow(mob)
                global.mimTelly(mob)
                global.mimFall(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)