import { system } from "@minecraft/server";
import { damageItemDurability, decreaseItemStack } from "../../ntk_functions.js";
import { unknownMagicSpell } from "./unknown_scroll_spells.js";
import {
  clearSkyMagicSpell,
  rainMagicSpell,
  thunderstormMagicSpell,
  hoardingMagicSpell1,
  hoardingMagicSpell2,
  hoardingMagicSpell3,
  homewardBoundMagicSpell
} from "./magic_scroll_spells.js";

const ItemWandOnUseComponent = {
  onUse({ source, itemStack }, { params }) {
    for (const child of params) {
      const eventChance = child.event_chance ?? 100.0;
      const itemName = child.item_name;
      const itemSlot = child.item_slot ?? itemSlot;
      const ammunitionType = child.ammunition_type ?? false ;
      const ammunitionSlot = child.ammunition_slot ?? "Offhand";
      const ammunitionFailGuide = child.ammunition_fail_guide ?? false;
      if (Math.random() * 100 <= eventChance) {
        if (itemStack.typeId === itemName) {
          const sourceEquippable = source.getComponent("equippable");
          const ammunitionItemSlot = sourceEquippable.getEquipmentSlot(ammunitionSlot);
          const ammunitionItemStack = ammunitionItemSlot.getItem();
          if (ammunitionType !== false) {
            if (ammunitionItemStack === undefined || !ammunitionItemStack.hasTag(ammunitionType)) {
              if (ammunitionFailGuide !== false) {
                source.runCommand(`tellraw @p {\"rawtext\":[{\"text\":\"§e${ammunitionFailGuide}§r\"}]}`);
                source.dimension.playSound("game.player.attack.nodamage", source.location, { pitch: 1.2, volume: 1.0 });
              }
            }
          }
        }
      }
    }
  },
};
system.beforeEvents.startup.subscribe(({ itemComponentRegistry }) => {
  itemComponentRegistry.registerCustomComponent(
    "ntk:wand_on_use",
    ItemWandOnUseComponent
  );
});

const ItemWandOnCompleteUseComponent = {
  onCompleteUse({ source, itemStack }, { params }) {
    for (const child of params) {
      const eventChance = child.event_chance ?? 100.0;
      const itemName = child.item_name;
      const itemSlot = child.item_slot ?? itemSlot;
      const ammunitionType = child.ammunition_type ?? false ;
      const ammunitionSlot = child.ammunition_slot ?? "Offhand";
      const ammunitionFailGuide = child.ammunition_fail_guide ?? false;
      const ammunitionSoundEffect = child.ammunition_sound_effect ?? false;
      const ammunitionSoundPitchMin = child.ammunition_sound_pitch_min ?? 1.0;
      const ammunitionSoundPitchMax = child.ammunition_sound_pitch_max ?? 1.0;
      const ammunitionSoundVolume = child.ammunition_sound_volume ?? 1.0;
      const particleEffect = child.particle_effect ?? false;
      const particleOffsetX = child.particle_offset_x ?? 0.0;
      const particleOffsetY = child.particle_offset_y ?? 0.0;
      const particleOffsetZ = child.particle_offset_z ?? 0.0;
      const soundEffect = child.sound_effect ?? false;
      const soundPitchMin = child.sound_pitch_min ?? 1.0;
      const soundPitchMax = child.sound_pitch_max ?? 1.0;
      const soundVolume = child.sound_volume ?? 1.0;
      if (Math.random() * 100 <= eventChance) {
        if (itemStack.typeId === itemName) {
          const soundPitch = Math.random() * (soundPitchMax - soundPitchMin) + soundPitchMin;
          const sourceEquippable = source.getComponent("equippable");
          const ammunitionItemSlot = sourceEquippable.getEquipmentSlot(ammunitionSlot);
          const ammunitionItemStack = ammunitionItemSlot.getItem();
          const ammunitionSoundPitch = Math.random() * (ammunitionSoundPitchMax - ammunitionSoundPitchMin) + ammunitionSoundPitchMin;
          if (ammunitionItemStack === undefined || !ammunitionItemStack.hasTag(ammunitionType)) {
            if (particleEffect !== false) {
              source.dimension.spawnParticle(particleEffect, { x: source.location.x + particleOffsetX, y: source.location.y + particleOffsetY, z: source.location.z + particleOffsetZ });
            }
            if (soundEffect !== false) {
              source.dimension.playSound(soundEffect, source.location, { pitch: soundPitch, volume: soundVolume });
            }
          }
          else if (ammunitionItemStack.hasTag(ammunitionType)) {
            if (ammunitionItemStack.hasTag("ntk:instant_magic_scroll")) {
              if (ammunitionItemStack.typeId === "ntk:scroll_of_clear_sky") {
                clearSkyMagicSpell(source);
                decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                damageItemDurability(source, itemStack, 1, itemSlot);
              }
              if (ammunitionItemStack.typeId === "ntk:scroll_of_rain") {
                rainMagicSpell(source);
                decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                damageItemDurability(source, itemSlot, 1, itemSlot);
              }
              if (ammunitionItemStack.typeId === "ntk:scroll_of_thunderstorm") {
                thunderstormMagicSpell(source);
                decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                damageItemDurability(source, itemSlot, 1, itemSlot);
              }
              if (ammunitionItemStack.typeId === "ntk:scroll_of_hoarding_1") {
                hoardingMagicSpell1(source);
                decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                damageItemDurability(source, itemSlot, 1, itemSlot);
              }
              if (ammunitionItemStack.typeId === "ntk:scroll_of_hoarding_2") {
                hoardingMagicSpell2(source);
                decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                damageItemDurability(source, itemSlot, 1, itemSlot);
              }
              if (ammunitionItemStack.typeId === "ntk:scroll_of_hoarding_3") {
                hoardingMagicSpell3(source);
                decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                damageItemDurability(source, itemSlot, 1, itemSlot);
              }
              if (ammunitionItemStack.typeId === "ntk:scroll_of_homeward_bound") {
                homewardBoundMagicSpell(source);
                if (source.getSpawnPoint() !== undefined) {
                  decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                  damageItemDurability(source, itemSlot, 1, itemSlot);
                }
              }
              if (ammunitionItemStack.typeId === "ntk:unknown_scroll") {
                unknownMagicSpell(source);
                decreaseItemStack(source, ammunitionItemStack, 1, ammunitionSlot);
                damageItemDurability(source, itemSlot, 1, itemSlot);
              }
            }
            else {
              // Ammunition sound effect:
              if (ammunitionSoundEffect !== false) {
                source.dimension.playSound(ammunitionSoundEffect, source.location, { pitch: ammunitionSoundPitch, volume: ammunitionSoundVolume });
              }
            }
          }
        }
      }
    }
  },
};
system.beforeEvents.startup.subscribe(({ itemComponentRegistry }) => {
  itemComponentRegistry.registerCustomComponent(
    "ntk:wand_on_complete_use",
    ItemWandOnCompleteUseComponent
  );
});
