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

const SOUND_TURN_PAGE = "item.book.page_turn";
const SOUND_OPTIONS = { pitch: 1.0, volume: 1.0, };

const MENU_MAIN = new ui.ActionFormData();
const MENU_RECIPE = new ui.ActionFormData();
const MENU_PET = new ui.ActionFormData();
const MENU_PET_INFO = new ui.ActionFormData();//added a comment
const MENU_MOOD = new ui.ActionFormData();


export function setupStaticMenus() {
    //BOOK MAIN
    MENU_MAIN.title({ translate: "cyd_mp.menu_main_title" });
    MENU_MAIN.body({ translate: "cyd_mp.menu_main_body", "with": ["\n"] });
    MENU_MAIN.button({ translate: "cyd_mp.menu_button_recipes" }, "textures/cyd/mp/items/sugarcookie");
    MENU_MAIN.button({ translate: "cyd_mp.menu_button_pet" }, "textures/cyd/mp/items/cute_orb_closed");
    MENU_MAIN.button({ translate: "cyd_mp.menu_button_mood" }, "textures/cyd/mp/particle/heart");
    MENU_MAIN.button({ translate: "cyd_mp.menu_button_settings" }, "textures/cyd/mp/items/forgetful_potion");


    //BOOK RECIPE
    MENU_RECIPE.title({ translate: "cyd_mp.menu_recipes_title" });
    MENU_RECIPE.body({ translate: "cyd_mp.menu_recipes_body", "with": ["\n"] });
    MENU_RECIPE.button({ translate: "cyd_mp.menu_button_return" }, "textures/cyd/mp/items/tutorial_book");

    //PET RECIPE
    MENU_PET.title({ translate: "cyd_mp.menu_pet_title" });
    MENU_PET.body({ translate: "cyd_mp.menu_pet_body", "with": ["\n"] });
    MENU_PET.button({ translate: "cyd_mp.menu_button_return" }, "textures/cyd/mp/items/tutorial_book");

    //PET MOOD
    MENU_MOOD.title({ translate: "cyd_mp.menu_mood_title" });
    MENU_MOOD.body({ translate: "cyd_mp.menu_mood_body", "with": ["\n"] });
    MENU_MOOD.button({ translate: "cyd_mp.menu_button_return" }, "textures/cyd/mp/items/tutorial_book");
    
    MENU_PET_INFO.button({ translate: "cyd_mp.menu_button_return" }, "textures/cyd/mp/items/tutorial_book");
}

export function setupDynamicMenus(entityInteract) {
    //PET INFO
    let identifier = entityInteract.typeId;
    MENU_PET_INFO.title({ translate: `entity.${identifier}.name` });
    MENU_PET_INFO.body({ translate: `cyd_mp.dynamic.${identifier}.information`, "with": { "rawtext":[ { "text": `${entityInteract.getProperty("property:level")}` }, { "text": `${entityInteract.getProperty("property:health_remaining")}` }, { "text": "\n" } ] } });
}

export function menuMain(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);
    MENU_MAIN.show(player).then(result => {
        if (result.canceled) return;
        let response = result.selection;
        switch (response) {
            case 0:
                menuRecipe(player);
                break;
            case 1:
                menuPet(player);
                break;
            case 2:
                menuMood(player);    
            case 3:
                settingMenu(player);
                break;

                break;            
            default: break;
        }
    });
}

function menuRecipe(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);
    MENU_RECIPE.show(player).then(result => {
        if (result.canceled) return;
        let response = result.selection;
        switch (response) {
            case 0:
                menuMain(player);
                break;
            default: break;
        }
    });
}

function menuPet(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);
    MENU_PET.show(player).then(result => {
        if (result.canceled) return;
        let response = result.selection;
        switch (response) {
            case 0:
                menuMain(player);
                break;
            default: break;
        }
    });
}
function menuMood(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);
    MENU_MOOD.show(player).then(result => {
        if (result.canceled) return;
        let response = result.selection;
        switch (response) {
            case 0:
                menuMain(player);
                break;
            default: break;
        }
    });
}

export function petMenu(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);
    MENU_PET_INFO.show(player).then(result => {
        if (result.canceled) return;
    });
}

export function settingMenu(player) {
    const global_showlvl = mc.world.scoreboard.getObjective("cyd_mp_settings").getScore("show");
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS);
    let form = new ui.ModalFormData();
    form.title({ translate: "cyd_mp.menu_settings_title" })
    form.toggle({ translate: "cyd_mp.menu_settings_show_lvl" }, isTrue(global_showlvl));

    form.show(player).then(result => {
        if (result.canceled) return;
        mc.world.scoreboard.getObjective("cyd_mp_settings").setScore("show", result.formValues[0])
    });
}

function isTrue(number) {
    if(number == 1) return true;
    return false;
}