import * as ui from "@minecraft/server-ui";
import { world, system, Player } from "@minecraft/server";

let showedForm = false;

const buttonIconPaths = {
    tutorial: "textures/octovon/db/menu/button_icon_tutorials",
    recipes: "textures/octovon/db/menu/button_icon_recipes",
    settings: "textures/octovon/db/menu/button_icon_settings",
    help: "textures/octovon/db/menu/button_icon_help",
    exit: "textures/octovon/db/menu/button_icon_exit",
    back: "textures/octovon/db/menu/button_icon_back",
    pin: "textures/octovon/db/menu/button_icon_pin",
    unpin: "textures/octovon/db/menu/button_icon_unpin",
    changelog: "textures/octovon/db/menu/button_icon_changelog",
    debuffs: "textures/octovon/db/menu/button_icon_debuffs",
    featureHealthMonitor: "textures/octovon/db/menu/button_icon_health_monitor",
    featureThirst: "textures/octovon/db/menu/button_icon_thirst",
    featureLimbDamage: "textures/octovon/db/menu/button_icon_limb_damage",
    featureClimateHazards: "textures/octovon/db/menu/button_icon_climate_hazards",
    fatureInsulatedGear: "textures/octovon/db/menu/button_icon_insulated_gear",
    featureRottingFood: "textures/octovon/db/menu/button_icon_rotting_food",
    featureTalismans: "textures/octovon/db/menu/button_icon_talismans"
}

world.beforeEvents.itemUse.subscribe(event => {
    if (event.itemStack.typeId.startsWith("oct_db")) {

        switch (event.itemStack.typeId) {
            case "oct_db:guidebook": {
                event.cancel = true;
                if (!showedForm) {
                    showedForm = true;
                    system.runTimeout(() => {
                        initGuidebookMenu(event.source, event.itemStack);
                        showedForm = false;
                    }, 1);
                }
                break;
            }

            default:
                // Not a valid item
                break;
        }
    }
});
export function initGuidebookMenu(player, guidebook) {
    // Get the stored page from the guidebook if any
    const storedPage = player.getDynamicProperty("oct_db:stored_page");

    switch (storedPage) {
        case "itemInfo":
            const itemInfoItem = player.getDynamicProperty("oct_db:stored_page_item") ?? "guidebook";
            openGuidebookMenuItemInfo(player, itemInfoItem, true);
            break;
        case "FeatureInfo":
            const FeatureInfoKey = player.getDynamicProperty("oct_db:stored_page_item") ?? "health_monitor";
            switch (FeatureInfoKey) {
                case "health_monitor":
                    openGuidebookMenuFeatureInfoMonitor(player, true);
                    break;
                default:
                    openGuidebookMenuFeatureInfo(player, FeatureInfoKey, true);
                    break;
            }
            break;
        default:
            openGuidebookMenuMain(player);
            break;
    }

}
function openGuidebookMenuMain(player) {
    // Create menu
    const guidebookMenuMain = new ui.ActionFormData();
    guidebookMenuMain.title({ rawtext: [{ translate: "forms.guidebookMenuMain.title" }] });

    // Populate with GUI Elements
    guidebookMenuMain.body(
        {
            rawtext: [
                { translate: "forms.guidebookMenuMain.body", with: ["\n"] }
            ]
        }
    );

    guidebookMenuMain.button({ rawtext: [{ translate: "forms.button.guidebookMenu.tutorial" }] }, buttonIconPaths["tutorial"]);
    guidebookMenuMain.button({ rawtext: [{ translate: "forms.button.guidebookMenu.recipes" }] }, buttonIconPaths["recipes"]);
    guidebookMenuMain.button({ rawtext: [{ translate: "forms.button.guidebookMenu.settings" }] }, buttonIconPaths["settings"]);
    guidebookMenuMain.button({ rawtext: [{ translate: "forms.button.guidebookMenu.help" }] }, buttonIconPaths["help"]);
    guidebookMenuMain.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuMain.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 4)
            return;

        switch (result.selection) {
            case 0:
                // Features
                openGuidebookMenuFeatures(player);
                break;
            case 1:
                // Crafring Recipes
                openGuidebookMenuRecipes(player);
                break;
            case 2:
                // Add-On Settings
                openGuidebookMenuSettings(player);
                break;
            case 3:
                // Help
                openGuidebookMenuHelp(player);
                break;
        }
    });
}

