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

const maskEffects = {
  "hf_mzs:mask_bliss": { id: 0, effect: "jump_boost" },
  "hf_mzs:mask_faith": { id: 1, effect: "health_boost" },
  "hf_mzs:mask_fear": { id: 2, effect: "speed" },
  "hf_mzs:mask_fury": { id: 3, effect: "strength" },
  "hf_mzs:mask_misery": { id: 4, effect: "resistance" },
  "hf_mzs:mask_rage": { id: 5, effect: "haste" },
};

export function umvuthanaMaskEffects(player) {
  const item = player.getComponent("equippable").getEquipment("Head");
  if (item === undefined || !item.hasTag("hf_mzs:mask")) return;
  player.addEffect(maskEffects[item.typeId].effect, 25, {
    amplifier: 0,
    showParticles: false,
  });
}
export function umvuthanaBeforeUse(event) {
  const helmet = event.source.getComponent("equippable").getEquipment("Head");
  if (helmet === undefined || helmet.typeId !== "hf_mzs:sol_visage") return;

  const item = event.itemStack;
  if (item === undefined || !item.hasTag("hf_mzs:mask")) return;

  if (item.getDynamicProperty("hf_mzs:entity") === undefined) {
    if (item.typeId === "hf_mzs:mask_faith") item.setDynamicProperty("hf_mzs:entity", "hf_mzs:umvuthana_crane");
    else if (item.typeId === "hf_mzs:mask_fury") item.setDynamicProperty("hf_mzs:entity", "hf_mzs:umvuthana_raptor");
    else item.setDynamicProperty("hf_mzs:entity", "hf_mzs:umvuthana");
  }
  mc.system.run(() => {
    let count = 0;
    event.source.dimension
      .getEntities({
        location: event.source.location,
        maxDistance: 48,
        families: ["hf_mzs_umvuthana"],
        excludeFamilies: ["hf_mzs_umvuthi"],
      })
      .forEach((element) => {
        if (
          element.isValid() &&
          element.getProperty("hf_mzs:tamed") &&
          element.getComponent("tameable").tamedToPlayerId === event.source.id
        )
          count++;
      });
    if (count >= 10) {
      event.source.runCommand('/tellraw @s {"rawtext":[{"translate":"message.hf_mzs.sol_visage_limit"}]}');
      return;
    }
    event.source.runCommand("/playsound hf_mzs:mob.umvuthi.drum @a[r=16] ~~~");
    const entity = event.source.dimension.spawnEntity(item.getDynamicProperty("hf_mzs:entity"), event.source.location);
    entity.triggerEvent("tamed");
    if (item.nameTag !== undefined) entity.nameTag = item.nameTag;

    //entity.runCommand(`/replaceitem entity @s slot.armor.head 0 ${item.typeId} 1 0`);
    const enchants = item.getComponent("enchantable").getEnchantments();
    for (const enchant of enchants) {
      entity.setProperty("hf_mzs:enchanted", true);
      entity.setDynamicProperty("hf_mzs:" + enchant.type.id, enchant.level);
    }

    const durability = 55 - item.getComponent("minecraft:durability").damage;
    const health = /*entity.getComponent("minecraft:health").effectiveMax*/ 20 * (durability / 55);

    entity.getComponent("minecraft:health").setCurrentValue(health);
    //mc.world.sendMessage(`${health}`);

    entity.triggerEvent("spawn");
    entity.getComponent("tameable").tame(event.source);
    entity.setProperty("hf_mzs:tamed", true);
    entity.setProperty("hf_mzs:mask", maskEffects[item.typeId].id);
    entity.setProperty("hf_mzs:y_rot", event.source.getRotation().y);
    if (event.source.getGameMode() === "survival" || event.source.getGameMode() === "adventure")
      event.source.getComponent("equippable").setEquipment("Mainhand");
    entity.applyImpulse(vector.multiply(event.source.getViewDirection(), 1.2));
  });
  event.cancel = true;
}
