import { BlockPermutation } from "@minecraft/server";
import { beforeEvents } from "./Eternal/Constants";
import { test, runTimeout } from "./Eternal/Utils";

let delay = 0;

beforeEvents.itemUseOn.subscribe((e) => {
  if (delay > 0) {
    e.cancel = true;
    return;
  }

  runTimeout(() => {
    delay = 0;
  }, 2);

  const { itemStack, block, source } = e;

  delay++;

  const dirts = [
    "minecraft:dirt",
    "minecraft:grass_block",
    "minecraft:grass_path",
    "eternal:mystical_jungle_dirt",
    "eternal:mystical_jungle_grass",
    "eternal:toxic_tundra_dirt",
    "eternal:toxic_tundra_grass",
    "eternal:theia_plateaus_dirt",
    "eternal:theia_plateaus_grass",
    "eternal:golden_glades_grass",
    "eternal:golden_grass_marble",
  ];

  const isDirt = dirts.includes(block.typeId);

  const isInteractive = itemStack.typeId.match(/(minecraft|eternal):.+(_hoe|_axe|_shovel)/);
  if (isInteractive) {
    if (/_axe/.test(itemStack.typeId)) {
      const nonStrippableRegex = /^(minecraft:stripped_\w+_(log|wood)|eternal:\w+_(stripped_log|hollow_log))$/;

      if (nonStrippableRegex.test(block.typeId)) return;

      const isEternalStrippable = /^eternal:.+_log/.test(block.typeId); // Replace this with one below when there are now custom stripped woods
      // const isEternalStrippable = /^eternal:.+(_log|_wood)/.test(block.typeId);
      const isVanillaStrippable = /^minecraft:.+(_log|_wood)/.test(block.typeId);

      if (isEternalStrippable) {
        const newBlockId = block.typeId.replace(/(_log|_wood)(?!_stripped)/, "_stripped$1");
        const rotation = block.permutation?.getState("eternal:rotation") || 0;

        block.dimension.commandRunAsync(
          `setblock ${block.location.x} ${block.location.y} ${block.location.z} ${newBlockId} ["eternal:rotation"=${rotation}]`
        );
        source.commandRunAsync(`playsound dig.wood @s ~~~ 1.0 0.7`);
        source.doUseAnimation(itemStack);
      }

      if (isVanillaStrippable) {
        const vanillaRegex = /minecraft:(\w+)(_log|_wood)/;
        const replacement = "minecraft:stripped_$1$2";

        const newBlockId = block.typeId.replace(vanillaRegex, replacement);
        const axis = block.permutation?.getState("pillar_axis") || "y";

        block.dimension.commandRunAsync(
          `setblock ${block.location.x} ${block.location.y} ${block.location.z} ${newBlockId} ["pillar_axis"="${axis}"]`,
          `playsound dig.wood @a ${block.location.x} ${block.location.y} ${block.location.z}`
        );
        source.doUseAnimation(itemStack);
      }
    } else if (/_hoe/.test(itemStack.typeId)) {
      if (!isDirt) return;

      block.dimension.commandRunAsync(
        `setblock ${block.location.x} ${block.location.y} ${block.location.z} minecraft:farmland`,
        `playsound dig.grass @a ${block.location.x} ${block.location.y} ${block.location.z}`
      );
      source.doUseAnimation(itemStack);
    } else if (/_shovel/.test(itemStack.typeId)) {
      if (!isDirt) return;
      if (block.typeId == "minecraft:grass_path") return;

      block.dimension.commandRunAsync(
        `setblock ${block.location.x} ${block.location.y} ${block.location.z} minecraft:grass_path`,
        `playsound dig.grass @a ${block.location.x} ${block.location.y} ${block.location.z}`
      );
      source.doUseAnimation(itemStack);
    }
  }
});