function openGuidebookMenuFeatures(player) {
    // Create menu
    const guidebookMenuFeatures = new ui.ActionFormData();
    guidebookMenuFeatures.title({ rawtext: [{ translate: "forms.guidebookMenuFeatures.title" }] });


    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenuFeatures.health_monitor" }] }, buttonIconPaths["featureHealthMonitor"]);
    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenuFeatures.thirst" }] }, buttonIconPaths["featureThirst"]);
    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenuFeatures.limb_damage" }] }, buttonIconPaths["featureLimbDamage"]);
    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenuFeatures.climate_hazards" }] }, buttonIconPaths["featureClimateHazards"]);
    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenuFeatures.insulated_gear" }] }, buttonIconPaths["fatureInsulatedGear"]);
    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenuFeatures.rotting_food" }] }, buttonIconPaths["featureRottingFood"]);
    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenuFeatures.talismans" }] }, buttonIconPaths["featureTalismans"]);

    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuFeatures.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuFeatures.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 8)
            return;

        switch (result.selection) {
            case 0:
                openGuidebookMenuFeatureInfoMonitor(player);
                break;
            case 1:
                openGuidebookMenuFeatureInfo(player, "thirst");
                break;
            case 2:
                openGuidebookMenuFeatureInfo(player, "limb_damage");
                break;
            case 3:
                openGuidebookMenuFeatureInfo(player, "climate_hazards");
                break;
            case 4:
                openGuidebookMenuFeatureInfo(player, "insulated_gear");
                break;
            case 5:
                openGuidebookMenuFeatureInfo(player, "rotting_food");
                break;
            case 6:
                openGuidebookMenuFeatureInfo(player, "talismans");
                break;
            case 7:
                openGuidebookMenuMain(player);
                break;
        }
    });
}

