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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:locked_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 lockedCrates = [
        "nicothekid:copper_crate",
        "nicothekid:diamond_crate",
        "nicothekid:golden_crate",
        "nicothekid:iron_crate",
        "nicothekid:piglin_crate"
      ];
      const cratesKeys = [
        "nicothekid:copper_crate_key",
        "nicothekid:diamond_crate_key",
        "nicothekid:golden_crate_key",
        "nicothekid:iron_crate_key",
        "nicothekid:piglin_crate_key"
      ];
      function decreaseItem () {
        system.run(() => {
          if (!player.matches({ gameMode: GameMode.creative })) {
            const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
            playerEquippable.setEquipment("Mainhand", decrementItem)
          }
        });
      }
      if (itemUsed === undefined) {
        if (lockedCrates.includes(block.typeId) && !player.isSneaking) {
          const pitchRange = Math.random() * (1.0 - 0.8) + 0.8;
          if (block.typeId === "nicothekid:copper_crate") {
            system.run(() => {
              block.dimension.playSound("block.nicothekid_locked_crate.lock", blockLocation, { pitch: pitchRange, volume: 1.0 });
              player.runCommandAsync(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Copper Crate Key to open the crate.§r\"}]}`);
            });
          }
          if (block.typeId === "nicothekid:diamond_crate") {
            system.run(() => {
              block.dimension.playSound("block.nicothekid_locked_crate.lock", blockLocation, { pitch: pitchRange, volume: 1.0 });
              player.runCommandAsync(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Diamond Crate Key to open the crate.§r\"}]}`);
            });
          }
          if (block.typeId === "nicothekid:golden_crate") {
            system.run(() => {
              block.dimension.playSound("block.nicothekid_locked_crate.lock", blockLocation, { pitch: pitchRange, volume: 1.0 });
              player.runCommandAsync(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Golden Crate Key to open the crate.§r\"}]}`);
            });
          }
          if (block.typeId === "nicothekid:iron_crate") {
            system.run(() => {
              block.dimension.playSound("block.nicothekid_locked_crate.lock", blockLocation, { pitch: pitchRange, volume: 1.0 });
              player.runCommandAsync(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Iron Crate Key to open the crate.§r\"}]}`);
            });
          }
          if (block.typeId === "nicothekid:piglin_crate") {
            system.run(() => {
              block.dimension.playSound("block.nicothekid_locked_crate.lock", blockLocation, { pitch: pitchRange, volume: 1.0 });
              player.runCommandAsync(`tellraw @p {\"rawtext\":[{\"text\":\"§eUse a Piglin Crate Key to open the crate.§r\"}]}`);
            });
          }
        }
        else return;
      }
      else if (cratesKeys.includes(itemUsed.typeId) && lockedCrates.includes(block.typeId)) {
        const pitchRange = Math.random() * (1.1 - 1.0) + 1.0;
        if (itemUsed.typeId === "nicothekid:copper_crate_key" && block.typeId === "nicothekid:copper_crate") {
          system.run(() => {
            decreaseItem ();
            block.dimension.playSound("random.chestopen", 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/locked_crates/nicothekid_copper_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (itemUsed.typeId === "nicothekid:diamond_crate_key" && block.typeId === "nicothekid:diamond_crate") {
          system.run(() => {
            decreaseItem ();
            block.dimension.playSound("random.chestopen", 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/locked_crates/nicothekid_diamond_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (itemUsed.typeId === "nicothekid:golden_crate_key" && block.typeId === "nicothekid:golden_crate") {
          system.run(() => {
            decreaseItem ();
            block.dimension.playSound("random.chestopen", 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/locked_crates/nicothekid_golden_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (itemUsed.typeId === "nicothekid:iron_crate_key" && block.typeId === "nicothekid:iron_crate") {
          system.run(() => {
            decreaseItem ();
            block.dimension.playSound("random.chestopen", 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/locked_crates/nicothekid_iron_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
        if (itemUsed.typeId === "nicothekid:piglin_crate_key" && block.typeId === "nicothekid:piglin_crate") {
          system.run(() => {
            decreaseItem ();
            block.dimension.playSound("random.chestopen", 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/nicothekid_piglin_crate_looted"`);
            block.setPermutation(BlockPermutation.resolve("minecraft:air"));
          });
        }
      }
      else return;
    }
  });
});
