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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:wooden_crates_interaction", {
    onPlayerInteract: eventData => {
      const player = eventData.player;
      const block = eventData.block;
      const blockLocation = block.location;
      const playerEquippable = player.getComponent("equippable");
      const itemUsed = playerEquippable.getEquipment("Mainhand");
      const woodenCrates = [
        "nicothekid:wooden_acacia_crate",
        "nicothekid:wooden_bamboo_crate",
        "nicothekid:wooden_birch_crate",
        "nicothekid:wooden_cherry_crate",
        "nicothekid:wooden_crimson_crate",
        "nicothekid:wooden_dark_oak_crate",
        "nicothekid:wooden_jungle_crate",
        "nicothekid:wooden_mangrove_crate",
        "nicothekid:wooden_oak_crate",
        "nicothekid:wooden_spruce_crate",
        "nicothekid:wooden_warped_crate"
      ];
      function damageDurability () {
        if (!player.matches({ gameMode: GameMode.creative })) {
          const itemEnchantable = itemUsed.getComponent("enchantable");
          const unbreakingLevel = itemEnchantable?.getEnchantment("unbreaking")?.level ?? 0;
          const breakChance = 100 / (unbreakingLevel + 1);
          const randomizeChance = Math.random() * 100;
          if (breakChance < randomizeChance) return;
          const itemDurability = itemUsed.getComponent("durability");
          if (!itemDurability) return;
          const itemMaxDurability = itemDurability.maxDurability;
          const itemDurabilityDamage = itemDurability.damage;
          const durabilityCalculation = itemMaxDurability - itemDurabilityDamage;
          let damageAmount = 1;
          const currentDurability = durabilityCalculation - damageAmount;
          if (currentDurability <= 0) {
            player.playSound("random.break", { pitch: 0.9, location: player.location, volume: 1 });
            playerEquippable.setEquipment("Mainhand", null);
          }
          else {
            itemDurability.damage += damageAmount;
            playerEquippable.setEquipment("Mainhand", itemUsed)
          }
        }
      }
      if (itemUsed === undefined) {
        if (woodenCrates.includes(block.typeId) && !player.isSneaking) {
          const pitchRange = Math.random() * (1.0 - 0.8) + 0.8;
          system.run(() => {
            block.dimension.playSound("game.player.attack.nodamage", blockLocation, { pitch: pitchRange, volume: 1.0 });
            player.runCommandAsync(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Flint Axe to open the crate.§r\"}]}`);
          });
        }
        else return;
      }
      else if (itemUsed.typeId === "nicothekid:flint_axe" && woodenCrates.includes(block.typeId)) {
        const pitchRange = Math.random() * (1.2 - 0.8) + 0.8;
        if (block.typeId === "nicothekid:wooden_acacia_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_bamboo_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_birch_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_cherry_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_crimson_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_nether_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_dark_oak_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_jungle_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_mangrove_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_oak_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_spruce_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (block.typeId === "nicothekid:wooden_warped_crate") {
          system.run(() => {
            damageDurability ();
            block.dimension.playSound("block.barrel.open", blockLocation, { pitch: pitchRange, volume: 1.0 });
            block.dimension.runCommandAsync(`loot spawn ${blockLocation.x + 0.5} ${blockLocation.y + 1.0} ${blockLocation.z + 0.5} loot "keys_crates/events/wooden_crates/nicothekid_wooden_nether_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
      }
      else return;
    }
  });
});
