import { world, ItemStack, GameMode, system } from "@minecraft/server";
import { updateBlock } from "../utils/block";
import { consumeEquipped } from "../utils/item";

const Utils = {
    getPlayerEquipment(player) {
        try {
            return player.getComponent("equippable");
        } catch (e) {
            return null;
        }
    },
    getSelectedItem(player) {
        const equip = this.getPlayerEquipment(player);
        try {
            return equip ? equip.getEquipment("Mainhand") : null;
        } catch (e) {
            return null;
        }
    },
    consumeItem(player, item) {
        try {
            if (player.getGameMode && player.getGameMode() === GameMode.creative) return;
        } catch (e) {}
        const equip = this.getPlayerEquipment(player);
        if (equip) consumeEquipped(equip, item);
    },
    setBlockState(block, stateName, value) {
        try {
            const newPerm = block.permutation.withState(stateName, value);
            block.setPermutation(newPerm);
        } catch (err) {
            try { updateBlock(block, { [stateName]: value }); } catch (e) {}
        }
    },
    giveItem(player, id) {
        try {
            let stack;
            try { stack = new ItemStack(id); } catch (e) {
                try { stack = ItemStack.create(id); } catch (e2) { stack = null; }
            }
            if (stack) player.getComponent("inventory").container.addItem(stack);
        } catch (e) {}
    },
    playSound(block, sound) {
        try { block.dimension.playSound(sound, block.location); } catch (e) {}
    },
    spawnParticle(block, particle) {
        const loc = block.location;
        try {
            block.dimension.spawnParticle(particle, {
                x: loc.x + 0.5,
                y: loc.y + 1,
                z: loc.z + 0.5
            });
        } catch (e) {}
    }
};

function scheduleDone(block) {
    try {
        const ticks = 20 + Math.floor(Math.random() * 30);
        const delayMs = ticks * 10;
        system.runTimeout(() => {
            try {
                const b = block.dimension.getBlock(block.location);
                if (!b) return;
                const mode = b.permutation.getState("peach:mode");
                if (mode === "3") {
                    Utils.setBlockState(b, "peach:mode", "4");
                    Utils.spawnParticle(b, "minecraft:campfire_smoke_particle");
                }
            } catch (err) {}
        }, delayMs);
    } catch (e) {}
}
function scheduleDoneLard(block) {
    try {
        const ticks = 20 + Math.floor(Math.random() * 40);
        const delayMs = ticks * 30;
        system.runTimeout(() => {
            try {
                const b = block.dimension.getBlock(block.location);
                if (!b) return;
                const mode = b.permutation.getState("peach:mode");
                if (mode === "5") {
                    Utils.setBlockState(b, "peach:mode", "1");
                    Utils.spawnParticle(b, "minecraft:campfire_smoke_particle");
                }
            } catch (err) {}
        }, delayMs);
    } catch (e) {}
}

