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


global.blazeRun = 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.BURNING_DASH_SPELL.get(), 8)
            })
            mob.server.scheduleInTicks(16, t => {
                mob.initiateCastSpell(SpellRegistry.BURNING_DASH_SPELL.get(), 8)
            })
        }
    }
}

global.blazeSlash = 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:dragon_fire ${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.FLAMING_STRIKE_SPELL.get(), 1)
            })
        }
    }
}

global.blazeBomb = 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:blaze', 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.blazeSlash(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)

EntityJSEvents.addGoalSelectors('minecraft:blaze', event => {
    event.customGoal(
        'blaze_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.blazeRun(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)

EntityJSEvents.addGoalSelectors('minecraft:blaze', 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.blazeBomb(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)


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

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

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

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