import * as mc from "@minecraft/server";
import * as ui from "@minecraft/server-ui";
import * as config from "./sgs_cake_config";

const VERSION = "1.0.0";
const MAX_SEARCH_RESULTS = 5;
const SOUND_TURN_PAGE = "item.book.page_turn";
const SOUND_OPTIONS = { pitch: 1.0, volume: 5.0, };

export const MENUS = [
    //MAIN MENU
    {
        menuID: "sgs_cake:main",
        title: "Main Menu",
        body: "Welcome to §6Cakes & Bakes§r!\n\nRead the Introduction section to get started.",
        buttons: [
            {
                title: "Search",
                icon: "textures/sgs/cake/items/icons/icon_search",
                connectID: "sgs_cake:search"
            },
            {
                title: "Introduction",
                icon: "textures/sgs/cake/items/icons/icon_basics",
                connectID: "sgs_cake:tutorial"
            },
            {
                title: "Chapters",
                icon: "textures/sgs/cake/items/icons/icon_chapters",
                connectID: "sgs_cake:chapters"
            },
            {
                title: "Settings",
                icon: "textures/sgs/cake/items/icons/icon_settings",
                connectID: "sgs_cake:config"
            },
            {
                title: "About",
                icon: "textures/sgs/cake/items/icons/icon_about",
                connectID: "sgs_cake:about"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //TUTORIAL MENU

    {
        menuID: "sgs_cake:tutorial",
        title: "Introduction",
        body: "To get started with §uCakes & Bakes§r, craft these basic tools first:\n\n1. Using a regular §6Crafting Table§r, craft the following items:\n  - Baker's Workbench\n  - Baker's Recipes\n\nPut down the §6Baker's Workbench§r and place the §6Baker's Recipe§r on top of it. If done correctly, a glowing effect will appear and your Cakes & Bakes Workbench is ready!\n\n2. At the §6Baker's Workbench§r, look up the recipe for the §6Baker's Market Stall§r and get crafting. Placing it will spawn in a §uVillage Baker§r that will sell new §6crop seeds§r and §6saplings§r. With these, set up a small farm to grow some ingredients.\n\nNow that you have your workbench ready and ingredients growing, check out the other chapters to set up your very own bakery and start making cakes!",
        buttons: [
            {
                title: "Build the Bakery",
                icon: "textures/sgs/cake/items/icons/icon_bakery",
                connectID: "sgs_cake:bakery"
            },
            {
                title: "Crafting Decorations",
                icon: "textures/sgs/cake/items/icons/icon_deco",
                connectID: "sgs_cake:decoration"
            },
            {
                title: "Crops",
                icon: "textures/sgs/cake/items/icons/icon_crops",
                connectID: "sgs_cake:crops"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //CHAPTERS MENU

    {
        menuID: "sgs_cake:chapters",
        title: "Chapters",
        body: "Choose a §6Cakes & Bakes§r chapter:\n\n",
        buttons: [
            {
                title: "Building the Bakery",
                icon: "textures/sgs/cake/items/icons/icon_bakery",
                connectID: "sgs_cake:bakery"
            },
            {
                title: "Using the Bakery",
                icon: "textures/sgs/cake/items/icons/icon_workbench",
                connectID: "sgs_cake:stations"
            },
            {
                title: "Decorations & Furniture",
                icon: "textures/sgs/cake/items/icons/icon_deco",
                connectID: "sgs_cake:decoration"
            },
            {
                title: "Placeable Food",
                icon: "textures/sgs/cake/items/icons/icon_display",
                connectID: "sgs_cake:display"
            },
            {
                title: "Kitchen Helpers",
                icon: "textures/sgs/cake/items/tools/item_bottle_glass_empty",
                connectID: "sgs_cake:kitchen_helpers"
            },
            {
                title: "Crops",
                icon: "textures/sgs/cake/items/icons/icon_crops",
                connectID: "sgs_cake:crops"
            },
            {
                title: "Giant Cake Blocks",
                icon: "textures/sgs/cake/items/icons/icon_cake_block",
                connectID: "sgs_cake:giant_cake"
            },
            {
                title: "Raccoon",
                icon: "textures/sgs/cake/items/item_spawn_egg_raccoon",
                connectID: "sgs_cake:raccoon"
            },
            {
                title: "Compatibility",
                icon: "textures/sgs/cake/items/misc/item_chop_board",
                connectID: "sgs_cake:compat"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //BAERY MENU

    {
        menuID: "sgs_cake:bakery",
        title: "Building the Bakery",
        body: "Before you can get started on your first recipe, you will need more §uWorkstations§r. All §uWorkstations§r can be created using the §6Baker's Workbench§r. Many Workstations also use a base and a top block, similar to the Baker's Workbench.\n\n§uBaker's Prep Table§r - Base Block\n  - §6Mixing Bowl§r\n  - §6Pastry Board§r\n  - §6Quartz Slab§r\n  - §6Blender§r\n\n§uBaker's Stove§r - Base Block\n  - §6Copper Cauldron§r\n  - §6Tea Pot§r\n  - §6Pan§r\n\n§uNo base block required§r\n  - §6Millstone§r\n  - §6Fruit Press§r\n  - §6Ice Churn§r\n  - §6Butter Churn§r\n\nFinally the §6Baking Oven§r is created by placing 8 Oven Brick in a §u2x2x2 structure§r, forming a large cube.",
        buttons: [
            {
                title: "Using the Bakery",
                icon: "textures/sgs/cake/items/icons/icon_workbench",
                connectID: "sgs_cake:stations"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //STATIONS MENU

    {
        menuID: "sgs_cake:stations",
        title: "Using the Bakery",
        body: "With your Workstations set up, it's time to start baking! In Cakes & Bakes there are two types of Workstations, §uCrafting Stations§r and §uInteractive Stations§r.\n\n§uCrafting Stations§r work like a normal crafting table. Use the §ucrafting grid§r to combine items and look up recipes.\n\n§uInteractive Stations§r work by interacting with the required items of the recipe you wish to create. You can look up available recipes by interacting with the Workstation and this §uGuidebook§r in hand.\n\nIf you put in the wrong ingredients you can §ureset§r the station via §6sneak + interact§r\n\nSome recipes require an extra item to pick up the finished product, this will be shown on top of the station. For example, the §6Tea Pot§r requires a §uCeramic Cup§r to grab a freshly brewed tea.\n\n§uInteractive stations§r:\n  - §6Mixing Bowl§r\n  - §6Blender§r\n  - §6Copper Cauldron§r\n  - §6Fruit Press§r\n  - §6Tea Pot§r\n  - §6Millstone§r\n  - §6Ice Churn§r\n  - §6Butter Churn§r\n  - §6Pan§r\n\nYou can also look up the recipes for each of these stations right here:",
        buttons: [
            {
                title: "Recipe Lookup",
                icon: "textures/sgs/cake/items/icons/icon_recipes",
                connectID: "sgs_cake:recipes"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //STATIONS MENU

    {
        menuID: "sgs_cake:kitchen_helpers",
        title: "Kitchen Helpers",
        body: "Craft these items using the Baker's Workbench. \n\n§uBottles§r\nAre used to handle liquids during baking. You can fill them with §6water§r and use them to §6milk§r cows directly. Bottles always stack up to §664§r items.\n\n§uWater Barrel§r\nContains an infinite source of water in one convenient block. Use §6Buckets§r or §6Bottles§r to scoop out water. You can also empty full containers back into the barrel.",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //RECIPES MENU

    {
        menuID: "sgs_cake:recipes",
        title: "Station Recipe Lookup",
        body: "",
        buttons: [
            {
                title: "Fruit Press",
                icon: "textures/sgs/cake/items/icons/icon_juice_press",
                connectID: "sgs_cake:fruit_press"
            },
            {
                title: "Millstone",
                icon: "textures/sgs/cake/items/icons/icon_millstone",
                connectID: "sgs_cake:millstone"
            },
            {
                title: "Butter Churn",
                icon: "textures/sgs/cake/items/icons/icon_butter_churn",
                connectID: "sgs_cake:butter_churn"
            },
            {
                title: "Mixing Bowls",
                icon: "textures/sgs/cake/items/misc/item_bowl_mix_iron",
                connectID: "sgs_cake:mixing_bowls"
            },
            {
                title: "Copper Cauldron",
                icon: "textures/sgs/cake/items/misc/item_couldron_copper",
                connectID: "sgs_cake:copper_cauldron"
            },
            {
                title: "Teapot",
                icon: "textures/sgs/cake/items/misc/item_teapot_copper",
                connectID: "sgs_cake:teapot"
            },
            {
                title: "Blender",
                icon: "textures/sgs/cake/items/misc/item_blender",
                connectID: "sgs_cake:blender"
            },
            {
                title: "Ice Cream Churn",
                icon: "textures/sgs/cake/items/icons/icon_icecream_churn",
                connectID: "sgs_cake:ice_cream_churn"
            },
            {
                title: "Pan",
                icon: "textures/sgs/cake/items/icons/icon_pan",
                connectID: "sgs_cake:pan"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:stations"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //FRUIT PRESS MENU

    {
        menuID: "sgs_cake:fruit_press",
        title: "Fruit Press",
        body: "Use the §6Fruit Press§r to squeeze juice from fruits. Recipes require an §6Empty Bottle§r to pick up.\n\n",
        buttons: [
            {
                title: "Juices",
                icon: "textures/sgs/cake/items/food/item_strawberry_juice",
                connectID: "sgs_cake:recipe_fruit_press"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //MILLSTONE MENU

    {
        menuID: "sgs_cake:millstone",
        title: "Millstone",
        body: "The §6Millstone§r will grind down §61§r Item into powder and paste ingredients.\n\n",
        buttons: [
            {
                title: "Powders",
                icon: "textures/sgs/cake/items/food/item_flour",
                connectID: "sgs_cake:powders"
            },
            {
                title: "Ground Nuts",
                icon: "textures/sgs/cake/items/food/item_ground_almonds",
                connectID: "sgs_cake:ground_nuts"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //POWDERS MENU

    {
        menuID: "sgs_cake:powders",
        title: "Millstone - Powders",
        body: "",
        recipe_array: config.MILLSTONE_RECIPES,
        type_category: "generic",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:millstone"
            }
        ],
        type: "recipe",
        openOnBlock: ""
    },

    //GROUND NUTS MENU

    {
        menuID: "sgs_cake:ground_nuts",
        title: "Millstone - Ground Nuts",
        body: "",
        recipe_array: config.MILLSTONE_RECIPES,
        type_category: "nut",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:millstone"
            }
        ],
        type: "recipe",
        openOnBlock: ""
    },

    //BUTTER CHURN MENU

    {
        menuID: "sgs_cake:butter_churn",
        title: "Butter Churn",
        //body: "The §6Butter Churn§r Recipes pop out of the machine on completion."
        body: "Turns cream into Butter. Recipe pops out of the machine on completion.\n\n §6Butter (3)§r:\n-1x Cream Bottle",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //MIXING BOWL MENU

    {
        menuID: "sgs_cake:mixing_bowls",
        title: "Mixing Bowls",
        body: "Use the §6Mixing Bowl§r to combine §63§r cold ingredients. Needs to be placed on a §6Prep Table§r to work.\n\n",
        buttons: [
            {
                title: "Ingredients",
                icon: "textures/sgs/cake/items/food/item_yeast",
                connectID: "sgs_cake:misc"
            },
            {
                title: "Doughs",
                icon: "textures/sgs/cake/items/food/item_dough",
                connectID: "sgs_cake:doughs"
            },
            {
                title: "Fillings",
                icon: "textures/sgs/cake/items/food/item_strawberry_filling",
                connectID: "sgs_cake:fillings"
            },
            {
                title: "Jellys",
                icon: "textures/sgs/cake/items/food/item_apple_jelly",
                connectID: "sgs_cake:jellys"
            },
            {
                title: "Batters",
                icon: "textures/sgs/cake/items/food/item_birthday_cake_batter",
                connectID: "sgs_cake:batters"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //COPPER CAULDRON MENU

    {
        menuID: "sgs_cake:copper_cauldron",
        title: "Copper Cauldron",
        body: "Use the §6Copper Cauldron§r to melt §62§r ingredients together. Needs to be placed on a §6Stove§r to work.\n\n",
        buttons: [
            {
                title: "Ingredients",
                icon: "textures/sgs/cake/items/food/item_toffee",
                connectID: "sgs_cake:cauldron_misc"
            },
            {
                title: "Jams",
                icon: "textures/sgs/cake/items/food/item_strawberry_jam",
                connectID: "sgs_cake:jams"
            },
            {
                title: "Glazes",
                icon: "textures/sgs/cake/items/food/item_strawberry_glaze",
                connectID: "sgs_cake:glazes"
            },
            {
                title: "Candy",
                icon: "textures/sgs/cake/items/food/item_apple_candy",
                connectID: "sgs_cake:candy"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //BLENDER MENU

    {
        menuID: "sgs_cake:blender",
        title: "Blender",
        body: "Use the §6Blender§r to mix §62§r ingredients. Recipes require an §6Empty Bottle§r to pick up. Needs to be placed on a §6Prep Table§r to work.\n\n",
        buttons: [
            {
                title: "Ingredients",
                icon: "textures/sgs/cake/items/food/item_cream_bottle",
                connectID: "sgs_cake:blender_milk"
            },
            {
                title: "Milkshakes",
                icon: "textures/sgs/cake/items/food/item_strawberry_milkshake",
                connectID: "sgs_cake:milkshakes"
            },
            {
                title: "Spritzers",
                icon: "textures/sgs/cake/items/food/item_apple_spritzer",
                connectID: "sgs_cake:spritzers"
            },
            {
                title: "Slushies",
                icon: "textures/sgs/cake/items/food/item_banana_slushy",
                connectID: "sgs_cake:slushies"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //ICE CREAM CHURN MENU

    {
        menuID: "sgs_cake:ice_cream_churn",
        title: "Ice Cream Churn",
        body: "The §6Ice Cream Churn§r freezes §62§r ingredients. Recipes pop out of the machine on completion.\n\n",
        buttons: [
            {
                title: "Ice Creams",
                icon: "textures/sgs/cake/items/food/item_strawberry_ice_cream",
                connectID: "sgs_cake:ice_creams"
            },
            {
                title: "Popsicles",
                icon: "textures/sgs/cake/items/food/item_strawberry_popsicle",
                connectID: "sgs_cake:popsicles"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //PAN MENU

    {
        menuID: "sgs_cake:pan",
        title: "Pan",
        body: "Fries Pancakes! The §6Pan§r can also be used to cook base game ingredients that can be cooked in a furnace. Needs to be placed on a §6Stove§r to work.\n\n",
        buttons: [
            {
                title: "Pan Recipes",
                icon: "textures/sgs/cake/items/food/item_pancake",
                connectID: "sgs_cake:recipe_pan"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //TEAPOT MENU

    {
        menuID: "sgs_cake:teapot",
        title: "Tea Pot",
        body: "Boils Tea and makes a tasty coffee! Pick up hot beverages with a §6Ceramic Cup§r. Needs to be placed on a §6Stove§r to work.\n\n",
        buttons: [
            {
                title: "Tea Pot Recipes",
                icon: "textures/sgs/cake/items/food/item_cup_of_tea",
                connectID: "sgs_cake:recipe_teapot"
            },
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:recipes"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //DISPLAY MENU

    {
        menuID: "sgs_cake:display",
        title: "Placeable Food",
        body: "Food Items that are marked as §uplaceable food§r can be placed down like blocks. §6Pies, Tarts§r and §6Cakes§r can be eaten like a Minecraft cake. You cannot pick them back up once a single slice has been eaten.\n\nAll food items can be displayed in §6display blocks§r that are crafted at the §uFurnishinhg Workbench§r. These include §6Trays, Cabinets, Baskets§r and many more.\n\nTo place a food item into a §6display block§r, just §uInteract§r with it. To retrieve an item use §usneak+interact§r or try to fill the block over capacity. Most display blocks will hold just one item but some can hold up to five.\n\n§uBottles§r and §uJars§r are best displayed in the §6Bottle Rack§r or §6Jar Shelf§r.",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //DECORATIONS MENU

    {
        menuID: "sgs_cake:decoration",
        title: "Decorations & Furniture",
        body: "Decorate your bakery with  §6Cabinets§r and §6Shelving§r. Open the §uCakes & Bakes Workbench§r and craft:§r\n - Baker's Workbench\n - Baker's Furnishing Tools\n\nPlace the §6Furnishing Tools§r on the §6Baker's Workbench§r block to create the §uFurnishing Workbench§r.\nUse it to craft Display Furniture, Chairs, Tables, Awnings, Umbrellas and fitting flooring choices. Some blocks have §uspecial interactions§r:\n\n§u- Display Blocks\nCan §6display§r food items.§r\n\n§u- Chairs§r\nInteract to §6sit.§r\n\n§u- Umbrella§r\nInteract to §6open§r and §6close.§r\n\n§u- Picnic Basket§r\nInteract to open up a storage §6inventory.§r\n\nMost §6wooden furniture and display items§r come in all §uwood variations,§r all §6cloth based decorations§r can be made in all §u16 wool colors.§r\n\nThere are also §6A-boards, Bunting§r and §6Ceramic Cup§r decorations.",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //CROPS MENU

    {
        menuID: "sgs_cake:crops",
        title: "Crops",
        body: "Cakes & Bakes comes with a few new §uCrops§r, all of them can be purchased from the §6Baker's Market Stall.§r\n\n§uBushes§r can be planted on farmland and grow like regular minecraft crops. You can also plant bushes in §6decorative bush pots§r that do not need to be hydrated.\n\n§uFruit trees§r are grown from saplings. Once matured they continously regrow §6fruit§r below their leaf blocks and do not need to be replanted.\nThe §ucinnamon tree§r does not grow fruit. To harvest §6cinnamon§r you need to use an §uaxe§r to shave off its §6bark§r, which will regrow over time.\n\nYou can craft §6Cropboxes§r to store crops more efficiently or just display your harvest in a decorative manner.",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //GIANT CAKE MENU

    {
        menuID: "sgs_cake:giant_cake",
        title: "Giant Cakes",
        body: "Crafting these blocks will allow you to build §6house sized cakes!§r\n\nTo craft §6Giant Cake Blocks§r you will need to bake a whole bunch of §6Plain Cake Layers§r. Combine them with §ufillings§r and §uglazes§r on a Pastry Board.\n\n§6Giant Cake blocks§r of similar type will connect to form a seamless glaze when stacked.\n\nYou can also §ueat the giant cake§r blocks for a considerable boost in §uhunger§r and §usaturation§r by crafting an §6Oversized Fork§r in your workbench and interacting with the §6Cake Blocks§r while holding it.",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //RACCOON MENU

    {
        menuID: "sgs_cake:raccoon",
        title: "Raccoon",
        body: "These little furballs have an insatiable appetite for §usweets§r.\n§6Raccoons§r will grab food from §udisplay blocks§r at any opportunity but can also be tamed with a §6Baker's Hat§r. A tamed raccoon will no longer steal from you but will happily §uaccept any sweets§r you give willingly. In the wild they keep their distance from players and tamed raccoons.\n\n§6Raccoons§r spawn in §utaiga§r and §uforest§r biomes and cannot be bred by players.\n\n§r§l§u(§6!§u)§rSpawning can be toggled in the settings menu.",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //COMPATIBILITY

    {
        menuID: "sgs_cake:compat",
        title: "Compatibility",
        body: "§uCakes & Bakes§r has compatibilities with our farming themed Add-On §2Crops & Farms§r from SomeGameStudio\n\n - both Add-Ons use similar styling\n - Station bases are interchangeable\n - Display blocks are compatible\n - §2C&F§r fruits can be used for fruit flavor items\n -  §2C&F§r milk and flour items are compatible\n - §2C&F§r farmland crops can be grown in bush pots\n - §uC&B§r crops can be harvested with machinery\n - Sickle works on §uC&B§r Crops",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:chapters"
            }
        ],
        type: "form",
        openOnBlock: ""
    },

    //ABOUT MENU

    {
        menuID: "sgs_cake:about",
        title: "About the Add-On",
        body: "§l§6Cakes & Bakes!§r is an Add-On created by §uSome Game Studios§r\n\nVersion: §d1.0.0§r\n\nDiscord: §o§6discord.gg/somegamestudio§r\nTwitter/X: §o§6@somegamestudioX§r\nYoutube: §o§6@somegamestudioYT§r\nTikTok: §o§6@somegamestudioTK§r\n\nWant to reach out? Contact us at §o§6hello@somegamestudio.co.uk§r!\n",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            },
        ],
        type: "about",
        openOnBlock: ""
    },

    //FUNCTIONAL

    {
        menuID: "sgs_cake:config",
        title: "Settings",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_settings",
                connectID: "sgs_cake:main"
            }

        ],
        type: "function",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:search",
        title: "Search",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_settings",
                connectID: "sgs_cake:main"
            }

        ],
        type: "function",
        openOnBlock: [],
        openOnEntity: []
    },
    //RECIPE LOOKUPS
    /*
    {
        menuID: "sgs_cake:recipe_cauldron",
        title: "Cauldron Recipes",
        recipe_array: config.CAULDRON_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:copper_cauldron"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    */
    {
        menuID: "sgs_cake:jams",
        title: "Cauldron Recipes - Jams",
        recipe_array: config.CAULDRON_RECIPES,
        type_category: "jams",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:copper_cauldron"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:glazes",
        title: "Cauldron Recipes - Glazes",
        recipe_array: config.CAULDRON_RECIPES,
        type_category: "glazes",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:copper_cauldron"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:doughs",
        title: "Mixing Bowl Recipes - Doughs",
        recipe_array: config.MIXING_BOWL_RECIPES,
        type_category: "doughs",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:mixing_bowls"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:fillings",
        title: "Mixing Bowl Recipes - Fillings",
        recipe_array: config.MIXING_BOWL_RECIPES,
        type_category: "fillings",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:mixing_bowls"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:jellys",
        title: "Mixing Bowl Recipes - Jellys",
        recipe_array: config.MIXING_BOWL_RECIPES,
        type_category: "jellys",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:mixing_bowls"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:batters",
        title: "Mixing Bowl Recipes - Batters",
        recipe_array: config.MIXING_BOWL_RECIPES,
        type_category: "batters",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:mixing_bowls"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:misc",
        title: "Mixing Bowl Recipes - Ingredients",
        recipe_array: config.MIXING_BOWL_RECIPES,
        type_category: "misc",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:mixing_bowls"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:candy",
        title: "Cauldron Recipes - Candy",
        recipe_array: config.CAULDRON_RECIPES,
        type_category: "candy",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:copper_cauldron"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:cauldron_misc",
        title: "Cauldron Recipes - Ingredients",
        recipe_array: config.CAULDRON_RECIPES,
        type_category: "cauldron_misc",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:copper_cauldron"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:blender_milk",
        title: "Blender Recipes - Ingredients",
        recipe_array: config.BLENDER_RECIPES,
        type_category: "milk",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:blender"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:milkshakes",
        title: "Blender Recipes - Milkshakes",
        recipe_array: config.BLENDER_RECIPES,
        type_category: "milkshakes",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:blender"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:slushies",
        title: "Blender Recipes - Slushies",
        recipe_array: config.BLENDER_RECIPES,
        type_category: "slushies",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:blender"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:spritzers",
        title: "Blender Recipes - Spritzers",
        recipe_array: config.BLENDER_RECIPES,
        type_category: "spritzers",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:blender"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:ice_creams",
        title: "Ice Cream Churn - Ice Cream",
        recipe_array: config.ICE_CHURN_RECIPES,
        type_category: "ice_creams",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:ice_cream_churn"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:popsicles",
        title: "Ice Cream Churn Recipes - Popsicles",
        recipe_array: config.ICE_CHURN_RECIPES,
        type_category: "popsicles",
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:ice_cream_churn"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    /*
    {
        menuID: "sgs_cake:recipe_mixing_bowl",
        title: "Mixing Bowl Recipes",
        recipe_array: config.MIXING_BOWL_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:mixing_bowl"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:recipe_ice_churn",
        title: "Ice Churn Recipes",
        recipe_array: config.ICE_CHURN_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    */
    {
        menuID: "sgs_cake:recipe_teapot",
        title: "Teapot Recipes",
        recipe_array: config.TEAPOT_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    /*
    {
        menuID: "sgs_cake:recipe_blender",
        title: "Blender Recipes",
        recipe_array: config.BLENDER_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    */
    {
        menuID: "sgs_cake:recipe_butter_churn",
        title: "Butter Churn Recipes",
        recipe_array: config.BUTTER_CHURN_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    {
        menuID: "sgs_cake:recipe_pan",
        title: "Pan Recipes",
        recipe_array: config.PAN_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    /*
    {
        menuID: "sgs_cake:recipe_millstone",
        title: "Millstone Recipes",
        recipe_array: config.MILLSTONE_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    },
    */
    {
        menuID: "sgs_cake:recipe_fruit_press",
        title: "Fruit Press Recipes",
        recipe_array: config.FRUIT_PRESS_RECIPES,
        body: "",
        buttons: [
            {
                title: "Return",
                icon: "textures/sgs/cake/items/icons/icon_return",
                connectID: "sgs_cake:main"
            }

        ],
        type: "recipe",
        openOnBlock: [],
        openOnEntity: []
    }
];

guideBookListeners();

export function openMenu(player, menuID) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);

    const menu_data = MENUS.find((f) => f.menuID == menuID);
    if (menu_data == undefined) return;
    //exceptions for functional menus
    if (menu_data.type == "function") {
        if (menu_data.menuID == "sgs_cake:config") configMenu(player);
        if (menu_data.menuID == "sgs_cake:search") searchMenu(player);
        return;
    };

    if (menu_data.type == "recipe") {
        recipeMenu(player, menu_data);
        return;
    };
    const menu = new ui.ActionFormData();
    menu.title(menu_data.title);
    menu.body(menu_data.body);
    menu_data.buttons.forEach((button) => {
        menu.button(button.title, button.icon);
    });

    menu.show(player).then(result => {
        if (result.canceled) return;

        let response = result.selection;
        openMenu(player, menu_data.buttons[response].connectID);
    });
}

function guideBookListeners() {
    //drop guidebook on player first spawn only
    mc.world.afterEvents.playerSpawn.subscribe((eventData) => {
        if (eventData.player.hasTag('sgs_cake_book')) { }
        else {
            mc.world.getDimension(eventData.player.dimension.id).spawnItem(new mc.ItemStack("sgs_cake:guidebook", 1), eventData.player.location);
            eventData.player.addTag('sgs_cake_book');
        }
    })

    //guidebook activation listener
    mc.world.afterEvents.itemUse.subscribe((eventData) => {
        let player = eventData.source;
        let itemUse = eventData.itemStack;
        if (itemUse === undefined) return;
        if (itemUse.typeId == "sgs_cake:guidebook") mc.system.runTimeout(() => { openMenu(player, "sgs_cake:main"); }, 2)
    })
}

//FUNCTION MENUS
function configMenu(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);

    const RACCOON = mc.world.scoreboard.getObjective("sgs_cake_settings").getScore("RACCOON");
    const CRAFTSOUND = mc.world.scoreboard.getObjective("sgs_cake_settings").getScore("CRAFTSOUND");

    let form = new ui.ModalFormData();
    form.title("Settings");

    form.toggle("Enable §aRaccoon§r spawning", { defaultValue: RACCOON == 1 ? true : false });
    form.toggle("Play §asound§r when an interactive crafting station §afinishes crafting§r", { defaultValue: CRAFTSOUND == 1 ? true : false });

    form.submitButton("Confirm");
    form.show(player).then(result => {
        if (result.canceled) return;
        mc.world.scoreboard.getObjective("sgs_cake_settings").setScore("RACCOON", result.formValues[0]);
        mc.world.scoreboard.getObjective("sgs_cake_settings").setScore("CRAFTSOUND", result.formValues[1])
    });
}

//RECIPE LOOKUP
function recipeMenu(player, menu_data) {
    let recipe_string = menu_data.body;
    const recipe_array = menu_data.recipe_array;
    const type_category = menu_data.type_category;

    for (let index = 0; index < recipe_array.length; index++) {
        const element = recipe_array[index];
        if (type_category == undefined) {
            recipe_string = recipe_string + element.display + "\n\n";
        }
        else {
            if (element.type == type_category) recipe_string = recipe_string + element.display + "\n\n";
        }
    }

    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 5.0, });

    const menu = new ui.ActionFormData();
    menu.title(menu_data.title);
    menu.body(recipe_string);

    menu_data.buttons.forEach((button) => {
        menu.button(button.title, button.icon);
    });

    menu.show(player).then(result => {
        if (result.canceled) return;

        let response = result.selection;
        openMenu(player, menu_data.buttons[response].connectID);
    });
    return;
}

//SEARCH
function searchMenu(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);

    let form = new ui.ModalFormData();
    form.title("Search");
    form.textField("§uWhat are you looking for?§r\n\nEnter a §6keyword§r to search for mentions in this Guidebook.\n\n§o§8If there are too many results, it might not show all of them. In that case, try specifying further.\n\n", "Enter keyword");
    form.submitButton("Search!");

    form.show(player).then(result => {
        if (result.canceled) return;
        searchResultMenu(player, result.formValues[0]);
    });
}

function searchResultMenu(player, string) {

    const searchString = string.toLowerCase();
    let results = [];

    MENUS.forEach(element => {
        let content = element.body;
        if (element.type == "recipe") {
            const recipe_array = element.recipe_array;
            const type_category = element.type_category;

            for (let index = 0; index < recipe_array.length; index++) {
                const recipes = recipe_array[index];
                if (type_category == undefined) {
                    content = content + recipes.display + "\n\n";
                }
                else {
                    if (recipes.type == type_category) content = content + recipes.display + "\n\n";
                }
            }
        }

        const elementString = unformatString(element.title + " " + content);


        let count = (elementString.match(new RegExp(searchString, "g")) || []).length

        if (count > 0) {
            if (count >= 10) count = 10;
            if (element.type == "form") results.push({ id: element, amount: count, icon: "textures/sgs/cake/items/icons/icon_search" });
            if (element.type == "recipe") results.push({ id: element, amount: count, icon: "textures/sgs/cake/items/icons/icon_search" });
            //add in recipes here
        }
    });

    results.sort(compareResults);
    results.push({ id: { menuID: "sgs_cake:main", title: "Return" }, amount: 0, icon: "textures/sgs/cake/items/icons/icon_return" });
    results = results.slice(0, MAX_SEARCH_RESULTS);

    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);

    let body = "Sorry, could not find anything"
    if (results.length > 0) body = "Pages related to your search: §6" + string;

    const menu = new ui.ActionFormData();
    menu.title("Search Results");
    menu.body(body);

    results.forEach((element) => {
        let buttonString = element.id.title + " §6§l\n";
        for (let index = 0; index < element.amount; index++) {
            buttonString = buttonString + "*"
        }
        menu.button(buttonString, element.icon);
    });

    menu.show(player).then(result => {
        if (result.canceled) return;

        let response = result.selection;
        openMenu(player, results[response].id.menuID);
    });
}

function unformatString(string) {
    let stringArray = string.split("§");
    for (let index = 0; index < stringArray.length; index++) {
        stringArray[index] = stringArray[index].substring(1).toLowerCase();
    }
    return stringArray.join();
}

function compareResults(a, b) {
    if (a.amount < b.amount) return 1;
    if (a.amount > b.amount) return -1;
    return 0;
}