import { world, ItemStack, system, 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 });
    },
    giveItem(player, id) {
        player.getComponent("inventory").container.addItem(new ItemStack(id));
    },
    playSound(block, sound) {
        block.dimension.playSound(sound, block.location);
    },
    spawnSmoke(block) {
        const loc = block.location;
        block.dimension.spawnParticle("minecraft:basic_smoke_particle", { x: loc.x + 0.5, y: loc.y + 1, z: loc.z + 0.5 });
    }
};

world.beforeEvents.worldInitialize.subscribe(pch => {
    pch.blockComponentRegistry.registerCustomComponent('peach:toaster_logic', {
        onPlayerInteract: e => {
            const { player, block } = e;
            const selectedItem = Utils.getSelectedItem(player);
            let state = block.permutation.getState("peach:toaster_state");

            if (state === "empty" && selectedItem?.typeId === "peach:bread_slice") {
                Utils.consumeItem(player, selectedItem);
                Utils.setBlockState(block, "peach:toaster_state", "slice_1");
                Utils.playSound(block, "random.pop");
                return;
            }

            if (state === "slice_1") {
                if (selectedItem?.typeId === "peach:bread_slice") {
                    Utils.consumeItem(player, selectedItem);
                    Utils.setBlockState(block, "peach:toaster_state", "slice_2");
                    Utils.playSound(block, "random.pop");
                    return;
                }
                if (player.isSneaking) {
                    Utils.setBlockState(block, "peach:toaster_state", "heating_1");
                    Utils.playSound(block, "tile.piston.out");
                    system.runTimeout(() => {
                        Utils.setBlockState(block, "peach:toaster_state", "toast_1");
                        Utils.playSound(block, "tile.piston.out");
                        Utils.spawnSmoke(block);
                    }, 800);
                    return;
                }
                Utils.giveItem(player, "peach:bread_slice");
                Utils.setBlockState(block, "peach:toaster_state", "empty");
                Utils.playSound(block, "random.pop");
                return;
            }

            if (state === "slice_2") {
                if (player.isSneaking) {
                    Utils.setBlockState(block, "peach:toaster_state", "heating_2");
                    Utils.playSound(block, "tile.piston.out");
                    system.runTimeout(() => {
                        Utils.setBlockState(block, "peach:toaster_state", "toast_2");
                        Utils.playSound(block, "tile.piston.out");
                        Utils.spawnSmoke(block);
                    }, 800);
                    return;
                }
                Utils.giveItem(player, "peach:bread_slice");
                Utils.setBlockState(block, "peach:toaster_state", "slice_1");
                Utils.playSound(block, "random.pop");
                return;
            }

            if (state === "toast_1") {
                Utils.giveItem(player, "peach:toast");
                Utils.setBlockState(block, "peach:toaster_state", "empty");
                Utils.playSound(block, "random.pop");
                return;
            }

            if (state === "toast_2") {
                Utils.giveItem(player, "peach:toast");
                Utils.giveItem(player, "peach:toast");
                Utils.setBlockState(block, "peach:toaster_state", "empty");
                Utils.playSound(block, "random.pop");
                return;
            }
        }
    });
});