function openGuidebookMenuFeatureInfoMonitor(player, isPinned = false) {
    // Check if pinned (case when going back from menu navigation)
    let pinnedPage = false;
    if (player.getDynamicProperty('oct_db:stored_page_item')) {
        pinnedPage = player.getDynamicProperty('oct_db:stored_page_item') === "health_monitor" ? true : false;
    }

    // Create menu
    const guidebookMenuFeatureInfo = new ui.ActionFormData();
    guidebookMenuFeatureInfo.title({ rawtext: [{ translate: "forms.guidebookMenuFeatures.health_monitor.title" }] });

    // Populate with GUI Elements
    guidebookMenuFeatureInfo.body(
        {
            rawtext: [
                {
                    translate: `forms.guidebookMenuFeatures.body.frame`, "with": {
                        "rawtext": [
                            { text: "\n" },
                            {
                                translate: `forms.guidebookMenuFeatures.body.health_monitor`, "with": {
                                    "rawtext": [
                                        { text: "\n" }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    );

    guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.debuffs" }] }, buttonIconPaths["debuffs"]);

    if (isPinned || pinnedPage) {
        guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.unpin" }] }, buttonIconPaths["unpin"]);
    } else {
        guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.pin" }] }, buttonIconPaths["pin"]);
    }

    guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuFeatureInfo.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 3)
            return;

        switch (result.selection) {
            case 0:
                openGuidebookMenuDebuffs(player);
                break;
            case 1:
                if (!isPinned && !pinnedPage) {
                    // Store the page
                    const pageId = "FeatureInfo";
                    player.setDynamicProperty('oct_db:stored_page', pageId);
                    player.setDynamicProperty('oct_db:stored_page_item', "health_monitor");
                    // Refresh
                    openGuidebookMenuFeatureInfoMonitor(player, true);
                } else {
                    // Clear stored page
                    player.setDynamicProperty('oct_db:stored_page', undefined);
                    player.setDynamicProperty('oct_db:stored_page_item', undefined);
                    // Refresh
                    openGuidebookMenuFeatureInfoMonitor(player, false);
                }
                break;
            case 2:
                openGuidebookMenuFeatures(player);
                break;
        }
    });
}
function openGuidebookMenuFeatureInfo(player, featureId, isPinned = false) {
    // Check if pinned (case when going back from menu navigation)
    let pinnedPage = false;
    if (player.getDynamicProperty('oct_db:stored_page_item')) {
        pinnedPage = player.getDynamicProperty('oct_db:stored_page_item') === featureId ? true : false;
    }

    // Create menu
    const guidebookMenuFeatureInfo = new ui.ActionFormData();
    guidebookMenuFeatureInfo.title({ rawtext: [{ translate: `forms.guidebookMenuFeatures.${featureId}.title` }] });

    // Populate with GUI Elements
    guidebookMenuFeatureInfo.body(
        {
            rawtext: [
                {
                    translate: `forms.guidebookMenuFeatures.body.frame`, "with": {
                        "rawtext": [
                            { text: "\n" },
                            {
                                translate: `forms.guidebookMenuFeatures.body.${featureId}`, "with": {
                                    "rawtext": [
                                        { text: "\n" }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    );


    if (isPinned || pinnedPage) {
        guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.unpin" }] }, buttonIconPaths["unpin"]);
    } else {
        guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.pin" }] }, buttonIconPaths["pin"]);
    }

    guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuFeatureInfo.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 2)
            return;

        switch (result.selection) {
            case 0:
                if (!isPinned && !pinnedPage) {
                    // Store the page
                    const pageId = "FeatureInfo";
                    player.setDynamicProperty('oct_db:stored_page', pageId);
                    player.setDynamicProperty('oct_db:stored_page_item', featureId);
                    // Refresh
                    openGuidebookMenuFeatureInfo(player, featureId, true);
                } else {
                    // Clear stored page
                    player.setDynamicProperty('oct_db:stored_page', undefined);
                    player.setDynamicProperty('oct_db:stored_page_item', undefined);
                    // Refresh
                    openGuidebookMenuFeatureInfo(player, featureId, false);
                }
                break;
            case 1:
                openGuidebookMenuFeatures(player);
                break;
        }
    });
}
function openGuidebookMenuDebuffs(player) {
    // Create menu
    const guidebookMenuDebuffs = new ui.ActionFormData();
    guidebookMenuDebuffs.title({ rawtext: [{ translate: "forms.guidebookMenuDebuffs.title" }] });

    guidebookMenuDebuffs.body(
        {
            rawtext: [
                { translate: "forms.guidebookMenuDebuffs.body", with: ["\n"] }
            ]
        }
    );

    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.thirst" }] }, "textures/octovon/db/menu/button_icon_debuff_thirst");
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.limb_damage" }] }, "textures/octovon/db/menu/button_icon_debuff_limb_damage");
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.warm" }] }, "textures/octovon/db/menu/button_icon_debuff_warm");
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.overheat" }] }, "textures/octovon/db/menu/button_icon_debuff_overheat");
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.heatstroke" }] }, "textures/octovon/db/menu/button_icon_debuff_heatstroke");
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.cold" }] }, "textures/octovon/db/menu/button_icon_debuff_cold");
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.frostbite" }] }, "textures/octovon/db/menu/button_icon_debuff_frostbite");
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.hypothermia" }] }, "textures/octovon/db/menu/button_icon_debuff_hypothermia");

    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuDebuffs.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuDebuffs.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 9)
            return;

        switch (result.selection) {
            case 0:
                openGuidebookMenuDebuffInfo(player, "thirst");
                break;
            case 1:
                openGuidebookMenuDebuffInfo(player, "limb_damage");
                break;
            case 2:
                openGuidebookMenuDebuffInfo(player, "warm");
                break;
            case 3:
                openGuidebookMenuDebuffInfo(player, "overheat");
                break;
            case 4:
                openGuidebookMenuDebuffInfo(player, "heatstroke");
                break;
            case 5:
                openGuidebookMenuDebuffInfo(player, "cold");
                break;
            case 6:
                openGuidebookMenuDebuffInfo(player, "frostbite");
                break;
            case 7:
                openGuidebookMenuDebuffInfo(player, "hypothermia");
                break;
            case 8:
                openGuidebookMenuFeatureInfoMonitor(player);
                break;
        }
    });
}
function openGuidebookMenuDebuffInfo(player, debuffId) {
    // Create menu
    const guidebookMenuFeatureInfo = new ui.ActionFormData();
    guidebookMenuFeatureInfo.title({ rawtext: [{ translate: `forms.guidebookMenuDebuffs.${debuffId}.title` }] });

    // Populate with GUI Elements
    guidebookMenuFeatureInfo.body(
        {
            rawtext: [
                {
                    translate: `forms.guidebookMenuFeatures.body.frame`, "with": {
                        "rawtext": [
                            { text: "\n" },
                            {
                                translate: `forms.guidebookMenuDebuffs.body.${debuffId}`, "with": {
                                    "rawtext": [
                                        { text: "\n" }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    );


    guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuFeatureInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuFeatureInfo.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 1)
            return;

        switch (result.selection) {
            case 0:
                openGuidebookMenuDebuffs(player);
                break;
        }
    });
}

function openGuidebookMenuHelp(player) {
    // Create menu
    const guidebookMenuHelp = new ui.ActionFormData();
    guidebookMenuHelp.title({ rawtext: [{ translate: "forms.guidebookMenuHelp.title" }] });

    // Populate with GUI Elements
    guidebookMenuHelp.body(
        {
            rawtext: [
                { translate: "forms.guidebookMenuHelp.body", with: ["\n"] }
            ]
        }
    );

    guidebookMenuHelp.button({ rawtext: [{ translate: "forms.button.guidebookMenu.changelog" }] }, buttonIconPaths["changelog"]);
    guidebookMenuHelp.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuHelp.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuHelp.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 2)
            return;

        switch (result.selection) {
            case 0:
                openGuidebookMenuChangelog(player);
                break;
            case 1:
                openGuidebookMenuMain(player);
                break;
        }
    });
}
function openGuidebookMenuChangelog(player) {
    // Create menu
    const guidebookMenuChangelog = new ui.ActionFormData();
    guidebookMenuChangelog.title({ rawtext: [{ translate: "forms.changelog.title" }] });

    // Populate with GUI Elements
    guidebookMenuChangelog.body(
        {
            rawtext: [
                {
                    translate: "forms.changelog.header", "with": {
                        "rawtext": [
                            { text: "\n" },
                            { translate: "forms.changelog.versionNumber" }
                        ]
                    }
                },
                { translate: "forms.changelog.divider", with: ["\n"] },
                { translate: "forms.changelog.version.1.0.0", with: ["\n"] },

                { translate: "forms.changelog.divider", with: ["\n"] },
                { translate: "forms.changelog.divider", with: ["\n"] },
                { translate: "forms.changelog.roadmap", with: ["\n"] },

                { translate: "forms.changelog.divider", with: ["\n"] }
            ]
        }
    );

    guidebookMenuChangelog.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuChangelog.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuChangelog.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 1)
            return;

        switch (result.selection) {
            case 0:
                openGuidebookMenuHelp(player);
                break;
        }
    });
}

function openGuidebookMenuSettings(player) {
    const isPlayerOP = player?.getGameMode() === "creative";

    const worldSetting = world.getDynamicProperty('oct_db:worldSetting') || 3; //E.G. Difficulty

    // Player settings
    const playerSettingsArr = [
        { name: "notifications", text: "forms.guidebookMenuSettings.toggle.notifications" }, // Text value is not dynamic for now because I can't f9 the text and I don't want to restart world every time I change the name of the setting
        { name: "offhand_sneak", text: "forms.guidebookMenuSettings.toggle.offhand_sneak" },
        { name: "actionbar_stats", text: "forms.guidebookMenuSettings.toggle.actionbar_stats" },
        { name: "use_metric", text: "forms.guidebookMenuSettings.toggle.use_metric" },
    ];
    const oldPlayerSettingsArr = [];
    for (const setting of playerSettingsArr) {
        const defaultValue = player.getDynamicProperty(`oct_db:setting_${setting.name}`) ?? (setting.name === "notifications" ? true : false); // EDGECASE: I want notifications to be turned on by default
        oldPlayerSettingsArr.push(defaultValue);
    };

    // Create menu
    const guidebookMenuSettings = new ui.ModalFormData();
    guidebookMenuSettings.title({ rawtext: [{ translate: "forms.guidebookMenuSettings.title" }] });

    // Populate with GUI Elements
    for (const setting of playerSettingsArr) {
        const defaultValue = player.getDynamicProperty(`oct_db:setting_${setting.name}`) ?? (setting.name === "notifications" ? true : false); // EDGECASE: I want notifications to be turned on by default
        guidebookMenuSettings.toggle({ rawtext: [{ "translate": setting.text, "with": ["\n"] }] }, defaultValue);
    }

    guidebookMenuSettings.slider({
        rawtext: [
            {
                "translate": "forms.guidebookMenuSettings.slider.worldSetting", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `${isPlayerOP ? "" : "forms.guidebookMenuSettings.opLock"}`, "with": ["\n"] }
                    ]
                }
            }]
    }, 1, 3, 1, worldSetting);

    // Submit Button
    guidebookMenuSettings.submitButton({ rawtext: [{ translate: "forms.guidebookMenuSettings.submitButton", with: ["\n"] }] });

    // Create and Show menu
    guidebookMenuSettings.show(player).then((result) => {
        // Action mappings
        if (result.canceled) {
            return;
        } else {
            let worldSettingChanged = false;
            let playerSettingChanged = false;

            // Fetch form values
            const worldSettingValue = result.formValues[playerSettingsArr.length];

            // Apply settings
            if (isPlayerOP) {
                world.setDynamicProperty('oct_db:worldSetting', worldSettingValue);
                worldSettingChanged = (worldSettingValue !== worldSetting);
            }

            for (const setting of playerSettingsArr) {
                const settingValue = result.formValues[playerSettingsArr.indexOf(setting)];
                player.setDynamicProperty(`oct_db:setting_${setting.name}`, settingValue);
                if (settingValue !== oldPlayerSettingsArr[playerSettingsArr.indexOf(setting)]) playerSettingChanged = true;

                // Edge case for offhand sneak animation
                if (setting.name === "offhand_sneak") {
                    // FIXME: I'm using commands here because the playAnimation function doesn't want to work and I'm way too tired to figure it out right now
                    player.runCommand(`playanimation @s animation.oct_db.health_menu.setting.offhand_sneak.${settingValue ? "off" : "on"} animation.oct_db.health_menu.setting.offhand_sneak.${settingValue ? "off" : "on"}`);
                }
            }

            // Send a message
            if (worldSettingChanged) {
                const notificationMessage = {
                    rawtext: [
                        {
                            "translate": "message.oct_db.announcer", "with": ["\n"]
                        },
                        {
                            "translate": "message.oct_db.settings_updated.world", "with": ["\n"]
                        }
                    ]
                };

                world.sendMessage(notificationMessage);
                const players = world.getAllPlayers();
                players.forEach((player) => {
                    player.playSound('random.orb');
                });

            }
            else if (playerSettingChanged) {
                const notificationMessage = {
                    rawtext: [
                        {
                            "translate": "message.oct_db.announcer", "with": ["\n"]
                        },
                        {
                            "translate": "message.oct_db.settings_updated.player", "with": ["\n"]
                        }
                    ]
                };
                player.sendMessage(notificationMessage);
                player.playSound('random.orb');
            }

            return;
        }
    });
}

function openGuidebookMenuRecipes(player) {
    // Create menu
    const guidebookMenuRecipes = new ui.ActionFormData();
    guidebookMenuRecipes.title({ rawtext: [{ translate: "forms.guidebookMenuRecipes.title" }] });

    // Populate with GUI Elements
    guidebookMenuRecipes.button({ rawtext: [{ translate: "forms.guidebookMenuItemInfo.itemName.guidebook" }] }, "textures/octovon/db/menu/recipes/guidebook");
    guidebookMenuRecipes.button({ rawtext: [{ translate: "item.oct_db:health_monitor.healthy.name" }] }, "textures/octovon/db/menu/recipes/health_monitor");
    guidebookMenuRecipes.button({ rawtext: [{ translate: "item.oct_db:insulation_fabric.name" }] }, "textures/octovon/db/menu/recipes/fabric");
    guidebookMenuRecipes.button({ rawtext: [{ translate: "forms.guidebookMenuItemInfo.itemName.water_flask" }] }, "textures/octovon/db/menu/recipes/water_flask");
    guidebookMenuRecipes.button({ rawtext: [{ translate: "item.oct_db:bandage.name" }] }, "textures/octovon/db/menu/recipes/bandage");
    guidebookMenuRecipes.button({ rawtext: [{ translate: "item.oct_db:enhanced_bandage.name" }] }, "textures/octovon/db/menu/recipes/enhanced_bandage");
    guidebookMenuRecipes.button({ rawtext: [{ translate: "item.oct_db:medkit.name" }] }, "textures/octovon/db/menu/recipes/medkit");
    guidebookMenuRecipes.button({ rawtext: [{ translate: "forms.guidebookMenuItemInfo.itemName.insulated_gear" }] }, "textures/octovon/db/menu/recipes/insulated_gear");


    guidebookMenuRecipes.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuRecipes.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuRecipes.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 9)
            return;

        switch (result.selection) {
            case 0:
                openGuidebookMenuItemInfo(player, "guidebook");
                break;
            case 1:
                openGuidebookMenuItemInfo(player, "health_monitor");
                break;
            case 2:
                openGuidebookMenuItemInfo(player, "insulation_fabric");
                break;
            case 3:
                openGuidebookMenuItemInfo(player, "water_flask");
                break;
            case 4:
                openGuidebookMenuItemInfo(player, "bandage");
                break;
            case 5:
                openGuidebookMenuItemInfo(player, "enhanced_bandage");
                break;
            case 6:
                openGuidebookMenuItemInfo(player, "medkit");
                break;
            case 7:
                openGuidebookMenuItemInfo(player, "insulated_gear");
                break;
            case 8:
                openGuidebookMenuMain(player);
                break;
        }
    });
}
function openGuidebookMenuItemInfo(player, itemTypeId, isPinned = false) {
    // Check if pinned (case when going back from menu navigation)
    let pinnedPage = false;
    if (player.getDynamicProperty('oct_db:stored_page_item')) {
        pinnedPage = player.getDynamicProperty('oct_db:stored_page_item') === itemTypeId ? true : false;
    }

    // Create menu
    const guidebookMenuItemInfo = new ui.ActionFormData();
    guidebookMenuItemInfo.title({
        rawtext: [
            { translate: `forms.guidebookMenuRecipes.${itemTypeId}.title` }
        ]
    });

    // Populate with GUI Elements
    if (itemRecipesDictionary[itemTypeId]) {
        guidebookMenuItemInfo.body(itemRecipesDictionary[itemTypeId]);
    } else {
        guidebookMenuItemInfo.body(itemRecipesDictionary['guidebook']);
    }


    if (isPinned || pinnedPage) {
        guidebookMenuItemInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.unpin" }] }, buttonIconPaths["unpin"]);
    } else {
        guidebookMenuItemInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.pin" }] }, buttonIconPaths["pin"]);
    }

    guidebookMenuItemInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.back" }] }, buttonIconPaths["back"]);
    guidebookMenuItemInfo.button({ rawtext: [{ translate: "forms.button.guidebookMenu.exit" }] }, buttonIconPaths["exit"]);


    // Create and Show menu
    guidebookMenuItemInfo.show(player).then(result => {
        // Action mappings
        if (result.canceled || result.selection === 2)
            return;

        switch (result.selection) {
            case 0:
                if (!isPinned && !pinnedPage) {
                    // Store the page
                    const pageId = "itemInfo";
                    player.setDynamicProperty('oct_db:stored_page', pageId);
                    player.setDynamicProperty('oct_db:stored_page_item', itemTypeId);
                    // Refresh
                    openGuidebookMenuItemInfo(player, itemTypeId, true);
                } else {
                    // Clear stored page
                    player.setDynamicProperty('oct_db:stored_page', undefined);
                    player.setDynamicProperty('oct_db:stored_page_item', undefined);
                    // Refresh
                    openGuidebookMenuItemInfo(player, itemTypeId, false);
                }
                break;
            case 1:
                openGuidebookMenuRecipes(player);
                break;
        }
    });
}

