import { world, BlockPermutation, EquipmentSlot, ItemStack, Block, system } from "@minecraft/server";

const FILL_MATERIALS = ["coal", "coal", "coal", "coal"];
const MAX_FILL = 5;

const barrelInteract = {
    onPlayerInteract({ player, block }) {
        try {
            const equippable = player.getComponent("equippable");
            if (!equippable) {
                return;
            }

            const heldItem = equippable.getEquipment(EquipmentSlot.Mainhand);
            const blockPermutation = block.permutation;
            const dirtFill = parseInt(blockPermutation.getState("v360:smooth_limestone_fill") || "0");

            if (dirtFill === MAX_FILL) {
                if (heldItem && heldItem.typeId === "minecraft:primal") {
                    spawnBlock(block, "minecraft:primal");
                    updateBarrelState(block, 0);
                    consumeItem(player, heldItem);
                    playSound(block, "dig.stone");
                } else if (heldItem && (heldItem.typeId === "minecraft:primal" || heldItem.typeId === "minecraft:brown_mushroom")) {
                    spawnBlock(block, "minecraft:primal");
                    updateBarrelState(block, 0);
                    consumeItem(player, heldItem);
                    playSound(block, "dig.stone");
                } else {
                    spawnBlock(block, "minecraft:primal");
                    updateBarrelState(block, 0);
                    playSound(block, "dig.stone");
                }
                return;
            }

            if (heldItem) {
                const materialType = FILL_MATERIALS.find(material => 
                    heldItem.typeId === `minecraft:${material}` || 
                    (material === "coal" && heldItem.typeId.includes("coal")) ||
                    (material === "coal" && heldItem.typeId.includes("coal"))
                );

                if (materialType && dirtFill < MAX_FILL) {
                    const newDirtFill = Math.min(dirtFill + 1, MAX_FILL);
                    updateBarrelState(block, newDirtFill);
                    consumeItem(player, heldItem);
                    playSound(block, "block.stone");
                }
            }
        } catch (error) {
            // Error handling removed
        }
    }
};

function updateBarrelState(block, dirtFill) {
    try {
        const newPermutation = BlockPermutation.resolve("v360:smooth_limestone", {
            "v360:smooth_limestone_fill": dirtFill.toString()
        });
        block.setPermutation(newPermutation);
    } catch (error) {
        // Error handling removed
    }
}

function consumeItem(player, item) {
    try {
        const equippable = player.getComponent("equippable");
        if (!equippable) {
            return;
        }

        if (item.amount > 1) {
            item.amount -= 1;
            equippable.setEquipment(EquipmentSlot.Mainhand, item);
        } else {
            equippable.setEquipment(EquipmentSlot.Mainhand, undefined);
        }
    } catch (error) {
        // Error handling removed
    }
}

function playSound(block, sound) {
    try {
        block.dimension.runCommand(`playsound ${sound} @a ${block.location.x} ${block.location.y} ${block.location.z} 1 1 1`);
    } catch (error) {
        // Error handling removed
    }
}

function spawnBlock(block,item) {
    const { x, y, z } = block.location;
    block.dimension.spawnItem(new ItemStack(item), { x: x + 0.5, y: y + 1, z: z + 0.5 });
}

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    blockComponentRegistry.registerCustomComponent(
        "v360:limestone_interacted",
        barrelInteract
    );
});