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

const recipes = [
    {
        output: "bagel",
        ingredients: [
            { item: "ww_dx:dough", count: 6 },
        ],
    },
    {
        output: "poppy_seed_bagel",
        ingredients: [
            { item: "ww_dx:bagel", count: 1 },
            { item: "ww_dx:poppy_seeds", count: 1 }
        ],
    },
    {
        output: "banana_bread",
        ingredients: [
            { item: "ww_dx:banana", count: 1 },
            { item: "ww_dx:flour", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "blueberry_muffin",
        ingredients: [
            { item: "ww_dx:blueberry", count: 1 },
            { item: "ww_dx:flour", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "raisin_cookie",
        ingredients: [
            { item: "minecraft:wheat", count: 2 },
            { item: "ww_dx:purple_raisins", count: 1 },
        ],
    },
    {
        output: "strawberry_tart",
        ingredients: [
            { item: "ww_dx:strawberry", count: 1 },
            { item: "ww_dx:flour", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "berry_tart",
        ingredients: [
            { item: "ww_dx:blueberry", count: 1 },
            { item: "ww_dx:blackberry", count: 1 },
            { item: "ww_dx:flour", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "pecan_pie",
        ingredients: [
            { item: "ww_dx:pecan_nuts", count: 1 },
            { item: "minecraft:sugar", count: 1 },
            { item: "minecraft:egg", count: 1 },
        ],
    },
    {
        output: "zucchini_bread",
        ingredients: [
            { item: "ww_dx:zucchini", count: 1 },
            { item: "ww_dx:flour", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "gingerbread_cookie",
        ingredients: [
            { item: "minecraft:wheat", count: 2 },
            { item: "ww_dx:ginger", count: 1 },
        ],
    },
    {
        output: "donut",
        ingredients: [
            { item: "ww_dx:dough", count: 3 },
            { item: "minecraft:sugar", count: 3 },
        ],
    },
    {
        output: "apple_pie",
        ingredients: [
            { item: "minecraft:apple", count: 1 },
            { item: "minecraft:sugar", count: 1 },
            { item: "minecraft:egg", count: 1 },
        ],
    },
    {
        output: "cinnamon_roll",
        ingredients: [
            { item: "ww_dx:cinnamon", count: 1 },
            { item: "ww_dx:donut", count: 1 },
        ],
    },
    {
        output: "peach_cobbler",
        ingredients: [
            { item: "ww_dx:peach", count: 1 },
            { item: "ww_dx:dough", count: 1 },
            { item: "minecraft:sugar", count: 1 },
        ],
    },
    {
        output: "carrot_cake",
        ingredients: [
            { item: "minecraft:carrot", count: 1 },
            { item: "minecraft:cake", count: 1 },
        ],
    },
    {
        output: "roasted_chestnuts",
        ingredients: [
            { item: "ww_dx:chestnuts", count: 1 },
            { item: "ww_dx:salt", count: 1 },
        ],
    },
    {
        output: "garlic_bread",
        ingredients: [
            { item: "ww_dx:garlic", count: 1 },
            { item: "ww_dx:butter", count: 1 },
            { item: "minecraft:bread", count: 1 },
        ],
    },
    {
        output: "stuffed_bell_peppers",
        ingredients: [
            { item: "ww_dx:red_bell_pepper", count: 1 },
            { item: "ww_dx:rice", count: 1 },
            { item: "ww_dx:tomato_sauce", count: 1 },
            { item: "ww_dx:raw_ground_beef", count: 1 },
        ],
    },
    {
        output: "shepherds_pie",
        ingredients: [
            { item: "ww_dx:mashed_potatoes", count: 1 },
            { item: "ww_dx:peas", count: 1 },
            { item: "minecraft:beef", count: 1 },
        ],
    },
    {
        output: "pizza",
        ingredients: [
            { item: "ww_dx:dough", count: 1 },
            { item: "ww_dx:tomato_sauce", count: 1 },
            { item: "ww_dx:cheese", count: 1 },
            { item: "ww_dx:pepperoni", count: 1 },
            { item: "ww_dx:basil", count: 1 },
            { item: "ww_dx:pepper", count: 1 },
        ],
    },
    {
        output: "quiche",
        ingredients: [
            { item: "ww_dx:dough", count: 1 },
            { item: "ww_dx:cream", count: 1 },
            { item: "ww_dx:spinach", count: 1 },
            { item: "minecraft:egg", count: 1 },
        ],
    },
    {
        output: "lasagna",
        ingredients: [
            { item: "ww_dx:noodles", count: 1 },
            { item: "ww_dx:cheese", count: 1 },
            { item: "ww_dx:tomato_sauce", count: 1 },
            { item: "minecraft:beef", count: 1 },
        ],
    },
    {
        output: "chicken_pot_pie",
        ingredients: [
            { item: "minecraft:chicken", count: 1 },
            { item: "minecraft:carrot", count: 1 },
            { item: "ww_dx:dough", count: 1 },
            { item: "ww_dx:peas", count: 1 },
        ],
    },
    {
        output: "churro",
        ingredients: [
            { item: "ww_dx:dough", count: 1 },
            { item: "minecraft:sugar", count: 1 },
            { item: "ww_dx:cinnamon", count: 1 },
        ],
    },
    {
        output: "cauliflower_pizza",
        ingredients: [
            { item: "ww_dx:cauliflower", count: 1 },
            { item: "ww_dx:tomato_sauce", count: 1 },
            { item: "ww_dx:cheese", count: 1 },
        ],
    },
];


const oven = new Map(); 


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


    if (oven.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);
                        }
                    }
                }
            }

      

            
            oven.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]")

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

            return; 
        } else {
          
        }
    }


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

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

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

            processOvenInventory(entity);
        }
    }
}

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