// 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)
            }
        }
    )
}
)
EntityJSEvents.addGoalSelectors('minecells:sweeper', event => {
    event.customGoal(
        'throw_bomb',
        1,
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        mob => mob.target != null && mob.hasLineOfSight(mob.target),
        true,
        mob => { },
        mob => { },
        true,
        mob => {
            try {
                global.bombthrow(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)
    }
}



EntityEvents.hurt('minecells:sweeper', event => {
    const player = event.source.player
    const mob = event.entity
    
    if (!player) return
    mob.server.runCommandSilent(`playsound minecraft:entity.illusioner.hurt hostile @a ${mob.x} ${mob.y} ${mob.z} 1 0.8`)
    mob.server.runCommandSilent(`particle minecells:fly ${mob.x} ${mob.y} ${mob.z} 1 1 1 0.2 60 normal`)
    mob.server.scheduleInTicks(20,t => {
    mob.server.runCommandSilent(`summon minecells:kamikaze ${mob.x} ${mob.y + 3} ${mob.z}`)
    mob.server.runCommandSilent(`particle cataclysm:rain_cloud ${mob.x} ${mob.y + 3} ${mob.z} 1 1 1 0.1 8 normal`)
    mob.server.runCommandSilent(`playsound minecells:fly.charge hostile @a ${mob.x} ${mob.y} ${mob.z}`)
    mob.server.runCommandSilent(`playsound minecells:fly.charge hostile @a ${mob.x} ${mob.y} ${mob.z}`)

 

}
)
})

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

//     let dist = target.distanceToEntity(mob)
//     let basePos = new BlockPos(    
//     entity.mob.x,
//     entity.mob.y,
//     entity.mob.z
// )
// let offsetPos = basePos.offset(
//     0, 5, 0
// )
// let zom = e.level.createEntity("minecraft:zombie")

//     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) {
// zom.setPos(offsetPos)
// zom.spawn()
//           }
//         }
//     }