const itemRecipesDictionary = {
    "guidebook": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemName.guidebook` },
                        { text: "1" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.guidebook`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeShapelessHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[§i█§8] [§n█§8] [▒]
[▒] [▒] [▒]
[▒] [▒] [▒]

§8[§i█§8] - §i%%1
§8[§n█§8] - §n%%2
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `item.oct_db:insulation_fabric.name` },
                                    { translate: `item.book.name` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "health_monitor": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `item.oct_db:health_monitor.healthy.name` },
                        { text: "1" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.health_monitor`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeShapelessHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[§n█§8] [§i█§8] [▒]
[▒] [▒] [▒]
[▒] [▒] [▒]

§8[§i█§8] - §i%%1
§8[§n█§8] - §n%%2
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `forms.guidebookMenuItemInfo.itemName.guidebook` },
                                    { translate: `tile.dirt.name` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "insulated_gear": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemName.insulated_gear` },
                        { text: "1" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.insulated_gear`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[▒] [§i█§8] [▒]
[§i█§8] [§d█§8] [§i█§8]
[▒] [§i█§8] [▒]

§8[§i█§8] - §i%%1
§8[§d█§8] - §d%%2
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `item.oct_db:insulation_fabric.name` },
                                    { translate: `forms.guidebookMenuItemInfo.itemName.armor` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "insulation_fabric": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `item.oct_db:insulation_fabric.name` },
                        { text: "4" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.insulation_fabric`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[▒] [▒] [▒]
[§f█§8] [§f█§8] [§f█§8]
[▒] [▒] [▒]

§8[§f█§8] - §f%%1
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `tile.wool.white.name` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "bandage": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `item.oct_db:bandage.name` },
                        { text: "4" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.bandage`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[▒] [▒] [▒]
[§n█§8] [§i█§8] [§n█§8]
[▒] [▒] [▒]

§8[§i█§8] - §i%%1
§8[§n█§8] - §n%%2
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `item.oct_db:insulation_fabric.name` },
                                    { translate: `item.string.name` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "enhanced_bandage": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `item.oct_db:enhanced_bandage.name` },
                        { text: "1" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.enhanced_bandage`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeShapelessHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[§i█§8] [§6█§8] [▒]
[§2█§8] [▒] [▒]
[▒] [▒] [▒]

§8[§i█§8] - §i%%1
§8[§2█§8] - §2%%2
§8[§6█§8] - §6%%3
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `item.oct_db:bandage.name` },
                                    { translate: `item.slime_ball.name` },
                                    { translate: `item.glow_berries.name` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "medkit": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `item.oct_db:medkit.name` },
                        { text: "2" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.medkit`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[§i█§8] [§m█§8] [§i█§8]
[§c█§8] [§g█§8] [§d█§8]
[§i█§8] [§b█§8] [§i█§8]

§8[§i█§8] - §i%%1
§8[§g█§8] - §g%%2
§8[§c█§8] - §c%%3
§8[§m█§8] - §m%%4
§8[§d█§8] - §d%%5
§8[§b█§8] - §b%%6
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `item.oct_db:insulation_fabric.name` },
                                    { translate: `item.oct_db:enhanced_bandage.name` },
                                    { translate: `item.apple.name` },
                                    { translate: `item.nether_wart.name` },
                                    { translate: `item.ghast_tear.name` },
                                    { translate: `item.milk.name` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    },
    "water_flask": {
        rawtext: [
            {
                translate: "forms.guidebookMenuItemInfo.itemNameHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemName.water_flask` },
                        { text: "1" }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemInfoHeader", "with": {
                    "rawtext": [
                        { text: "\n" },
                        { translate: `forms.guidebookMenuItemInfo.itemInfo.water_flask`, with: ["\n"] }
                    ]
                }
            },
            {
                translate: "forms.changelog.divider", with: ["\n"]
            },
            {
                translate: "forms.guidebookMenuItemInfo.itemRecipeHeader", "with": {
                    "rawtext": [
                        {
                            translate: `§8
[▒] [§i█§8] [▒]
[§n█§8] [▒] [§n█§8]
[▒] [§6█§8] [▒]

§8[§i█§8] - §i%%1
§8[§n█§8] - §n%%2
§8[§6█§8] - §6%%3
§r`,
                            "with": {
                                "rawtext": [
                                    { translate: `item.string.name` },
                                    { translate: `item.rabbit_hide.name` },
                                    { translate: `item.leather.name` }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
}