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


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

    let dist = target.distanceToEntity(mob)

    mob.lookAt("eyes", new Vec3d(targetX, targetY, targetZ))

    //This one makes them cut the guy if close
    if (dist > 8 && dist < 15) {
        if (age % 200 === 0) {
            mob.server.scheduleInTicks(8, t => {
                mob.initiateCastSpell(SpellRegistry.BLOOD_STEP_SPELL.get(), 8)
            })
            mob.server.scheduleInTicks(16, t => {
                 mob.server.runCommandSilent(`effect clear @e[limit=1,sort=nearest,type=witch]`)
            })
        }
    }
}

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

    let dist = target.distanceToEntity(mob)

    mob.lookAt("eyes", new Vec3d(targetX, targetY, targetZ))

    //This one makes them cut the guy if close
    if (dist > 0 && dist < 5) {
        if (age % 60 === 0) {
            mob.server.runCommandSilent(`particle irons_spellbooks:unstable_ender ${mob.x} ${mob.y} ${mob.z} 0.5 1 0.5 0.1 100 normal`)
            mob.server.runCommandSilent(`playsound minecraft:block.bell.use master @a ${mob.x} ${mob.y} ${mob.z}`)
            mob.server.scheduleInTicks(8, t => {
                mob.initiateCastSpell(SpellRegistry.BLACK_HOLE_SPELL.get(), 1)
            })
        }
    }
}

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

    let dist = target.distanceToEntity(mob)

    mob.lookAt("eyes", new Vec3d(targetX, targetY, targetZ))

    //This one makes them cut the guy if close
    if (dist > 20 && dist < 30) {
        if (age % 200 === 0) {
            mob.server.runCommandSilent(`particle irons_spellbooks:dragon_fire ${mob.x} ${mob.y} ${mob.z} 0.5 1 0.5 0.1 100 normal`)
            mob.server.runCommandSilent(`playsound abyssal_decor:foghorn_honk master @a ${mob.x} ${mob.y} ${mob.z} 5 1.8`)
            mob.server.scheduleInTicks(8, t => {
                mob.initiateCastSpell(SpellRegistry.FIREBALL_SPELL.get(), 6)
            })
        }
    }
}

EntityJSEvents.addGoalSelectors('minecraft:witch', event => {
    event.customGoal(
        'fire_cut',
        1,
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        true,
        mob => { },
        mob => { },
        true,
        mob => {
            try {
                global.witchSlash(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)

EntityJSEvents.addGoalSelectors('minecraft:witch', event => {
    event.customGoal(
        'witch_dash',
        1,
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        true,
        mob => { },
        mob => { },
        true,
        mob => {
            try {
                global.witchRun(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)

EntityJSEvents.addGoalSelectors('minecraft:witch', event => {
    event.customGoal(
        'fire_cut',
        1,
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        true,
        mob => { },
        mob => { },
        true,
        mob => {
            try {
                global.witchBomb(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)


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

    if (!player) return
    mob.initiateCastSpell(SpellRegistry.BLOOD_STEP_SPELL.get(), 20)
}
)

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

    if (event.source.getType() === 'drown') {
        event.cancel ();
    }
})