import { ARMOR_EFFECT_STATS, SPECIAL_SWORD_FUNCTION, SPECIAL_BLASTER_SWORD } from "../data.js";

export function playerUseSpecialSword(player) {
  const equippable = player.getComponent("minecraft:equippable");
  const mainHand = equippable.getEquipment("Mainhand");

  if (!mainHand) return;

  if (SPECIAL_SWORD_FUNCTION[mainHand.typeId]) {
    SPECIAL_SWORD_FUNCTION[mainHand.typeId](player);
  }

  if (SPECIAL_BLASTER_SWORD.includes(mainHand.typeId)) {
    player.runCommand("/playsound 4ks_wp.blaster.shoot @a[r=8]");
  }
}

const SHIELD_RESTRICTED_WEAPONS = ["halberd", "greatsword"];

export async function playerHoldSpecialWeapon(player) {
  const equippable = player.getComponent("minecraft:equippable");
  const mainHand = equippable.getEquipment("Mainhand");

  // Handle base armor effects first
  handleArmorSpeedEffects(player);

  // If no item held, just remove weapon-specific effects
  if (!mainHand) {
    handleGlowstoneWeapon(player, mainHand);
    removeWeaponEffects(player);
    return;
  }

  const identifier = mainHand.typeId.replace("minecraft:", "").replace("4ks_wp:", "");
  const weaponType = identifier.split("_")[1];

  applyWeaponEffects(player, weaponType, mainHand);
  handleGlowstoneWeapon(player, mainHand);
  handleShieldRestriction(player, equippable, weaponType);
}

function handleArmorSpeedEffects(player) {
  if (player.hasTag("4ksdemonicarm")) {
    player.runCommand(`/effect @s speed ${ARMOR_EFFECT_STATS.demonic_armor_sword_speed_duration} 1 true`);
  } else if (player.hasTag("4ksdivinearm")) {
    player.runCommand(`/effect @s speed ${ARMOR_EFFECT_STATS.divine_armor_sword_speed_duration} 0 true`);
  }
}

function removeWeaponEffects(player) {
  // Remove weapon-specific tags
  const weaponTags = ["4ksspwp", "4kssgs", "4ksshb", "4ksscutlass", "4kssdragon"];
  let tagRemoved = false
  for (const tag of weaponTags) {
    if (player.hasTag(tag)) {
      player.removeTag(tag);
      tagRemoved = true
    }
  }

  // Remove weapon-specific effects
  if (tagRemoved) {

    if (!player.hasTag("4ksdemonicarm") && !player.hasTag("4ksdivinearm")) {
      player.runCommand("/effect @s speed 0 0 true");
    }
    player.runCommand("/effect @s slowness 0 0 true");
    player.runCommand("/effect @s strength 0 0 true");
  }

}

function applyWeaponEffects(player, weaponType, mainHand) {
  // Remove previous weapon effects first
  removeWeaponEffects(player);

  // Apply new weapon effects
  switch (weaponType) {
    case "dagger":
      if (!mainHand) return;

      const materialTiers = {
        tier1: ["wood", "stone", "gold",],
        tier2: ["iron", "copper", "steel", "amethyst", "lapis", "emerald"],
        tier3: ["diamond", "ruby", "sapphire", "platinum", "flame", "cryo", "obsidian", "sculk"],
        tier4: ["netherite", "dragon", "bedrock", "divine", "demon"]
      };

      const itemParts = mainHand.typeId.split(":");
      const itemId = itemParts[1] || itemParts[0];
      const material = itemId.split("_")[0];

      let speedLevel = 1;
      if (materialTiers.tier4.includes(material)) {
        speedLevel = 4;
      } else if (materialTiers.tier3.includes(material)) {
        speedLevel = 3;
      } else if (materialTiers.tier2.includes(material)) {
        speedLevel = 2;
      }

      player.runCommand(`/effect @s speed 999999 ${speedLevel - 1} true`);
      player.addTag("4ksspwp");
      break;
    case "cutlass":
      player.runCommand("/effect @s speed 999999 0 true");
      player.addTag("4ksscutlass");
      break;
    case "greatsword":
    case "halberd":
      player.runCommand("/effect @s slowness 999999 0 true");
      player.addTag(weaponType === "greatsword" ? "4kssgs" : "4ksshb");
      break;
  }
}

function handleShieldRestriction(player, equippable, weaponType) {
  if (SHIELD_RESTRICTED_WEAPONS.includes(weaponType)) {
    const offhand = equippable.getEquipment("Offhand");
    if (offhand?.typeId === "minecraft:shield") {
      equippable.setEquipment("Offhand", undefined);
      const inventory = player.getComponent("minecraft:inventory");
      inventory.container.addItem(offhand);
    }
  }
}

function handleGlowstoneWeapon(player, mainHand) {
  if (mainHand?.typeId.startsWith("4ks_wp:") && mainHand?.typeId.includes("glowstone")) {
    player.addTag("4kssglowstone");
  } else {
    player.removeTag("4kssglowstone");
    player.runCommand("/function 4ks/wp/headlight/off");
  }
}