world.beforeEvents.worldInitialize.subscribe(init => {
    init.blockComponentRegistry.registerCustomComponent("peach:saucepan_logic", {
        onPlayerInteract: e => {
            const { player, block } = e;
            if (!player || !block) return;

            const item = Utils.getSelectedItem(player);
            const recipe = block.permutation.getState("peach:recipe");
            const mode = block.permutation.getState("peach:mode");
            const required = block.permutation.getState("peach:required");

            if (item && item.typeId.startsWith("zz:cancel_block")) {
                e.cancel = true;
            }

            // ---------------- When empty ----------------
            if (recipe === "0" && mode === "0" && required === "0") {
                if (item.typeId === "minecraft:potion") {
                    Utils.setBlockState(block, "peach:mode", "2");
                    Utils.playSound(block, "cauldron.fillwater");
                    Utils.giveItem(player, "minecraft:glass_bottle");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "minecraft:water_bucket") {
                    Utils.setBlockState(block, "peach:mode", "2");
                    Utils.playSound(block, "cauldron.fillwater");
                    Utils.giveItem(player, "minecraft:bucket");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:lard") {
                    Utils.setBlockState(block, "peach:mode", "1");
                    Utils.playSound(block, "use.honey_block");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "minecraft:porkchop") {
                    Utils.setBlockState(block, "peach:mode", "5");
                    Utils.playSound(block, "use.honey_block");
                    Utils.consumeItem(player, item);
                    scheduleDoneLard(block);
                    return;
                }
            }

            // ---------------- mode == 1 Lard ----------------
            if (mode === "1" && recipe === "0" && required === "0") {
                if (item.typeId === "peach:onion_rings_raw") {
                    Utils.setBlockState(block, "peach:recipe", "1");
                    Utils.setBlockState(block, "peach:mode", "3");
                    Utils.playSound(block, "extinguish.candle");
                    scheduleDone(block);
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:thighy_raw") {
                    Utils.setBlockState(block, "peach:recipe", "2");
                    Utils.setBlockState(block, "peach:mode", "3");
                    Utils.playSound(block, "extinguish.candle");
                    scheduleDone(block);
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "minecraft:potato") {
                    Utils.setBlockState(block, "peach:recipe", "3");
                    Utils.setBlockState(block, "peach:mode", "3");
                    Utils.playSound(block, "extinguish.candle");
                    scheduleDone(block);
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:dough") {
                    Utils.setBlockState(block, "peach:recipe", "4");
                    Utils.setBlockState(block, "peach:mode", "3");
                    Utils.playSound(block, "extinguish.candle");
                    scheduleDone(block);
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:pastry_mold") {
                    Utils.setBlockState(block, "peach:required", "1");
                    Utils.playSound(block, "extinguish.candle");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:skimmer") {
                    Utils.setBlockState(block, "peach:mode", "0");
                    Utils.playSound(block, "extinguish.candle");
                    Utils.giveItem(player, "peach:lard");
                    return;
                }
            }

            // ---------------- mode == 2 Water ----------------
            if (mode === "2" && recipe === "0" && required === "0") {
                if (item.typeId === "peach:rice") {
                    Utils.setBlockState(block, "peach:recipe", "11");
                    Utils.setBlockState(block, "peach:mode", "3");
                    Utils.playSound(block, "cauldron.fillwater");
                    scheduleDone(block);
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:pasta") {
                    Utils.setBlockState(block, "peach:required", "6");
                    Utils.setBlockState(block, "peach:mode", "2");
                    Utils.playSound(block, "cauldron.fillwater");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "minecraft:potato") {
                    Utils.setBlockState(block, "peach:required", "8");
                    Utils.setBlockState(block, "peach:mode", "2");
                    Utils.playSound(block, "cauldron.fillwater");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "minecraft:bucket") {
                    Utils.setBlockState(block, "peach:mode", "0");
                    Utils.setBlockState(block, "peach:required", "0");
                    Utils.setBlockState(block, "peach:recipe", "0");
                    Utils.playSound(block, "cauldron.fillwater");
                    Utils.giveItem(player, "minecraft:water_bucket");
                    Utils.consumeItem(player, item);
                    return;
                }
            }

            // ---------------- required > 0 ----------------
            if (recipe === "0" && mode === "1" && required === "1") {
                if (item.typeId === "peach:banana") {
                    Utils.setBlockState(block, "peach:required", "2");
                    Utils.playSound(block, "extinguish.candle");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "minecraft:cooked_chicken") {
                    Utils.setBlockState(block, "peach:required", "3");
                    Utils.playSound(block, "extinguish.candle");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:camembert") {
                    Utils.setBlockState(block, "peach:required", "4");
                    Utils.playSound(block, "extinguish.candle");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:guava") {
                    Utils.setBlockState(block, "peach:required", "5");
                    Utils.playSound(block, "extinguish.candle");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "peach:mozzarella") {
                    Utils.setBlockState(block, "peach:recipe", "6");
                    Utils.setBlockState(block, "peach:mode", "3");
                    Utils.playSound(block, "extinguish.candle");
                    scheduleDone(block);
                    Utils.consumeItem(player, item);
                    return;
                }
                if (item.typeId === "minecraft:cooked_beef") {
                    Utils.setBlockState(block, "peach:recipe", "5");
                    Utils.setBlockState(block, "peach:mode", "3");
                    Utils.playSound(block, "extinguish.candle");
                    scheduleDone(block);
                    Utils.consumeItem(player, item);
                    return;
                }
            }

            if (recipe === "0" && mode === "1" && required === "2" && item.typeId === "peach:chocolate_powder") {
                Utils.setBlockState(block, "peach:required", "0");
                Utils.setBlockState(block, "peach:recipe", "7");
                Utils.setBlockState(block, "peach:mode", "3");
                Utils.playSound(block, "extinguish.candle");
                scheduleDone(block);
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "1" && required === "3" && item.typeId === "minecraft:cooked_beef") {
                Utils.setBlockState(block, "peach:required", "0");
                Utils.setBlockState(block, "peach:recipe", "8");
                Utils.setBlockState(block, "peach:mode", "3");
                Utils.playSound(block, "extinguish.candle");
                scheduleDone(block);
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "1" && required === "4" && item.typeId === "peach:chicken_thigh_cooked") {
                Utils.setBlockState(block, "peach:required", "0");
                Utils.setBlockState(block, "peach:recipe", "9");
                Utils.setBlockState(block, "peach:mode", "3");
                Utils.playSound(block, "extinguish.candle");
                scheduleDone(block);
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "1" && required === "5" && item.typeId === "peach:mozzarella") {
                Utils.setBlockState(block, "peach:required", "0");
                Utils.setBlockState(block, "peach:recipe", "10");
                Utils.setBlockState(block, "peach:mode", "3");
                Utils.playSound(block, "extinguish.candle");
                scheduleDone(block);
                Utils.consumeItem(player, item);
                return;
            }

            if (recipe === "0" && mode === "2" && required === "6" && ["minecraft:egg","minecraft:blue_egg","minecraft:brown_egg"].includes(item.typeId)) {
                Utils.setBlockState(block, "peach:required", "7");
                Utils.setBlockState(block, "peach:recipe", "0");
                Utils.setBlockState(block, "peach:mode", "2");
                Utils.playSound(block, "extinguish.candle");
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "2" && required === "7" && item.typeId === "minecraft:dried_kelp") {
                Utils.setBlockState(block, "peach:required", "0");
                Utils.setBlockState(block, "peach:recipe", "12");
                Utils.setBlockState(block, "peach:mode", "3");
                Utils.playSound(block, "extinguish.candle");
                scheduleDone(block);
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "2" && required === "8" && item.typeId === "peach:onion") {
                Utils.setBlockState(block, "peach:required", "9");
                Utils.setBlockState(block, "peach:recipe", "0");
                Utils.setBlockState(block, "peach:mode", "2");
                Utils.playSound(block, "extinguish.candle");
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "2" && required === "9" && item.typeId === "peach:garlic") {
                Utils.setBlockState(block, "peach:required", "10");
                Utils.setBlockState(block, "peach:recipe", "0");
                Utils.setBlockState(block, "peach:mode", "2");
                Utils.playSound(block, "extinguish.candle");
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "2" && required === "10" && item.typeId === "peach:bell_pepper") {
                Utils.setBlockState(block, "peach:required", "11");
                Utils.setBlockState(block, "peach:recipe", "0");
                Utils.setBlockState(block, "peach:mode", "2");
                Utils.playSound(block, "extinguish.candle");
                Utils.consumeItem(player, item);
                return;
            }
            if (recipe === "0" && mode === "2" && required === "11" && item.typeId === "minecraft:carrot") {
                Utils.setBlockState(block, "peach:required", "0");
                Utils.setBlockState(block, "peach:recipe", "13");
                Utils.setBlockState(block, "peach:mode", "3");
                Utils.playSound(block, "extinguish.candle");
                scheduleDone(block);
                Utils.consumeItem(player, item);
                return;
            }
            // ---------------- mode == 4 outputs ----------------
            if (mode === "4") {
                const giveAndReset = (giveId, setModeTo1 = true) => {
                    Utils.giveItem(player, giveId);
                    Utils.setBlockState(block, "peach:recipe", "0");
                    Utils.setBlockState(block, "peach:required", "0");
                    Utils.setBlockState(block, "peach:mode", "0");
                    Utils.playSound(block, "random.pop");
                    if (setModeTo1) Utils.setBlockState(block, "peach:mode", "1");
                };

                if (recipe === "1" && item.typeId === "peach:skimmer") { giveAndReset("peach:onion_rings"); return; }
                if (recipe === "2" && item.typeId === "peach:skimmer") { giveAndReset("peach:thighy"); return; }
                if (recipe === "3" && item.typeId === "peach:skimmer") { giveAndReset("peach:fries"); return; }
                if (recipe === "4" && item.typeId === "peach:skimmer") { giveAndReset("peach:donut"); return; }
                if (recipe === "5" && item.typeId === "peach:skimmer") { giveAndReset("peach:fried_pastry_beef"); return; }
                if (recipe === "6" && item.typeId === "peach:skimmer") { giveAndReset("peach:fried_pastry_cheese"); return; }
                if (recipe === "7" && item.typeId === "peach:skimmer") { giveAndReset("peach:fried_pastry_banana_and_chocolate"); return; }
                if (recipe === "8" && item.typeId === "peach:skimmer") { giveAndReset("peach:fried_pastry_miscellaneous"); return; }
                if (recipe === "9" && item.typeId === "peach:skimmer") { giveAndReset("peach:fried_pastry_chiken"); return; }
                if (recipe === "10" && item.typeId === "peach:skimmer") { giveAndReset("peach:fried_pastry_cheese_and_guava"); return; }

                if (recipe === "11" && item.typeId === "minecraft:bowl") {
                    Utils.giveItem(player, "peach:bowl_rice");
                    Utils.setBlockState(block, "peach:recipe", "0");
                    Utils.setBlockState(block, "peach:required", "0");
                    Utils.setBlockState(block, "peach:mode", "0");
                    Utils.playSound(block, "random.pop");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (recipe === "12" && item.typeId === "minecraft:bowl") {
                    Utils.giveItem(player, "peach:bowl_lamen");
                    Utils.setBlockState(block, "peach:recipe", "0");
                    Utils.setBlockState(block, "peach:required", "0");
                    Utils.setBlockState(block, "peach:mode", "0");
                    Utils.playSound(block, "random.pop");
                    Utils.consumeItem(player, item);
                    return;
                }
                if (recipe === "13" && item.typeId === "minecraft:bowl") {
                    Utils.giveItem(player, "peach:bowl_vegetables_stew");
                    Utils.setBlockState(block, "peach:recipe", "0");
                    Utils.setBlockState(block, "peach:required", "0");
                    Utils.setBlockState(block, "peach:mode", "0");
                    Utils.playSound(block, "random.pop");
                    Utils.consumeItem(player, item);
                    return;
                }
            }
            return;
        },
    });
});
