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

const store_npc = "raz:dss_foodstallstories_refrigerator"; // Entity identifier

// Main Store Menu
function openStore(player) {
    const title = "§l§eIngredients";
    const form = new ActionFormData()
        .title(title)
        .body("§lSelect the item you want.")
        .button("Eggs", "textures/items/egg.png")
        .button("Flour", "textures/items/flour.png")
        .button("Onions", "textures/items/onions.png")
        .button("Butter", "textures/items/butter.png")
        .button("Rice", "textures/items/rice.png")
        .button("More", "textures/items/more.png");

    form.show(player).then((r) => {
        if (r.selection === 0) giveItem(player, "minecraft:egg", 5);
        else if (r.selection === 1) giveItem(player, "bow:flour", 5);
        else if (r.selection === 2) giveItem(player, "item_gem:onions", 5);
        else if (r.selection === 3) giveItem(player, "item_gem:butter", 5);
        else if (r.selection === 4) giveItem(player, "item_gem:rice", 5);
        else if (r.selection === 5) openStorePage2(player);
    });
}

// Second Page of Store
function openStorePage2(player) {
    const title = "§l§eIngredients";
    const form = new ActionFormData()
        .title(title)
        .body("§lSelect the item you want.")
        .button("Coffee Liqueur", "textures/items/coffee_liqueur.png")
        .button("Mascarpone Cream", "textures/items/mascarpone_cream.png")
        .button("Cocoa Powder", "textures/items/cocoa_powder.png")
        .button("Ground Coffee", "textures/items/ground_coffee.png")
        .button("Milk", "textures/items/bucket_milk.png")
        .button("More", "textures/items/more.png");

    form.show(player).then((r) => {
        if (r.selection === 0) giveItem(player, "item_gem:coffee_liqueur", 5);
        else if (r.selection === 1) giveItem(player, "item_gem:mascarpone_cream", 5);
        else if (r.selection === 2) giveItem(player, "item_gem:cocoa_powder", 5);
        else if (r.selection === 3) giveItem(player, "item_gem:ground_coffee", 5);
        else if (r.selection === 4) giveItem(player, "minecraft:milk_bucket", 5);
        else if (r.selection === 5) openStorePage3(player);
    });
}

// Third Page of Store
function openStorePage3(player) {
    const title = "§l§eIngredients";
    const form = new ActionFormData()
        .title(title)
        .body("§lSelect the item you want.")
        .button("Tomato Sauce", "textures/items/tomato_sauce.png")
        .button("Mozarella", "textures/items/mozarella.png")
        .button("Basil Leaves", "textures/items/basil_leaves.png")
        .button("Cheese", "textures/items/cheese.png")
        .button("Beef", "textures/items/beef.png")
        .button("More", "textures/items/more.png");

    form.show(player).then((r) => {
        if (r.selection === 0) giveItem(player, "item_gem:tomato_sauce", 5);
        else if (r.selection === 1) giveItem(player, "item_gem:mozarella", 5);
        else if (r.selection === 2) giveItem(player, "item_gem:basil_leaves", 5);
        else if (r.selection === 3) giveItem(player, "bow:cheese", 5);
        else if (r.selection === 4) giveItem(player, "bow:beef", 5);
        else if (r.selection === 5) openStorePage4(player);
    });
}

// Fourth Page of Store
function openStorePage4(player) {
    const title = "§l§eIngredients";
    const form = new ActionFormData()
        .title(title)
        .body("§lSelect the item you want.")
        .button("Dough Mixture Ingredients", "textures/items/dough_mixture.png")
        .button("Sugar", "textures/items/granulated_sugar.png")
        .button("More", "textures/items/more.png");

    form.show(player).then((r) => {
        if (r.selection === 0) [
            giveItem(player, "bow:olive_oil ", 5),
            giveItem(player, "bow:granulated_sugar", 5),
            giveItem(player, "bow:salt", 5),
            giveItem(player, "bow:cup_water", 5),
            giveItem(player, "bow:instant_yeast", 5),
            giveItem(player, "bow:flour", 5)];
        else if (r.selection === 1) giveItem(player, "bow:granulated_sugar", 5);
        else if (r.selection === 5) openStore(player);
    });
}

// Generic function to give items
function giveItem(player, item, amount) {
    player.runCommandAsync(`give @s ${item} ${amount}`);
}

// Interaction Event for NPC Store
world.beforeEvents.playerInteractWithEntity.subscribe((data) => {
    const player = data.player;
    const targetEntity = data.target;

    if (targetEntity.typeId === store_npc) {
        system.run(() => openStore(player));
    }
});
