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

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

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

global.inqArrow = mob => {

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

    if (offCooldown(mob, "ARROW_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, "ARROW_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 minecraft:block.bell.use master @a ${mob.x} ${mob.y} ${mob.z} 3 0`)
            mob.server.scheduleInTicks(8, t => {
                mob.initiateCastSpell(SpellRegistry.MAGIC_ARROW_SPELL.get(), 1)
                }
        )
        mob.server.scheduleInTicks(28, t => {
            mob.server.runCommandSilent(`particle archeryexp:quickdraw_shine ${mob.x} ${mob.y + 2} ${mob.z} 0 0 0 0 1 normal`)
        }
    )
    }

}

global.inqFall = 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 abyssal_decor:foghorn_honk master @a ${mob.x} ${mob.y} ${mob.z} 5 1`)
            mob.server.scheduleInTicks(50, t => {
                mob.initiateCastSpell(SpellRegistry.STARFALL_SPELL.get(), 5)
                }
        )
        }
    }





global.inqTelly = 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", 15 * 20) // Cooldown for 20 seconds
        mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 1)

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


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

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

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

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

}

EntityJSEvents.addGoalSelectors('minecells:inquisitor', event => {
    event.customGoal(
        'inq_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.inqArrow(mob)
                global.inqTelly(mob)
                global.inqFall(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)