import { world, system, EquipmentSlot, EffectTypes } from "@minecraft/server";

import { woodSet } from "./wood.js";
import { jadeSet } from "./jade_2.js";
import { glimmeriteSet } from "./glimmerite_2.js";
import { viritumSet } from "./viritum_2.js";
import { chitinSet } from "./chitin.js";
import { tagsOnlySets } from "./tags.js";
import { vanillaArmorSets } from "./vanilla.js";
import { weaponCombos } from "./weapons.js";
import { silverAura } from "./silver_aura.js";
import { titaniumEffects } from "./titanium_aura.js";

export const INTERVAL = 20;
export const BUFF_DUR = 60;

export let E = null;

function initEffects() {
  if (E) return;

  E = {
    poison: EffectTypes.get("minecraft:poison"),
    wither: EffectTypes.get("minecraft:wither"),
    fatalPoison: EffectTypes.get("minecraft:fatal_poison"),
    blindness: EffectTypes.get("minecraft:blindness"),
    resistance: EffectTypes.get("minecraft:resistance"),
    healthBoost: EffectTypes.get("minecraft:health_boost"),
    regeneration: EffectTypes.get("minecraft:regeneration"),
    strength: EffectTypes.get("minecraft:strength"),
    haste: EffectTypes.get("minecraft:haste"),
    slowness: EffectTypes.get("minecraft:slowness"),
    miningFatigue: EffectTypes.get("minecraft:mining_fatigue"),
    speed: EffectTypes.get("minecraft:speed"),
  };
}


// Helpers compartidos
export function getEq(player) {
  return player.getComponent("minecraft:equippable");
}
export function idOf(itemStack) {
  return itemStack?.typeId ?? null;
}
export function safeAddEffect(player, effectType, amplifier = 0, duration = BUFF_DUR) {
  if (!effectType) return;
  player.addEffect(effectType, duration, { amplifier, showParticles: false });
}

export function safeRemoveEffect(player, effectType) {
  if (!effectType) return;
  if (player.getEffect(effectType)) player.removeEffect(effectType);
}

export function setTag(player, tag, enabled) {
  if (enabled) {
    if (!player.hasTag(tag)) player.addTag(tag);
  } else {
    if (player.hasTag(tag)) player.removeTag(tag);
  }
}

