import { world } from "@minecraft/server";
import { ActionFormData, MessageFormData, ModalFormData, ModalFormResponse } from "@minecraft/server-ui";
import { showMenu } from "./settings";

// Constants
// Categories
const categories = {
    introduction: {
        icon: "textures/squaredreams/realism/icons/introduction"
    },
    tips: {
        icon: "textures/squaredreams/realism/icons/tips"
    },
    settings: {
        icon: "textures/squaredreams/realism/icons/settings"
    }
};

// Item listener will open the menu
world.afterEvents.itemUse.subscribe(event => {
    if (event.itemStack.typeId === "squaredreams_realism:addon_book") {
        showAddonMenu(event.source);
    }
});

// Menu opener
function showAddonMenu(player) {
    const form = new ActionFormData();
    // Title
    form.title({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.title" }] });
    form.body({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.body", with: ["\n"] }] });

    // Add all categories
    for (const [categoryID, category] of Object.entries(categories)) {
        form.button({ rawtext: [{ translate: `ui.squaredreams_realism.addon_book.category.${categoryID}` }] }, category.icon);
    }

    // Failsafe
    form.show(player).then(response => {
        if (!response.canceled) {
            switch (response.selection) {
                case 0:
                    showIntroductionMenu(player);
                    break;
                case 1:
                    showTipsMenu(player);
                    break;
                case 2:
                    showMenu(player);
                    break;
                default:
                    break;
            }
        }
    });
}

// Category pages
function showIntroductionMenu(player) {
    const form = new MessageFormData();
    // Title
    form.title({ rawtext: [ { translate: `ui.squaredreams_realism.addon_book.category.${Object.keys(categories)[0]}` } ] });
    form.body({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.introduction.body", with: ["\n"] }] });
    form.button1({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.button.close" }] });
    form.button2({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.button.return" }] })

    // Failsafe
    form.show(player).then(response => {
        if (!response.canceled && response.selection) {
            showAddonMenu(player);
        }
    });
}

// Category pages
function showTipsMenu(player) {
    const form = new MessageFormData();
    // Title
    form.title({ rawtext: [ { translate: `ui.squaredreams_realism.addon_book.category.${Object.keys(categories)[1]}` } ] });
    form.body({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.tips.body", with: ["\n"] }] });
    form.button1({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.button.close" }] });
    form.button2({ rawtext: [{ translate: "ui.squaredreams_realism.addon_book.button.return" }] })

    // Failsafe
    form.show(player).then(response => {
        if (!response.canceled && response.selection) {
            showAddonMenu(player);
        }
    });
}