import * as ui from "@minecraft/server-ui";
import { world } 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_FOG_SETTINGS = new ui.ActionFormData()

export const biomeFogMap = new Map([
    ["taiga", "cyd_dino:fog_cold"],
    ["frozen", "cyd_dino:fog_cold"],
    ["desert", "cyd_dino:fog_warm"],
    ["mesa", "cyd_dino:fog_warm"],
    ["savanna", "cyd_dino:fog_warm"],
    ["jungle", "cyd_dino:fog_jungle"],
    ["default", "cyd_dino:fog_default"]
])

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/dino/entity/items/spawn_eggs/cyd_dino_tyranosaurus");

    //BOOK DINO
    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/dino/entity/items/misc/cyd_dino_dino_taming_manual");

    MENU_FOG_SETTINGS.title({ text: '(Primal Overworld) Settings' })
    MENU_FOG_SETTINGS.button({
        text: `Toggle Fog`
    })
}

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;
            default: break;
        }
    });
}

export function showFogMenuSettings(player) {
    player.playSound(SOUND_TURN_PAGE, SOUND_OPTIONS)

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

            if (result.selection === 0) {
                const currStatus = world.getDynamicProperty('cyd_dino:enabled_fog') ?? false
                const newStatus = !currStatus

                world.setDynamicProperty('cyd_dino:enabled_fog', newStatus)
            }
        })
}

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;
        }
    });
}