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

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

export function item_lore_event(player) {
  const loreLangMap = {
    "dungeon:titanium_axe": [
      
    ],
    "dungeon:titanium_sword": [],
    /*
    "dungeon:silver_sword": [
      "dungeon.langcode.knockback.lore.1"
    ],
    "dungeon:silver_axe": [
      "dungeon.langcode.knockback.lore.2"
    ],
    "dungeon:jade_sword": [
      "dungeon.langcode.knockback.lore.3"
    ],
    "dungeon:obsidian_sword": [
      "dungeon.langcode.knockback.lore.4"
    ],
    "dungeon:tungsten_sword": [
      "dungeon.langcode.knockback.lore.3"
    ],
    "dungeon:cobalt_sword": [
      "dungeon.langcode.knockback.lore.2"
    ],
    "dungeon:cobalt_axe": [
      "dungeon.langcode.knockback.lore.3"
    ],
    */
    "dungeon:bug_net": [
    ],

    "dungeon:wither_spike": [
    ],

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

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

  const set_bonus_0 = [];
  const set_bonus_1 = [];
  const silver_set_bonus = [];
  const inmunity_potion = [];

  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:silver_helmet", "dungeon:silver_chestplate", "dungeon:silver_leggings", "dungeon:silver_boots"
  ], silver_set_bonus);


  setLoreGroup(
    ["dungeon:cobalt_helmet", "dungeon:cobalt_chestplate", "dungeon:cobalt_leggings", "dungeon:cobalt_boots"],
    [
      "dungeon.langcode.cobalt_set_bonus.lore.0"    ]
  );

  setLoreGroup(
    ["dungeon:tungsten_helmet", "dungeon:tungsten_chestplate", "dungeon:tungsten_leggings", "dungeon:tungsten_boots"],
    [

      set_bonus_1[0],
    ]
  );

  setLoreGroup(
    ["dungeon:jade_helmet", "dungeon:jade_chestplate", "dungeon:jade_leggings", "dungeon:jade_boots"],
    [
      set_bonus_1[0],
    ]
  );

  setLoreGroup(
    ["dungeon:ancient_helmet", "dungeon:ancient_chestplate", "dungeon:ancient_leggings", "dungeon:ancient_boots"],
    [
      set_bonus_1[0],
    ]
  );

  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 ["chitin", "viritum"]) {
    setLoreGroup(
      [],
      [set_bonus_1[0]]
    );
  }

  // Titanium
  setLoreGroup(
    ["dungeon:titanium_helmet", "dungeon:titanium_chestplate", "dungeon:titanium_leggings", "dungeon:titanium_boots"],
    [
      set_bonus_1[0]

    ]
  );
  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);
    }
  }
}