import { world, 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);
  },
  spawnParticle(block, particle) {
    const loc = block.location;
    block.dimension.spawnParticle(particle, {
      x: loc.x + 0.5,
      y: loc.y + 1,
      z: loc.z + 0.5
    });
  }
};

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

      let ingredient = block.permutation.getState("peach:ingredient");
      let level = block.permutation.getState("peach:level");

      if (ingredient === 0 && selectedItem?.typeId === "minecraft:milk_bucket") {
        Utils.consumeItem(player, selectedItem);
        Utils.giveItem(player, "minecraft:bucket");
        Utils.setBlockState(block, "peach:ingredient", 1);
        Utils.playSound(block, "bucket.fill_water");
        return;
      }

      if (ingredient === 1) {
        if (selectedItem?.typeId === "minecraft:sugar") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:ingredient", 2);
          Utils.spawnParticle(block, "minecraft:crop_growth_emitter");
          Utils.playSound(block, "random.pop");
          return;
        }
        if (selectedItem?.typeId === "peach:salt") {
          Utils.consumeItem(player, selectedItem);
          Utils.setBlockState(block, "peach:ingredient", 3);
          Utils.spawnParticle(block, "minecraft:crop_growth_emitter");
          Utils.playSound(block, "random.pop");
          return;
        }
        if (selectedItem?.typeId === "minecraft:bucket") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:milk_bucket");
          Utils.setBlockState(block, "peach:ingredient", 0);
          Utils.playSound(block, "bucket.fill_water");
          return;
        }
      }


      if (level < 6 && ingredient > 1) {
        Utils.setBlockState(block, "peach:level", level + 1);
        Utils.playSound(block, "block.barrel.close");
        return;
      }

      if (level === 6) {
        if (ingredient === 2) {
          Utils.giveItem(player, "peach:cream", 2);
          Utils.setBlockState(block, "peach:ingredient", 0);
          Utils.setBlockState(block, "peach:level", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
        if (ingredient === 3) {
          Utils.giveItem(player, "peach:butter", 2);
          Utils.setBlockState(block, "peach:ingredient", 0);
          Utils.setBlockState(block, "peach:level", 0);
          Utils.playSound(block, "random.pop");
          return;
        }
      }
    }
  });
});
