import { world, ItemStack, GameMode } 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 });
    }
};

world.beforeEvents.worldInitialize.subscribe(pch => {
    // Wine
    pch.blockComponentRegistry.registerCustomComponent('peach:wine_logic', {
        onPlayerInteract: e => {
            const { player, block } = e;
            const selectedItem = Utils.getSelectedItem(player);
            if (!selectedItem) return;

            const level = block.permutation.getState("peach:barrel_level");

            if (selectedItem.typeId === "peach:mug_wine" && level > 0) {
                Utils.consumeItem(player, selectedItem);
                player.getComponent("inventory").container.addItem(new ItemStack("peach:mug"));
                Utils.setBlockState(block, "peach:barrel_level", level - 1);
                player.playSound("random.pop");
                return;
            }

            if (selectedItem.typeId === "peach:mug" && level < 7) {
                Utils.consumeItem(player, selectedItem);
                player.getComponent("inventory").container.addItem(new ItemStack("peach:mug_wine"));
                Utils.setBlockState(block, "peach:barrel_level", level + 1);
                player.playSound("random.pop2");
                return;
            }
        }
    });

    // Beer
    pch.blockComponentRegistry.registerCustomComponent('peach:beer_logic', {
        onPlayerInteract: e => {
            const { player, block } = e;
            const selectedItem = Utils.getSelectedItem(player);
            if (!selectedItem) return;

            const level = block.permutation.getState("peach:barrel_level");

            if (selectedItem.typeId === "peach:mug_beer" && level > 0) {
                Utils.consumeItem(player, selectedItem);
                player.getComponent("inventory").container.addItem(new ItemStack("peach:mug"));
                Utils.setBlockState(block, "peach:barrel_level", level - 1);
                player.playSound("random.pop");
                return;
            }

            if (selectedItem.typeId === "peach:mug" && level < 7) {
                Utils.consumeItem(player, selectedItem);
                player.getComponent("inventory").container.addItem(new ItemStack("peach:mug_beer"));
                Utils.setBlockState(block, "peach:barrel_level", level + 1);
                player.playSound("random.pop2");
                return;
            }
        }
    });

    // mead
    pch.blockComponentRegistry.registerCustomComponent('peach:mead_logic', {
        onPlayerInteract: e => {
            const { player, block } = e;
            const selectedItem = Utils.getSelectedItem(player);
            if (!selectedItem) return;

            const level = block.permutation.getState("peach:barrel_level");

            if (selectedItem.typeId === "peach:mug_mead" && level > 0) {
                Utils.consumeItem(player, selectedItem);
                player.getComponent("inventory").container.addItem(new ItemStack("peach:mug"));
                Utils.setBlockState(block, "peach:barrel_level", level - 1);
                player.playSound("random.pop");
                return;
            }

            if (selectedItem.typeId === "peach:mug" && level < 7) {
                Utils.consumeItem(player, selectedItem);
                player.getComponent("inventory").container.addItem(new ItemStack("peach:mug_mead"));
                Utils.setBlockState(block, "peach:barrel_level", level + 1);
                player.playSound("random.pop2");
                return;
            }
        }
    });
});