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:bucket_logic", {
    onPlayerInteract: e => {
      const { player, block } = e;
      const selectedItem = Utils.getSelectedItem(player);

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

      if (selectedItem?.typeId === "minecraft:bucket") {
        if (liquid === 1) {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:water_bucket");
          Utils.setBlockState(block, "peach:liquid", 0);
          Utils.playSound(block, "bucket.fill_water");
          return;
        }
        if (liquid === 2) {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:lava_bucket");
          Utils.setBlockState(block, "peach:liquid", 0);
          Utils.playSound(block, "bucket.fill_lava");
          return;
        }
        if (liquid === 3) {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:milk_bucket");
          Utils.setBlockState(block, "peach:liquid", 0);
          Utils.playSound(block, "bucket.fill_water");
          return;
        }
        if (liquid === 4) {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:powder_snow_bucket");
          Utils.setBlockState(block, "peach:liquid", 0);
          Utils.playSound(block, "bucket.fill_powder_snow");
          return;
        }
      }

      if (liquid === 0) {
        if (selectedItem?.typeId === "minecraft:water_bucket") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:bucket");
          Utils.setBlockState(block, "peach:liquid", 1);
          Utils.playSound(block, "bucket.fill_water");
          return;
        }
        if (selectedItem?.typeId === "minecraft:lava_bucket") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:bucket");
          Utils.setBlockState(block, "peach:liquid", 2);
          Utils.playSound(block, "bucket.fill_lava");
          return;
        }
        if (selectedItem?.typeId === "minecraft:milk_bucket") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:bucket");
          Utils.setBlockState(block, "peach:liquid", 3);
          Utils.playSound(block, "bucket.fill_water");
          return;
        }
        if (selectedItem?.typeId === "minecraft:powder_snow_bucket") {
          Utils.consumeItem(player, selectedItem);
          Utils.giveItem(player, "minecraft:bucket");
          Utils.setBlockState(block, "peach:liquid", 4);
          Utils.playSound(block, "bucket.fill_powder_snow");
          return;
        }
      }
    }
  });
});
