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

import langData from "./dungeonlang.js"; // Diccionario de traducciones

export function item_lore_event(player) {
  const loreLangMap = {
    "dungeon:wither_spike": [
      "dungeon.langcode.wither_spike.lore.0",
      "dungeon.langcode.wither_spike.lore.1"
    ],

    "dungeon:flyswatter": [
      "dungeon.langcode.flyswatter.lore.0",
      "dungeon.langcode.flyswatter.lore.1"
    ],
    "dungeon:ancient_copper_sword": [
      "dungeon.langcode.ancient_copper_sword.lore.0"
    ],
    "dungeon:bone_pickaxe": [
      "dungeon.langcode.bone_pickaxe.lore.0"
    ],
    "dungeon:cursed_sickle": [
      "dungeon.langcode.cursed_sickle.lore.0",
      "dungeon.langcode.cursed_sickle.lore.1"
    ],
    "dungeon:glimmerite_hammer": [
      "dungeon.langcode.glimmerite_hammer.lore.0",
      "dungeon.langcode.glimmerite_hammer.lore.1"
    ],
    "dungeon:lunar_sword": [
      "dungeon.langcode.moon_sword.lore.0"
    ],
    "dungeon:spectrum_spike": [
      "dungeon.langcode.spectrum_spike.lore.0"
    ],
    "dungeon:spider_fang": [
      "dungeon.langcode.spider_fang.lore.0",
      "dungeon.langcode.spider_fang.lore.1"
    ],
    "dungeon:storm_sword": [
      "dungeon.langcode.storm_sword.lore.0"
    ],
    "dungeon:melon_redpie": [
      "dungeon.langcode.melon_redpie.lore.0"
    ],
    "dungeon:crystal_arrow": [
      "dungeon.langcode.crystal_arrow.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:feather_hat": [
      "dungeon.langcode.feather_hat.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:heart": [
      "dungeon.langcode.heart.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:heavy_rock": [
      "dungeon.langcode.heavy_rock.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:lightboots": [
      "dungeon.langcode.lightboots.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:miner_glove": [
      "dungeon.langcode.miner_glove.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:moonstone": [
      "dungeon.langcode.moonstone.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:silk_mask": [
      "dungeon.langcode.silk_mask.lore.0",
      "dungeon.langcode.silk_mask.lore.1",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:super_heart": [
      "dungeon.langcode.super_heart.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:totemofrecovery": [
      "dungeon.langcode.totemofrecovery.lore.0",
      "dungeon.langcode.totemofrecovery.lore.1",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:ultimate_heart": [
      "dungeon.langcode.ultimate_heart.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:vampiric_amulet": [
      "dungeon.langcode.vampiric_amulet.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:magic_seeds": [
      "dungeon.langcode.magic_seeds.lore.0"
    ],
    "dungeon:bandage": [
      "dungeon.langcode.bandage.lore.0"
    ],
    "dungeon:return_potion": [
      "dungeon.langcode.return_potion.lore.0"
    ],
    "dungeon:teleport_potion": [
      "dungeon.langcode.teleport_potion.lore.0"
    ],
    "dungeon:hunter_potion": [
      "dungeon.langcode.hunter_potion.lore.0"
    ],
    "dungeon:viritum_sword": [
      "dungeon.langcode.viritum_sword.lore.0"
    ],
    "dungeon:silver_ring": [
      "dungeon.langcode.silver_ring.lore.0",
      "dungeon.langcode.accessories.lore.0"
    ],
    "dungeon:super_bone_sword": [
      "dungeon.langcode.super_bone_sword.lore.0"
    ],
    "dungeon:bone_sword": [
      "dungeon.langcode.bone_sword.lore.0"
    ],
    "dungeon:bone_dagger": [
      "dungeon.langcode.bone_dagger.lore.0"
    ],
    "dungeon:bone_battleaxe": [
      "dungeon.langcode.bone_battleaxe.lore.0"
    ],
    "dungeon:bone_relic": [
      "dungeon.langcode.bone_relic.lore.0"
    ],
    "dungeon:gauntlet": [
      "dungeon.langcode.gauntlet.lore.0"
    ],
    "dungeon:petrifyn": [
      "dungeon.langcode.petrifyn.lore.0"
    ],
    "dungeon:punisher": [
      "dungeon.langcode.punisher.lore.0"
    ],
    "dungeon:cobalt_hammer": [
      "dungeon.langcode.cobalt_hammer.lore.0",
      "dungeon.langcode.cobalt_hammer.lore.1"
    ]
  };

  // Set Lore Groups por tipo
  const setLoreGroup = (items, keys) => {
    for (const id of items) {
      loreLangMap[id] = keys;
    }
  };

  const set_bonus_0 = ["dungeon.langcode.set_bonus.lore.0"];
  const set_bonus_1 = ["dungeon.langcode.set_bonus.lore.1"];
  const silver_set_bonus = ["dungeon.langcode.silver_set_bonus.lore.0"];
  const inmunity_potion = ["dungeon.langcode.inmunity_potion.lore.0"];

  setLoreGroup([
    "dungeon:mangrove_helmet", "dungeon:mangrove_chestplate", "dungeon:mangrove_leggings", "dungeon:mangrove_boots",
    "dungeon:jungle_helmet", "dungeon:jungle_chestplate", "dungeon:jungle_leggings", "dungeon:jungle_boots",
    "dungeon:oak_helmet", "dungeon:oak_chestplate", "dungeon:oak_leggings", "dungeon:oak_boots",
    "dungeon:spruce_helmet", "dungeon:spruce_chestplate", "dungeon:spruce_leggings", "dungeon:spruce_boots",
    "dungeon:darkoak_helmet", "dungeon:darkoak_chestplate", "dungeon:darkoak_leggings", "dungeon:darkoak_boots",
    "dungeon:birch_helmet", "dungeon:birch_chestplate", "dungeon:birch_leggings", "dungeon:birch_boots",
    "dungeon:cherry_helmet", "dungeon:cherry_chestplate", "dungeon:cherry_leggings", "dungeon:cherry_boots"
  ], set_bonus_0);

  setLoreGroup([
    "dungeon:cobalt_helmet", "dungeon:cobalt_chestplate", "dungeon:cobalt_leggings", "dungeon:cobalt_boots",
    "dungeon:tungsten_helmet", "dungeon:tungsten_chestplate", "dungeon:tungsten_leggings", "dungeon:tungsten_boots"
  ], set_bonus_1);

  setLoreGroup([
    "dungeon:silver_helmet", "dungeon:silver_chestplate", "dungeon:silver_leggings", "dungeon:silver_boots"
  ], silver_set_bonus);

  setLoreGroup([
    "dungeon:poison_inmunity", "dungeon:weakness_inmunity", "dungeon:slowness_inmunity", "dungeon:mining_fatigue_inmunity", "dungeon:wither_inmunity"
  ], inmunity_potion);

  //setLoreGroup([
    //"dungeon:silver_ring", "dungeon:totemofrecovery", "dungeon:vampiric_amulet", "dungeon:ultimate_heart", "dungeon:heart", "dungeon:super_heart", "dungeon:moonstone", "dungeon:miner_glove", "dungeon:lightboots", "dungeon:heavy_rock", "dungeon:silk_mask", "dungeon:feather_hat", "dungeon:crystal_arrow"
  //], accessories);


  // Armaduras combinadas
  const extraSetLore = (base, extra) => [
    ...set_bonus_1,
    `dungeon.langcode.${base}_set_bonus.lore.${extra}`
  ];

  const compositeSet = (base, suffixes, max) => {
    for (const suffix of suffixes) {
      setLoreGroup([
        `dungeon:${base}_helmet`,
        `dungeon:${base}_chestplate`,
        `dungeon:${base}_leggings`,
        `dungeon:${base}_boots`
      ], Array.from({ length: max }, (_, i) => `dungeon.langcode.${base}_set_bonus.lore.${i}`).unshift(set_bonus_1[0]));
    }
  };

  // Jade, Chitin, Viritum, Glimmerite
  for (const mat of ["jade", "chitin", "viritum", "glimmerite"]) {
    setLoreGroup(
      [`dungeon:${mat}_helmet`, `dungeon:${mat}_chestplate`, `dungeon:${mat}_leggings`, `dungeon:${mat}_boots`],
      [set_bonus_1[0], `dungeon.langcode.${mat}_set_bonus.lore.0`]
    );
  }

  // Titanium
  setLoreGroup(
    ["dungeon:titanium_helmet", "dungeon:titanium_chestplate", "dungeon:titanium_leggings", "dungeon:titanium_boots"],
    [
      set_bonus_1[0],
      "dungeon.langcode.titanium_set_bonus.lore.0",
      "dungeon.langcode.titanium_set_bonus.lore.1"
    ]
  );

  setLoreGroup(
    ["dungeon:titanium_helmet", "dungeon:titanium_chestplate", "dungeon:titanium_leggings", "dungeon:titanium_boots"],
    [
      set_bonus_1[0],
      "dungeon.langcode.titanium_set_bonus.lore.0",
      "dungeon.langcode.titanium_set_bonus.lore.1"
    ]
  );
  const TAG_TO_LANG = {
    "en_US": "lang_en_US",
    "es_ES": "lang_es_ES",
    "es_MX": "lang_es_MX",
    "pt_BR": "lang_pt_BR",
    "pt_PT": "lang_pt_PT"
  };
  // Mapa de etiquetas válidas a idiomas
 // 1. Determinar idioma del jugador según sus tags
 const playerTags = player.getTags();
 const playerLanguage = Object
   .keys(TAG_TO_LANG)
   .find(tag => playerTags.includes(tag)) 
   || "en_US";

 // 2. Obtener inventario
 const inventoryComp = player.getComponent("inventory");
 if (!inventoryComp) return;
 const inventory = inventoryComp.container;
 if (!inventory) return;

 // 3. Recorrer cada slot
 for (let slot = 0; slot < inventory.size; slot++) {
   const item = inventory.getItem(slot);
   if (!item) continue;

   // 4. Obtener las claves de lore para este ítem
   const loreKeys = loreLangMap[item.typeId];
   if (!loreKeys) continue;

   // 5. Construir el nuevo lore traducido
   const newLore = loreKeys.map(key => langData[playerLanguage]?.[key] || key);
   const currentLore = item.getLore();

   // 6. Comparación ligera (longitud + elemento a elemento)
   let changed = false;
   if (currentLore.length !== newLore.length) {
     changed = true;
   } else {
     for (let i = 0; i < newLore.length; i++) {
       if (currentLore[i] !== newLore[i]) {
         changed = true;
         break;
       }
     }
   }

   // 7. Si cambió, actualiza una sola vez
   if (changed) {
     item.setLore(newLore);
     inventory.setItem(slot, item);
   }
 }
}