import * as mc from "@minecraft/server";
const { world, system } = mc;

// Grouped and consistently formatted registration of sliced block components.
system.beforeEvents.startup.subscribe((initEvent) => {
    const registry = initEvent.blockComponentRegistry;

    /* -------------------------
       Flaxen cheese wheels (0..3)
       ------------------------- */
    registry.registerCustomComponent("brewinandchewin:flaxen_cheese_wheel", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "flaxen"`
                );
                result.block.setType("brewinandchewin:flaxen_cheese_wheel_2");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    registry.registerCustomComponent("brewinandchewin:flaxen_cheese_wheel1", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "flaxen"`
                );
                result.block.setType("brewinandchewin:flaxen_cheese_wheel_3");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    registry.registerCustomComponent("brewinandchewin:flaxen_cheese_wheel2", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "flaxen"`
                );
                result.block.setType("brewinandchewin:flaxen_cheese_wheel_4");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    registry.registerCustomComponent("brewinandchewin:flaxen_cheese_wheel3", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "flaxen"`
                );
                result.block.setType("minecraft:air");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    /* -------------------------
       Scarlet cheese wheels (0..3)
       ------------------------- */
    registry.registerCustomComponent("brewinandchewin:scarlet_cheese_wheel", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "scarlet"`
                );
                result.block.setType("brewinandchewin:scarlet_cheese_wheel_2");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    registry.registerCustomComponent("brewinandchewin:scarlet_cheese_wheel1", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "scarlet"`
                );
                result.block.setType("brewinandchewin:scarlet_cheese_wheel_3");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    registry.registerCustomComponent("brewinandchewin:scarlet_cheese_wheel2", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "scarlet"`
                );
                result.block.setType("brewinandchewin:scarlet_cheese_wheel_4");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    registry.registerCustomComponent("brewinandchewin:scarlet_cheese_wheel3", {
        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            item.getTags().forEach((tag) => {
                if (tag !== "fd_knife") return;

                result.dimension.runCommand(
                    `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "scarlet"`
                );
                result.block.setType("minecraft:air");

                const dur = item.getComponent("minecraft:durability");
                if (dur.damage < dur.maxDurability) {
                    dur.damage++;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                } else {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                    player.dimension.playSound("random.break", player.location);
                }
            });
        },
    });

    /* -------------------------
       Quiche slices (0..3)
       ------------------------- */
    registry.registerCustomComponent("brewinandchewin:quiche", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.eat", player.location);
            player.addEffect("saturation", 3, { amplifier: 0, showParticles: false });
            result.block.setType("brewinandchewin:quiche_slice1");
        },
    });

    registry.registerCustomComponent("brewinandchewin:quiche1", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.eat", player.location);
            player.addEffect("saturation", 3, { amplifier: 0, showParticles: false });
            result.block.setType("brewinandchewin:quiche_slice2");
        },
    });

    registry.registerCustomComponent("brewinandchewin:quiche2", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.eat", player.location);
            player.addEffect("saturation", 3, { amplifier: 0, showParticles: false });
            result.block.setType("brewinandchewin:quiche_slice3");
        },
    });

    registry.registerCustomComponent("brewinandchewin:quiche3", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.eat", player.location);
            player.addEffect("saturation", 3, { amplifier: 0, showParticles: false });
            result.block.setType("minecraft:air");
        },
    });

    /* -------------------------
       Pizza slices (0..3)
       ------------------------- */
    registry.registerCustomComponent("brewinandchewin:pizza", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.slime.big", player.location);
            result.dimension.runCommand(
                `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "pizza"`
            );
            result.block.setType("brewinandchewin:pizza_slice1");
        },
    });

    registry.registerCustomComponent("brewinandchewin:pizza1", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.slime.big", player.location);
            result.dimension.runCommand(
                `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "pizza"`
            );
            result.block.setType("brewinandchewin:pizza_slice2");
        },
    });

    registry.registerCustomComponent("brewinandchewin:pizza2", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.slime.big", player.location);
            result.dimension.runCommand(
                `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "pizza"`
            );
            result.block.setType("brewinandchewin:pizza_slice3");
        },
    });

    registry.registerCustomComponent("brewinandchewin:pizza3", {
        onPlayerInteract: (result) => {
            const player = result.player;
            result.block.dimension.playSound("random.slime.big", player.location);
            result.dimension.runCommand(
                `loot spawn ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} loot "pizza"`
            );
            result.block.setType("minecraft:air");
        },
    });

    /* -------------------------
       Fiery fondue pots (ffpf, ffpl1, ffpl2)
       ------------------------- */
    registry.registerCustomComponent("brewinandchewin:ffpf", {
        onTick: (result) => {
            result.block.dimension.runCommand(
                `execute positioned ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} run function smoke`
            );
        },

        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            if (item.typeId === "minecraft:bowl") {
                result.block.setType("brewinandchewin:fiery_fondue_pot_level1");
                result.dimension.runCommand(
                    `execute as ${player.name} run loot give @s loot "fiery_fondue"`
                );

                if (item.amount === 1) {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                } else {
                    item.amount--;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                }
            }
        },
    });

    registry.registerCustomComponent("brewinandchewin:ffpl1", {
        onTick: (result) => {
            result.block.dimension.runCommand(
                `execute positioned ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} run function smoke`
            );
        },

        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            if (item.typeId === "minecraft:bowl") {
                result.block.setType("brewinandchewin:fiery_fondue_pot_level2");
                result.dimension.runCommand(
                    `execute as ${player.name} run loot give @s loot "fiery_fondue"`
                );

                if (item.amount === 1) {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                } else {
                    item.amount--;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                }
            }
        },
    });

    registry.registerCustomComponent("brewinandchewin:ffpl2", {
        onTick: (result) => {
            result.block.dimension.runCommand(
                `execute positioned ${result.block.center().x} ${result.block.center().y} ${result.block.center().z} run function smoke`
            );
        },

        onPlayerInteract: (result) => {
            const player = result.player;
            const item = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
            if (!item) return;

            if (item.typeId === "minecraft:bowl") {
                result.block.setType("minecraft:cauldron");
                result.dimension.runCommand(
                    `execute as ${player.name} run loot give @s loot "fiery_fondue_and_bone"`
                );

                if (item.amount === 1) {
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", undefined);
                } else {
                    item.amount--;
                    player.getComponent("minecraft:equippable").setEquipment("Mainhand", item);
                }
            }
        },
    });
});