import { world } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { mutantData } from "./data/guidebook";

let mutants = mutantData.creatures;
let UIdata = mutantData.UI;

export const ShowMutantsUI = (player) => {
    const actionForm = new ActionFormData()
    .title("Nightmare Craft by Block Perfect Studios");

    for (const page in UIdata) {
        actionForm.button(`${UIdata[page].title}`);
    };

    actionForm.show(player).then((response) => {
        if (response.selection != undefined) {
            let selectedPage = Object.values(UIdata)[response.selection];
            ShowPage(player, selectedPage);
        }
    });
}

function ShowPage(player, page) {
    let buttonCount = 0;
    const actionForm = new ActionFormData()
    .title(page.title)
    .body({ translate: page.description, with: ['\n']});
    if ("subpages" in page) {
        for (const subpage in page.subpages) {
            buttonCount++;
            if ( "icon" in page.subpages[subpage]) {
                actionForm.button(`${page.subpages[subpage].title}`, page.subpages[subpage].icon);
            } else {
                actionForm.button(`${page.subpages[subpage].title}`);
            }
        }

        actionForm.button("Back");

        actionForm.show(player).then((response) => {
            if (response.selection === undefined) {
                return;
            }
            if (response.selection == buttonCount) {
                if ("parent" in page) {
                    ShowPage(player, UIdata[page.parent]);
                } else {
                    ShowMutantsUI(player);
                }
            } else {
                let selectedPage = Object.values(page.subpages)[response.selection];
                if ("spawn_rules" in selectedPage) {
                    ShowMutantSpawnRules(player);
                } else if ("gamemodes" in selectedPage) {
                    ShowGamemodesMenu(player);
                } else if ("general_settings" in selectedPage) {
                    ShowGeneralMenu(player);
                } else {
                    ShowPage(player, selectedPage);
                }
            }
        });
    } else {
        if ("recipe" in page) {
            actionForm.button("Back", page.recipe);
        } else {
            actionForm.button("Back");
        }
        actionForm.show(player).then(() => {
            ShowPage(player, UIdata[page.parent]);
        });
    }
}

function ShowGamemodesMenu(player) {
    let gamemodeObj = world.scoreboard.getObjective("bps_nc.spawn");
    gamemodeObj.addScore('gamemode', 0);

    const modalForm = new ModalFormData()
    .title("Adjust gamemodes:")
    // .body('bps_nc:guidebook.settings.gamemodes.description')
    .dropdown({ translate: 'bps_nc:guidebook.settings.gamemodes.description', with: ['\n']}, ['Easy', 'Normal', 'Hard', 'Nightmare'], gamemodeObj.getScore('gamemode'));

    modalForm
        .show(player)
        .then((response) => {
            if (response.formValues !== undefined) {
                // submitted spawn interval
                player.runCommand(`function bps/nc/spawning/set_gamemode/${response.formValues[0]}`);
                // gamemodeObj.setScore('gamemode', response.formValues[0]);
                ShowPage(player, UIdata["settings"]);
            }
        });
}

function ShowGeneralMenu(player) {
    let hasBookDenyTag = player.hasTag("bps_nc_no_book");

    const modalForm = new ModalFormData()
    .title({ translate: 'bps_nc:guidebook.settings.general_settings.title', with: ['\n']})
    .toggle({ translate: 'bps_nc:guidebook.settings.general_settings.description', with: ['\n']}, !hasBookDenyTag);

    modalForm
        .show(player)
        .then((response) => {
            if (response.formValues !== undefined) {
                if (response.formValues[0] == true) {
                    player.removeTag("bps_nc_no_book");
                } else if (response.formValues[0] == false) {
                    player.addTag("bps_nc_no_book");
                }
                ShowPage(player, UIdata["settings"]);
            }
        });
}

export const ShowMutantSpawnRules = (player) => {
    let buttonCount = 0;
    const actionForm = new ActionFormData()
    .title("Adjust spawn rules for:");
    
    for (const mutant in mutants) {
        buttonCount++;
        actionForm.button(`${mutants[mutant].displayName}`);
    };
    actionForm.button("Back");
    
    actionForm.show(player).then((response) => {
        if (response.selection != undefined) {
            if (response.selection == buttonCount) {
                ShowPage(player, UIdata["settings"]);
            } else {
                ShowMutantSpawnOptions(player, mutants[response.selection]);
            }
        }
    });
}

function ShowMutantSpawnOptions(player, mutant) {
    let spawnIntervalsObj = world.scoreboard.getObjective("bps_nc.spawn");
    let mobCapsObj = world.scoreboard.getObjective("bps_nc.mob_caps");

    const modalForm = new ModalFormData()
        .title(`Adjust spawn rules for ${mutant.displayName}`)
        
        if (mutant.name == "noface") {
            modalForm.slider(`${mutant.displayName} spawn interval (minutes)`, 5, 60, 5, spawnIntervalsObj.getScore(mutant.interval_score_name)/60);
        } else {
            modalForm.slider(`${mutant.displayName} spawn interval`, 30, 200, 10, spawnIntervalsObj.getScore(mutant.interval_score_name));
        }
        modalForm.slider(`${mutant.displayName} mob cap`, 0, 16, 1, mobCapsObj.getScore(mutant.name));

    modalForm
        .show(player)
        .then((response) => {
            if (response.formValues !== undefined) {
                // submitted spawn interval
                spawnIntervalsObj.setScore(mutant.interval_score_name, response.formValues[0]);
                if (mutant.name == "noface")
                    spawnIntervalsObj.setScore(mutant.interval_score_name, response.formValues[0] * 60);
                // submitted mob cap
                mobCapsObj.setScore(mutant.name, response.formValues[1]);
                // ShowMutantsUI(player);
                ShowMutantSpawnRules(player);
                // ShowPage(player, UIdata["settings"]["subpages"]["spawn_rules"]);
            }
        });
}