import * as mc from "@minecraft/server";
import * as vector from "../utils/vector";

export function eventBehavior(event) {
    if (event.eventId === 'minecraft:entity_spawned') {
        if (event.entity.location.y < 3) {
            event.entity.setProperty("hf_mzs:deepslate", true);
            /*
            let perfect = true
            for (let i = 1; i <= 7; i++) {
                const isPink = (Math.random()*65 < Math.abs(event.entity.location.y));
                event.entity.setProperty(`hf_mzs:pink${i}`, isPink);

                if (perfect === true)
                    perfect = isPink;
            }
            if (perfect === true)
                event.entity.setDynamicProperty("hf_mzs:perfect", true)
            */
        }
        return;
    }
    if (event.eventId === "register") {
      event.entity.setDynamicProperty('hf_mzs:ding_cooldown', 0);
      return;
    }
    if (event.eventId === 'death_no_drop') {
        event.entity.setProperty("hf_mzs:no_drop", true);
        event.entity.setProperty("hf_mzs:death", true);
        //mc.world.sendMessage("n");
    }
    if (event.eventId === 'drop') {
        if (!event.entity.getProperty("hf_mzs:no_drop")) {
            const item = new mc.ItemStack("minecraft:diamond", event.entity.getDynamicProperty("hf_mzs:drop_amount"));
            event.entity.dimension.spawnItem(item, event.entity.location);
        }
        event.entity.remove();
        return;
    }
    if (event.eventId === 'attempt_flee') {
        const block = event.entity.dimension.getBlockFromRay(event.entity.location, vector.down, {
            includeTypes: [
                "minecraft:grass_block", 
                "minecraft:dirt", 
                "minecraft:stone", 
                "minecraft:deepslate"
            ],
            maxDistance: 1.5
        });
        if (block === undefined)
            return;
        event.entity.triggerEvent("hide");
        return;
    }
}
export function hurtBehavior(event) {
    //mc.world.sendMessage(`${event.damageSource.damagingEntity === undefined}   ${!event.damageSource.damagingEntity.isValid()}   ${event.damageSource.damagingEntity.typeId !== "minecraft:player"}`);
    if (event.damageSource.damagingEntity === undefined || 
        !event.damageSource.damagingEntity.isValid() || 
        event.damageSource.damagingEntity.typeId !== "minecraft:player"
    )
        return;
    const weapon = event.damageSource.damagingEntity.getComponent("equippable")?.
            getEquipment("Mainhand");
            
    //mc.world.sendMessage(`${event.damageSource.damagingEntity.typeId}`);
    if (weapon?.getComponent("enchantable").hasEnchantment("silk_touch")) {
        // perfect pink
        /*
        if (event.hurtEntity.getDynamicProperty("hf_mzs:perfect")) {
            event.hurtEntity.runCommand("/particle hf_mzs:grottol_break_pink ~~~");
            event.hurtEntity.runCommand("/playsound dig.deepslate @a[r=16] ~~~ 2");

            const item = new mc.ItemStack("hf_mzs:captured_grottol_pink");
            for (let i = 1; i <= 7; i++) {
                item.setDynamicProperty(`hf_mzs:pink${i}`, true);
            }

            event.hurtEntity.dimension.spawnItem(item, event.hurtEntity.location);
        }
        */
        // regular
        if (!event.hurtEntity.getProperty("hf_mzs:deepslate")) {
            event.hurtEntity.runCommand("/particle hf_mzs:grottol_break ~~~");
            event.hurtEntity.runCommand("/playsound dig.stone @a[r=16] ~~~ 5");

            const item = new mc.ItemStack("hf_mzs:captured_grottol");

            event.hurtEntity.dimension.spawnItem(item, event.hurtEntity.location);
        }
        else {
            event.hurtEntity.runCommand("/particle hf_mzs:grottol_break ~~~");
            event.hurtEntity.runCommand("/playsound dig.deepslate @a[r=16] ~~~ 2");

            const item = new mc.ItemStack("hf_mzs:captured_grottol_deep");

            event.hurtEntity.dimension.spawnItem(item, event.hurtEntity.location);
        }
        // mixed
        /*
        else {
            event.hurtEntity.runCommand("/particle hf_mzs:grottol_break ~~~");
            event.hurtEntity.runCommand("/playsound dig.stone @a[r=16] ~~~ 2");

            const item = new mc.ItemStack("hf_mzs:captured_grottol_mixed");
            for (let i = 1; i <= 7; i++) {
                //mc.world.sendMessage(`${event.hurtEntity.getProperty(`hf_mzs:pink${i}`)}`);
                item.setDynamicProperty(`hf_mzs:pink${i}`, event.hurtEntity.getProperty(`hf_mzs:pink${i}`));
            }

            event.hurtEntity.dimension.spawnItem(item, event.hurtEntity.location);
        }
        */
        event.hurtEntity.remove();
        return;
    }
    
    //mc.world.sendMessage(`${event.damageSource.damagingEntity.typeId}`);
    event.hurtEntity.setProperty("hf_mzs:death", true);
    if (weapon?.getComponent("enchantable").hasEnchantment("fortune"))
        event.hurtEntity.setDynamicProperty("hf_mzs:drop_amount", 
                Math.ceil(weapon.getComponent("enchantable").getEnchantment("fortune")?.level/2 + 1));
    else
        event.hurtEntity.setDynamicProperty("hf_mzs:drop_amount", 1);

    event.hurtEntity.triggerEvent("death");
    return;
}