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

world.beforeEvents.playerInteractWithBlock.subscribe(event => {
  const player = event.player;
  const itemStack = event.itemStack;
  const block = event.block;
  const statePillarAxis = block.permutation.getState("pillar_axis");
  const strippedWoods = [
    "minecraft:stripped_acacia_log",
    "minecraft:stripped_acacia_wood",
    "minecraft:stripped_bamboo_block",
    "minecraft:stripped_birch_log",
    "minecraft:stripped_birch_wood",
    "minecraft:stripped_cherry_log",
    "minecraft:stripped_cherry_wood",
    "minecraft:stripped_crimson_hyphae",
    "minecraft:stripped_crimson_stem",
    "minecraft:stripped_dark_oak_log",
    "minecraft:stripped_dark_oak_wood",
    "minecraft:stripped_jungle_log",
    "minecraft:stripped_jungle_wood",
    "minecraft:stripped_mangrove_log",
    "minecraft:stripped_mangrove_wood",
    "minecraft:stripped_oak_log",
    "minecraft:stripped_oak_wood",
    "minecraft:stripped_pale_oak_log",
    "minecraft:stripped_pale_oak_wood",
    "minecraft:stripped_spruce_log",
    "minecraft:stripped_spruce_wood",
    "minecraft:stripped_warped_hyphae",
    "minecraft:stripped_warped_stem"
  ];
  if (itemStack === undefined) return;
  if (strippedWoods.includes(block.typeId)) {
    if (itemStack.typeId === "minecraft:bone_meal" || itemStack.hasTag("ntk:bone_meal")) {
      system.run(() => {
        decreaseItemStack(player, itemStack, 1, "Mainhand", false);
        block.dimension.playSound("item.bone_meal.use", block.location, { pitch: 1.0, volume: 1.0 });
        block.dimension.spawnParticle("ntk:barked_2_particle", { x: block.location.x, y: block.location.y, z: block.location.z });
        if (block.permutation.matches("minecraft:stripped_acacia_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:acacia_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_acacia_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:acacia_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_bamboo_block")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:bamboo_block", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_birch_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:birch_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_birch_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:birch_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_cherry_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:cherry_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_cherry_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:cherry_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_crimson_stem")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:crimson_stem", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_crimson_hyphae")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:crimson_hyphae", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_dark_oak_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:dark_oak_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_dark_oak_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:dark_oak_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_jungle_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:jungle_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_jungle_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:jungle_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_mangrove_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:mangrove_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_mangrove_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:mangrove_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_oak_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:oak_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_oak_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:oak_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_pale_oak_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:pale_oak_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_pale_oak_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:pale_oak_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_spruce_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:spruce_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_spruce_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:spruce_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_warped_stem")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:warped_stem", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_warped_hyphae")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:warped_hyphae", { "pillar_axis": statePillarAxis } ));
          return;
        }
      });
    }
    else return;
  }
  else return;
});
