import { world,system} from "@minecraft/server"
import { BondlingLink } from "./runic_mobs";

function isFamily(family,entity) {
    return entity?.getComponent('minecraft:type_family')?.hasTypeFamily(family)
  }




world.afterEvents.entitySpawn.subscribe((entityEvent) => {

    const entity=entityEvent.entity
    try{
        BloodMoonMob(entity)
        HardVoidMob(entity)
    }catch(e){}
    
    if (entity.typeId == "edx:blazeblade2" || entity.typeId == "edx:soul_blazeblade2") {
        var arenas = entity.dimension.getEntities({
            location: { x: entity.location.x, y: entity.location.y, z: entity.location.z },
            type:  "edx:blazeblade_arena_manager" ,
            maxDistance: 32,
        });
        for (let arena of arenas)
        {
            arena.setDynamicProperty('id',entity.id)
            entity.setDynamicProperty('id',arena.id)
            return
        }
    }
    else if (entity.typeId == "edx:blazeblade" ) {
        const arena = entity.dimension.spawnEntity("edx:blazeblade_arena_manager",entity.location);
        arena.setDynamicProperty('id',entity.id)
        entity.setDynamicProperty('id',arena.id)
    }
    else if (entity.typeId == "edx:soul_blazeblade" ) {
        const arena = entity.dimension.spawnEntity("edx:blazeblade_arena_manager",entity.location);
        arena.setDynamicProperty('id',entity.id)
        arena.setProperty('edx:soul',true)
        entity.setDynamicProperty('id',arena.id)
    }
    else if (entity.typeId == "edx:vengeful_soul" ) {
        system.runTimeout(() => {
            entity.runCommand(`playanimation @s animation.hemoglobin_spirit.spawn`) 
        }, 1);
    }
    else if (entity?.typeId == "edx:burper" ) {
        if (isFamily("burper0",entity) ) 
            {
            entity.setDynamicProperty("mob_eat", 0)
        }
        else if ((isFamily("burper1",entity) )  )  
            {
            entity.setDynamicProperty("mob_eat", 1)
        }
        else if (isFamily("burper2",entity) )   {
            entity.setDynamicProperty("mob_eat", 3)
        }
    }
    else if(entity.typeId =="edx:bondling")
    {
        BondlingLink(entity)
    }
    else if (entity.typeId == "edx:blazeblade_fire_charge" ) {
        entity.applyImpulse({ x:entity.getViewDirection().x/2 , y: entity.getViewDirection().y/2, z: entity.getViewDirection().z/2 })
    }
    else if (entity.typeId == "minecraft:wither" ) {
        const night_time = world.getTimeOfDay() > 12000 && world.getTimeOfDay() < 24000;
        if(!night_time && entity.dimension.id == "minecraft:overworld") 
        {
            entity.runCommand(`tellraw @a[r=20] {"rawtext":[{"text":"message.event.wither_on_day"}]}`) 
            entity.runCommand(`give @p[r=30] skull 3 1`)
            entity.runCommand(`give @p[r=30] soul_sand 4`)
            entity.remove()
        }
        else if (entity.dimension.id == "minecraft:overworld" || entity.dimension.id == "minecraft:the_end")
        {
            entity.dimension.spawnEntity("edx:wither",entity.location);
            entity.remove()
        }
    }
    }

);

function BloodMoonMob(entity)
{
    if(world?.getDynamicProperty('blood_moon')==true && entity?.dimension?.id == "minecraft:overworld" && entity?.getComponent("minecraft:type_family")?.hasTypeFamily("blood_moon") && entity?.dimension?.getBlockFromRay(entity?.location,{x:0,y:1,z:0},{ maxDistance: 200,includePassableBlocks:false,includeLiquidBlocks:false})?.block==undefined)
    {
        entity.triggerEvent('blood_variant')
    }
}
function HardVoidMob(entity)
{
    if(world?.getDynamicProperty('hard_mode')==true && entity?.getComponent("minecraft:type_family")?.hasTypeFamily("hellmode"))
    {
        entity.triggerEvent('hellmode')
    }
    else if(world?.getDynamicProperty('void_mode')==true && entity?.getComponent("minecraft:type_family")?.hasTypeFamily("void_mode"))
    {
        entity.triggerEvent('void_mode')
    }
}