// Sets de armadura (IDs)
export const ARMOR_SETS = {
   ancient: {
    [EquipmentSlot.Head]: "dungeon:ancient_helmet",
    [EquipmentSlot.Chest]: "dungeon:ancient_chestplate",
    [EquipmentSlot.Legs]: "dungeon:ancient_leggings",
    [EquipmentSlot.Feet]: "dungeon:ancient_boots",
  },
  oak: {
    [EquipmentSlot.Head]: "dungeon:oak_helmet",
    [EquipmentSlot.Chest]: "dungeon:oak_chestplate",
    [EquipmentSlot.Legs]: "dungeon:oak_leggings",
    [EquipmentSlot.Feet]: "dungeon:oak_boots",
  },
  birch: {
    [EquipmentSlot.Head]: "dungeon:birch_helmet",
    [EquipmentSlot.Chest]: "dungeon:birch_chestplate",
    [EquipmentSlot.Legs]: "dungeon:birch_leggings",
    [EquipmentSlot.Feet]: "dungeon:birch_boots",
  },
  cherry: {
    [EquipmentSlot.Head]: "dungeon:cherry_helmet",
    [EquipmentSlot.Chest]: "dungeon:cherry_chestplate",
    [EquipmentSlot.Legs]: "dungeon:cherry_leggings",
    [EquipmentSlot.Feet]: "dungeon:cherry_boots",
  },
  spruce: {
    [EquipmentSlot.Head]: "dungeon:spruce_helmet",
    [EquipmentSlot.Chest]: "dungeon:spruce_chestplate",
    [EquipmentSlot.Legs]: "dungeon:spruce_leggings",
    [EquipmentSlot.Feet]: "dungeon:spruce_boots",
  },
  jungle: {
    [EquipmentSlot.Head]: "dungeon:jungle_helmet",
    [EquipmentSlot.Chest]: "dungeon:jungle_chestplate",
    [EquipmentSlot.Legs]: "dungeon:jungle_leggings",
    [EquipmentSlot.Feet]: "dungeon:jungle_boots",
  },
  mangrove: {
    [EquipmentSlot.Head]: "dungeon:mangrove_helmet",
    [EquipmentSlot.Chest]: "dungeon:mangrove_chestplate",
    [EquipmentSlot.Legs]: "dungeon:mangrove_leggings",
    [EquipmentSlot.Feet]: "dungeon:mangrove_boots",
  },
  darkoak: {
    [EquipmentSlot.Head]: "dungeon:darkoak_helmet",
    [EquipmentSlot.Chest]: "dungeon:darkoak_chestplate",
    [EquipmentSlot.Legs]: "dungeon:darkoak_leggings",
    [EquipmentSlot.Feet]: "dungeon:darkoak_boots",
  },

  jade: {
    [EquipmentSlot.Head]: "dungeon:jade_helmet",
    [EquipmentSlot.Chest]: "dungeon:jade_chestplate",
    [EquipmentSlot.Legs]: "dungeon:jade_leggings",
    [EquipmentSlot.Feet]: "dungeon:jade_boots",
  },
  glimmerite: {
    [EquipmentSlot.Head]: "dungeon:glimmerite_helmet",
    [EquipmentSlot.Chest]: "dungeon:glimmerite_chestplate",
    [EquipmentSlot.Legs]: "dungeon:glimmerite_leggings",
    [EquipmentSlot.Feet]: "dungeon:glimmerite_boots",
  },
  viritum: {
    [EquipmentSlot.Head]: "dungeon:viritum_helmet",
    [EquipmentSlot.Chest]: "dungeon:viritum_chestplate",
    [EquipmentSlot.Legs]: "dungeon:viritum_leggings",
    [EquipmentSlot.Feet]: "dungeon:viritum_boots",
  },
  chitin: {
    [EquipmentSlot.Head]: "dungeon:chitin_helmet",
    [EquipmentSlot.Chest]: "dungeon:chitin_chestplate",
    [EquipmentSlot.Legs]: "dungeon:chitin_leggings",
    [EquipmentSlot.Feet]: "dungeon:chitin_boots",
  },
  cobalt: {
    [EquipmentSlot.Head]: "dungeon:cobalt_helmet",
    [EquipmentSlot.Chest]: "dungeon:cobalt_chestplate",
    [EquipmentSlot.Legs]: "dungeon:cobalt_leggings",
    [EquipmentSlot.Feet]: "dungeon:cobalt_boots",
  },
  tungsten: {
    [EquipmentSlot.Head]: "dungeon:tungsten_helmet",
    [EquipmentSlot.Chest]: "dungeon:tungsten_chestplate",
    [EquipmentSlot.Legs]: "dungeon:tungsten_leggings",
    [EquipmentSlot.Feet]: "dungeon:tungsten_boots",
  },
  titanium: {
    [EquipmentSlot.Head]: "dungeon:titanium_helmet",
    [EquipmentSlot.Chest]: "dungeon:titanium_chestplate",
    [EquipmentSlot.Legs]: "dungeon:titanium_leggings",
    [EquipmentSlot.Feet]: "dungeon:titanium_boots",
  },

  diamond: {
    [EquipmentSlot.Head]: "minecraft:diamond_helmet",
    [EquipmentSlot.Chest]: "minecraft:diamond_chestplate",
    [EquipmentSlot.Legs]: "minecraft:diamond_leggings",
    [EquipmentSlot.Feet]: "minecraft:diamond_boots",
  },
  netherite: {
    [EquipmentSlot.Head]: "minecraft:netherite_helmet",
    [EquipmentSlot.Chest]: "minecraft:netherite_chestplate",
    [EquipmentSlot.Legs]: "minecraft:netherite_leggings",
    [EquipmentSlot.Feet]: "minecraft:netherite_boots",
  },
  iron: {
    [EquipmentSlot.Head]: "minecraft:iron_helmet",
    [EquipmentSlot.Chest]: "minecraft:iron_chestplate",
    [EquipmentSlot.Legs]: "minecraft:iron_leggings",
    [EquipmentSlot.Feet]: "minecraft:iron_boots",
  },
  gold: {
    [EquipmentSlot.Head]: "minecraft:golden_helmet",
    [EquipmentSlot.Chest]: "minecraft:golden_chestplate",
    [EquipmentSlot.Legs]: "minecraft:golden_leggings",
    [EquipmentSlot.Feet]: "minecraft:golden_boots",
  },
  chainmail: {
    [EquipmentSlot.Head]: "minecraft:chainmail_helmet",
    [EquipmentSlot.Chest]: "minecraft:chainmail_chestplate",
    [EquipmentSlot.Legs]: "minecraft:chainmail_leggings",
    [EquipmentSlot.Feet]: "minecraft:chainmail_boots",
  },
  copper: {
    [EquipmentSlot.Head]: "minecraft:copper_helmet",
    [EquipmentSlot.Chest]: "minecraft:copper_chestplate",
    [EquipmentSlot.Legs]: "minecraft:copper_leggings",
    [EquipmentSlot.Feet]: "minecraft:copper_boots",
  },
  silver: {
    [EquipmentSlot.Head]: "dungeon:silver_helmet",
    [EquipmentSlot.Chest]: "dungeon:silver_chestplate",
    [EquipmentSlot.Legs]: "dungeon:silver_leggings",
    [EquipmentSlot.Feet]: "dungeon:silver_boots",
  },
};

export function hasSet(eqIds, setDef) {
  for (const slotKey of Object.keys(setDef)) {
    if (eqIds[slotKey] !== setDef[slotKey]) return false;
  }
  return true;
}

export function getEquipmentIds(player) {
  const eq = getEq(player);
  if (!eq) return null;

  return {
    [EquipmentSlot.Head]: idOf(eq.getEquipment(EquipmentSlot.Head)),
    [EquipmentSlot.Chest]: idOf(eq.getEquipment(EquipmentSlot.Chest)),
    [EquipmentSlot.Legs]: idOf(eq.getEquipment(EquipmentSlot.Legs)),
    [EquipmentSlot.Feet]: idOf(eq.getEquipment(EquipmentSlot.Feet)),
    [EquipmentSlot.Mainhand]: idOf(eq.getEquipment(EquipmentSlot.Mainhand)),
    [EquipmentSlot.Offhand]: idOf(eq.getEquipment(EquipmentSlot.Offhand)),
  };
}

// 1 SOLO loop
system.beforeEvents.startup.subscribe(() => {
  system.run(() => {
    initEffects();

    system.runInterval(() => {
      if (!E) return;

      for (const p of world.getAllPlayers()) {
        const eqIds = getEquipmentIds(p);
        if (!eqIds) continue;

        woodSet(p, eqIds);
        jadeSet(p, eqIds);
        glimmeriteSet(p, eqIds);
        viritumSet(p, eqIds);
        chitinSet(p, eqIds);

        silverAura(p, eqIds);
        titaniumEffects(p, eqIds);

        tagsOnlySets(p, eqIds);
        vanillaArmorSets(p, eqIds);
        weaponCombos(p, eqIds);
      }
    }, INTERVAL);
  });
});
