import { world, system, BlockPermutation, ItemStack, GameMode } from "@minecraft/server";
import { damageItemDurability, decreaseItemStack } from "../ntk_functions.js";

world.beforeEvents.playerInteractWithBlock.subscribe(event => {
  const player = event.player;
  const itemStack = event.itemStack;
  const block = event.block;
  if (itemStack === undefined) {
    if (block.hasTag("ntk:wood_crate") && !player.isSneaking) {
      const pitchRange = Math.random() * (1.2 - 1.0) + 1.0;
      system.run(() => {
        block.dimension.playSound("game.player.attack.nodamage", block.location, { pitch: pitchRange, volume: 1.0 });
        player.runCommand(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Flint Axe to open the crate.§r\"}]}`);
      });
    }
    if (block.hasTag("ntk:locked_crate") && !player.isSneaking) {
      const pitchRange = Math.random() * (1.0 - 0.8) + 0.8;
      if (block.typeId === "ntk:copper_crate") {
        system.run(() => {
          block.dimension.playSound("block.ntk_locked_crate.lock", block.location, { pitch: pitchRange, volume: 1.0 });
          player.runCommand(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Copper Crate Key to open the crate.§r\"}]}`);
        });
      }
      if (block.typeId === "ntk:diamond_crate") {
        system.run(() => {
          block.dimension.playSound("block.ntk_locked_crate.lock", block.location, { pitch: pitchRange, volume: 1.0 });
          player.runCommand(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Diamond Crate Key to open the crate.§r\"}]}`);
        });
      }
      if (block.typeId === "ntk:golden_crate") {
        system.run(() => {
          block.dimension.playSound("block.ntk_locked_crate.lock", block.location, { pitch: pitchRange, volume: 1.0 });
          player.runCommand(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Golden Crate Key to open the crate.§r\"}]}`);
        });
      }
      if (block.typeId === "ntk:iron_crate") {
        system.run(() => {
          block.dimension.playSound("block.ntk_locked_crate.lock", block.location, { pitch: pitchRange, volume: 1.0 });
          player.runCommand(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Iron Crate Key to open the crate.§r\"}]}`);
        });
      }
      if (block.typeId === "ntk:piglin_crate") {
        system.run(() => {
          block.dimension.playSound("block.ntk_locked_crate.lock", block.location, { pitch: pitchRange, volume: 1.0 });
          player.runCommand(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Piglin Crate Key to open the crate.§r\"}]}`);
        });
      }
    }
  }
  else if (itemStack.hasTag("ntk:crate_key") && block.hasTag("ntk:locked_crate")) {
    const pitchRange = Math.random() * (1.1 - 1.0) + 1.0;
    if (itemStack.typeId === "ntk:copper_crate_key" && block.typeId === "ntk:copper_crate") {
      system.run(() => {
        decreaseItemStack(player, itemStack, 1, "Mainhand");
        block.dimension.playSound("random.chestopen", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_copper_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (itemStack.typeId === "ntk:diamond_crate_key" && block.typeId === "ntk:diamond_crate") {
      system.run(() => {
        decreaseItemStack(player, itemStack, 1, "Mainhand");
        block.dimension.playSound("random.chestopen", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_diamond_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (itemStack.typeId === "ntk:golden_crate_key" && block.typeId === "ntk:golden_crate") {
      system.run(() => {
        decreaseItemStack(player, itemStack, 1, "Mainhand");
        block.dimension.playSound("random.chestopen", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_golden_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (itemStack.typeId === "ntk:iron_crate_key" && block.typeId === "ntk:iron_crate") {
      system.run(() => {
        decreaseItemStack(player, itemStack, 1, "Mainhand");
        block.dimension.playSound("random.chestopen", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_iron_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (itemStack.typeId === "ntk:piglin_crate_key" && block.typeId === "ntk:piglin_crate") {
      system.run(() => {
        decreaseItemStack(player, itemStack, 1, "Mainhand");
        block.dimension.playSound("random.chestopen", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_piglin_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
  }
  else if (itemStack.typeId === "ntk:flint_axe" && block.hasTag("ntk:wood_crate")) {
    const pitchRange = Math.random() * (1.2 - 0.8) + 0.8;
    if (block.typeId === "ntk:acacia_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:bamboo_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:birch_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:cherry_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:crimson_hyphae_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_nether_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:dark_oak_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:jungle_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:mangrove_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:oak_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:pale_oak_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:spruce_wood_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
    if (block.typeId === "ntk:warped_hyphae_crate") {
      system.run(() => {
        damageItemDurability(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("block.barrel.open", block.location, { pitch: pitchRange, volume: 1.0 });
        block.dimension.runCommand(`loot spawn ${block.location.x + 0.5} ${block.location.y + 1.0} ${block.location.z + 0.5} loot "kc/events/loot_wood_nether_crate"`);
        block.setPermutation(BlockPermutation.resolve("minecraft:air"));
      });
    }
  }
  else return;
});
