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

const masks = [
    'hf_mzs:mask_bliss',
    'hf_mzs:mask_faith',
    'hf_mzs:mask_fear',
    'hf_mzs:mask_fury',
    'hf_mzs:mask_misery',
    'hf_mzs:mask_rage'
]
const maskEffects = {
    0: 'jump_boost',
    1: 'health_boost',
    2: 'speed',
    3: 'strength',
    4: 'resistance',
    5: 'haste'
}
const allEnchantments = [
    "aqua_affinity",
    "blast_protection",
    "curse_of_binding",
    "curse_of_vanishing",
    "fire_protection",
    "mending",
    "projectile_protection",
    "protection",
    "respiration",
    "thorns",
    "unbreaking"
]

export function eventBehavior(event) {
    //if (event.eventId !== "reenforce_movement" && event.eventId !== "can_check" && event.eventId !== "no_creative")
    //    mc.world.sendMessage(event.entity.typeId+':    '+event.eventId+'');
    if (event.eventId === 'register') {
        event.entity.addEffect(maskEffects[event.entity.getProperty("hf_mzs:mask")], 9999, {
            amplifier: 0,
            showParticles: false
        });
        return;
    }
    if (event.eventId === 'to_attack') {
        event.entity.addEffect("speed", 20, {
            amplifier: 1,
            showParticles: false
        });
        return;
    }
    if (event.eventId === 'drop') {
        dropMask(event);
        return;
    }
    if (event.eventId === 'is_attacking') {
        selectors.union({
            location: event.entity.location,
            maxDistance: 16,
            excludeFamilies: ['hf_mzs_umvuthana_crane', 'hf_mzs_umvuthi']
        }, ['hf_mzs_umvuthana'], event.entity.dimension, event.entity.id)
            .forEach((target) => {
                if (target.isValid() && !target.getProperty("hf_mzs:tamed") &&
                    !target.getProperty("hf_mzs:death") && !target.getProperty("hf_mzs:spawn") &&
                    !target.getProperty("hf_mzs:counter") && !target.getProperty("hf_mzs:turning_into_mask") &&
                    !target.getProperty("hf_mzs:roaring")
                ) {
                    target.triggerEvent("cooldown");
                }
            });
        if (event.entity.getComponent("minecraft:health").currentValue > 4)
            return;
        event.entity.addEffect("speed", 40, {
            amplifier: 1,
            showParticles: false
        });
        return;
    }
    if (event.eventId === 'alert') {
        event.entity.runCommand("/tp @s ~~~ facing @p[r=12]");
        event.entity.addEffect("slowness", 14, {
            amplifier: 100,
            showParticles: false
        });
        return;
    }
    if (event.eventId === 'roar') {
        event.entity.runCommand("/tp @s ~~~ facing @p[r=12]");
        event.entity.addEffect("slowness", 20, {
            amplifier: 100,
            showParticles: false
        });
        return;
    }
    if (event.eventId === 'turned_into_mask') {
        dropMaskTamed(event.entity);
        return;
    }
    // make it despawn because very rarely it fails to for some reason
    if (event.eventId === 'death') {
        mc.system.runTimeout(()=>{
            if (!event.entity.isValid())
                return;
            event.entity.triggerEvent("drop");
        }, 60)
        return;
    }
}
function dropMask(event) {
    if (event.entity.getProperty("hf_mzs:tamed") === true || Math.random() < 0.7)
        return;
    const item = new mc.ItemStack(masks[event.entity.getProperty('hf_mzs:mask')]);
    item.setDynamicProperty('hf_mzs:entity', event.entity.typeId)
    event.entity.dimension.spawnItem(item, event.entity.location);
}
function dropMaskTamed(entity) {
    const item = new mc.ItemStack(masks[entity.getProperty('hf_mzs:mask')]);
    item.setDynamicProperty('hf_mzs:entity', entity.typeId)
    if (entity.nameTag !== undefined)
        item.nameTag = entity.nameTag;
    for (const enchantment of allEnchantments) {
        if (entity.getDynamicProperty("hf_mzs:" + enchantment) !== undefined)
            item.getComponent("enchantable").addEnchantment({
                type: new mc.EnchantmentType(enchantment),
                level: entity.getDynamicProperty("hf_mzs:" + enchantment)
            });
    }

    const health = 55 - (entity.getComponent("minecraft:health").currentValue / 20/*entity.getComponent("minecraft:health").effectiveMax*/) * 54;

    item.getComponent("minecraft:durability").damage = Math.min(health + 5, 54);

    entity.dimension.spawnItem(item, vector.add(entity.location, { x: 0, y: 1.5, z: 0 }));
    entity.remove();
}