// THE DEEPA CAT SAVED MY SOUL WITH THIS ONE, THANK YOU DEEPA THE CREATURE CAT
let backGoal = "com.github.mim1q.minecells.entity.ai.goal.JumpBackGoal"  //Won't work for every goal but you can replace this with another kind of goal if you so prefer.

function flipBurn(mob) {
    let t = mob.target
    if (!t) return
    mob.goalSelector.runningGoals.forEach(g => {                            // This whole section is responsible for even making the casting real
        if (g.goal.class.name.toString() == backGoal) {
            if (mob.age % 20 != 0) return                                   // Not the best cooldown system but its basically the "cooldown" for the sweeper casting surge
            mob.initiateCastSpell(SpellRegistry.HEAT_SURGE_SPELL.get(), 1) //The last number at the end is a level, put the spell here
        }
    })
}


// Everything here is just adding goals and stuff, I should totally make this neater but ill do that Later tm
EntityJSEvents.addGoalSelectors("minecells:sweeper", event => {
    let entity = event.entity

    if (!entity.server) { return }

    event.customGoal(
        'goalflipBurn',
        0,
        mob => true,  
        mob => true,  
        true,
        mob => { },
        mob => { },
        true,
        mob => {
            try {
                flipBurn(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)
EntityJSEvents.addGoalSelectors('minecells:sweeper', event => {
    event.customGoal(
        'cast_and_burn',
        1,
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        true,
        mob => { },
        mob => { },
        true,
        mob => {
            try {
                global.castBurn(mob)
            } catch (error) {
                console.log(error)
            }
        }
    )
}
)

// This part is the casting based on conditions section


global.castBurn = 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 throw bomb on blocker.
    if (dist > 5 && dist < 15 && target.blocking) {
         if (age % 135 === 0) {
            mob.server.runCommandSilent(`particle cataclysm:trap_flame ${mob.x} ${mob.y} ${mob.z} 1 1 1 0 20 normal`)
            mob.server.scheduleInTicks(8, t => {
                mob.initiateCastSpell(SpellRegistry.MAGMA_BOMB_SPELL.get(), 1)
            })
          }
        }

        //This following line is the fiery teleport one. TLDR, run command to spawn particle, teleport, then spawn particle again.
     else if (dist > 15 && dist < 22) {
        if (age % 40 === 0) {
            mob.server.runCommandSilent(`particle cataclysm:trap_flame ${mob.x} ${mob.y} ${mob.z} 1 1 1 0 20 normal`)
            mob.server.scheduleInTicks(4,t => {
                mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 5)
            })
            mob.initiateCastSpell(SpellRegistry.TELEPORT_SPELL.get(), 5)
            mob.server.scheduleInTicks(8, t => { 
                mob.server.runCommandSilent(`particle cataclysm:trap_flame ${mob.x} ${mob.y} ${mob.z} 1 1 1 0 20 normal`)
                mob.server.runCommandSilent(`playsound cataclysm:flame_burst hostile @a ${mob.x} ${mob.y} ${mob.z}`)
            })
            
        }
        //This is set to Twenty because I think It's Funny when it blasts someone away into the stratosphere.
    } else if (dist > 1 && dist < 4) {
        if (age % 100 === 0) {
            mob.initiateCastSpell(SpellRegistry.GUST_SPELL.get(), 20) 
        }
    } else if (dist > 3 && dist < 50) {
        mob.getNavigation().moveTo(target, 1.0)
    }
}


