import { world, system, GameMode, ItemStack } from "@minecraft/server";
import { updateBlock } from "../utils/block";
import { consumeEquipped } from "../utils/item";

const Utils = {
  getPlayerEquipment(player) {
    return player.getComponent("equippable");
  },
  getSelectedItem(player) {
    const equip = this.getPlayerEquipment(player);
    return equip ? equip.getEquipment("Mainhand") : null;
  },
  consumeItem(player, item) {
    if (player.getGameMode() === GameMode.creative) return;
    const equip = this.getPlayerEquipment(player);
    consumeEquipped(equip, item);
  },
  setBlockState(block, stateName, value) {
    updateBlock(block, { [stateName]: value });
  },
  giveItem(player, id, amount = 1) {
    player.getComponent("inventory").container.addItem(new ItemStack(id, amount));
  },
  playSound(block, sound) {
    block.dimension.playSound(sound, block.location);
  }
};

world.beforeEvents.worldInitialize.subscribe(init => {
  init.blockComponentRegistry.registerCustomComponent("peach:pestle_logic", {
    onPlayerInteract: e => {
      const { player, block } = e;
      const selectedItem = Utils.getSelectedItem(player);

      let mode = block.permutation.getState("peach:mode");
      let recipe = block.permutation.getState("peach:recipe");

      if (mode === 0 && recipe === 0) {
        if (selectedItem?.typeId === "minecraft:wheat") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 1);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "minecraft:bone") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 2);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "peach:cassava") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 3);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "peach:pepper_black") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 4);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "peach:bell_pepper") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 5);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "peach:oregano") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 6);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "peach:turmenic") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 7);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "peach:roasted_cocoa_beans") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 8);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
        if (selectedItem?.typeId === "peach:roasted_seeds_coffee") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:recipe", 9);
          Utils.setBlockState(block, "peach:mode", 1);
          Utils.playSound(block, "block.composter.fill");
          return;
        }
      }

      if (mode < 9 && mode > 0) {
        Utils.setBlockState(block, "peach:mode", mode + 1);
        Utils.playSound(block, "block.grindstone.use");
        return;
      }

      if (mode === 9) {
        if (recipe === 1) {
          Utils.giveItem(player, "peach:flour");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 2) {
          Utils.giveItem(player, "peach:collagen");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 3) {
          Utils.giveItem(player, "peach:tapioca");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 4 && selectedItem?.typeId === "peach:pot") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "peach:pot_black_pepper");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 5 && selectedItem?.typeId === "peach:pot") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "peach:pot_paprika");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 6 && selectedItem?.typeId === "peach:pot") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "peach:pot_oregano");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 7 && selectedItem?.typeId === "peach:pot") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "peach:pot_turmenic");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 8) {
          Utils.giveItem(player, "peach:chocolate_powder");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (recipe === 9 && selectedItem?.typeId === "peach:tin") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "peach:tin_coffee_powder");
          Utils.setBlockState(block, "peach:recipe", 0);
          Utils.setBlockState(block, "peach:mode", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
      }
    }
  });
});
