import { system, world } from "@minecraft/server";

const recipes = [
    {
        output: "bbq_sauce",
        ingredients: [
            { item: "ww_dx:tomato_sauce", count: 1 },
            { item: "ww_dx:vinegar", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "berry_smoothie",
        ingredients: [
            { item: "ww_dx:strawberry", count: 1 },
            { item: "ww_dx:blueberry", count: 1 },
            { item: "ww_dx:yogurt", count: 1 },
        ],
    },
    {
        output: "blueberry_smoothie",
        ingredients: [
            { item: "ww_dx:blueberry", count: 1 },
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "minecraft:ice", count: 1 },
        ],
    },
    {
        output: "blueberry_yogurt",
        ingredients: [
            { item: "ww_dx:yogurt", count: 1 },
            { item: "ww_dx:blueberry", count: 1 },
        ],
    },
    {
        output: "chocolate_milkshake",
        ingredients: [
            { item: "minecraft:ice", count: 1 },
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "ww_dx:chocolate", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "cream",
        ingredients: [
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "ww_dx:butter", count: 1 },
        ],
    },
    {
        output: "cucumber_water",
        ingredients: [
            { item: "ww_dx:cucumber", count: 1 },
            { item: "minecraft:water_bucket", count: 1 },
        ],
    },
    {
        output: "hot_chocolate",
        ingredients: [
            { item: "minecraft:cocoa_beans", count: 1 },
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "iced_coffee",
        ingredients: [
            { item: "ww_dx:coffee_beans", count: 1 },
            { item: "minecraft:ice", count: 1 },
            { item: "minecraft:sugar", count: 1 },
            { item: "ww_dx:cream", count: 1 },
        ],
    },
    {
        output: "lemonade",
        ingredients: [
            { item: "ww_dx:lemon", count: 1 },
            { item: "minecraft:water_bucket", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "mango_smoothie",
        ingredients: [
            { item: "ww_dx:mango", count: 1 },
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "minecraft:ice", count: 1 },
        ],
    },
    {
        output: "milkshake",
        ingredients: [
            { item: "minecraft:ice", count: 1 },
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "pumpkin_spice_latte",
        ingredients: [
            { item: "ww_dx:coffee_beans", count: 1 },
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "minecraft:pumpkin_seeds", count: 1 },
        ],
    },
    {
        output: "raspberry_yogurt",
        ingredients: [
            { item: "ww_dx:yogurt", count: 1 },
            { item: "ww_dx:raspberry", count: 1 },
        ],
    },
    {
        output: "strawberry_smoothie",
        ingredients: [
            { item: "ww_dx:strawberry", count: 1 },
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "minecraft:ice", count: 1 },
        ],
    },
    {
        output: "vanilla_yogurt",
        ingredients: [
            { item: "ww_dx:yogurt", count: 1 },
            { item: "ww_dx:vanilla", count: 1 },
        ],
    },
    {
        output: "yogurt",
        ingredients: [
            { item: "minecraft:milk_bucket", count: 1 },
            { item: "ww_dx:cream", count: 1 },
        ],
    },
];





const blender = new Map();

function processBlenderInventory(entity) {
    const id = entity.id;

    if (blender.get(id)?.isProcessing) {

        return;
    }

    const inventoryComponent = entity.getComponent("minecraft:inventory");

    if (!inventoryComponent || !inventoryComponent.container) {

        return;
    }

    const inventory = inventoryComponent.container;

    for (const recipe of recipes) {

        let matches = true;

        const ingredientsMap = new Map(
            recipe.ingredients.map((ingredient) => [ingredient.item, ingredient.count])
        );

        for (let i = 0; i < inventory.size; i++) {
            const item = inventory.getItem(i);
            if (item) {
                if (ingredientsMap.has(item.typeId)) {
                    const remainingCount = ingredientsMap.get(item.typeId) - item.amount;
                    if (remainingCount <= 0) {
                        ingredientsMap.delete(item.typeId);
                    } else {
                        ingredientsMap.set(item.typeId, remainingCount);
                    }
                } else {

                    matches = false;
                    break;
                }
            }
        }

        if (matches && ingredientsMap.size === 0) {
            entity.runCommandAsync("summon ww_dx:processing_particle ~ ~1 ~")

            for (const ingredient of recipe.ingredients) {
                let remainingToRemove = ingredient.count;

                for (let i = 0; i < inventory.size; i++) {
                    const item = inventory.getItem(i);
                    if (item && item.typeId === ingredient.item) {
                        if (item.amount > remainingToRemove) {
                            item.amount -= remainingToRemove;
                            inventory.setItem(i, item);
                            break;
                        } else {
                            remainingToRemove -= item.amount;
                            inventory.setItem(i, undefined);
                        }
                    }
                }
            }


            blender.set(id, { isProcessing: true });
            system.runTimeout(() => {
                if (!entity.isValid()) return;
                const { x, y, z } = entity.location;
                entity.runCommandAsync(`loot spawn ${x} ${y + 1} ${z} loot ${recipe.output}`);
                entity.runCommandAsync("event entity @e[type=ww_dx:processing_particle,r=1] ww_dx:despawn")
                entity.runCommandAsync("playsound ww_dx:finish_cooking @a[r=5]")

                blender.set(id, { isProcessing: false });
            }, 200);

            return;
        } else {

        }
    }

}
let worldInitialize = false;
world.afterEvents.playerSpawn.subscribe((event) => {
    worldInitialize = true;

})
function checkBlender() {
    if (worldInitialize) {
        const currentDimension = world.getDimension(world.getPlayers()[0].dimension.id);

        for (const entity of currentDimension.getEntities()) {
            if (entity.typeId === "ww_dx:blender") {
                if (!blender.has(entity.id)) {
                    blender.set(entity.id, { isProcessing: false });

                }

                processBlenderInventory(entity);
            }
        }
    }
}

system.runInterval(() => {
    checkBlender();
}, 20); 
