import { ActionFormData, ModalFormData } from '@minecraft/server-ui';
import { isPlayerOp, translated } from '../../utils/utils.js';
import { PLAYER_SETTINGS, WORLD_SETTINGS } from '../../settings/settings.js';
import { openBook } from '../book.js';

function settingsGUI(player) {
    const isOp = isPlayerOp(player);
    const actionFormData = new ActionFormData()
        .title(translated(bookKey("title")))
        .button(translated(bookKey("player")))
        .button(translated(bookKey("world")))
        .button(translated("ui.ftb_sieves.book_main.button.back"));
    actionFormData.show(player).then(value => {
        if (value.canceled || value.selection === 2) {
            openBook(player, "bookMain");
            return;
        }
        switch (value.selection) {
            case 0:
                playerSettingsGUI(player);
                break;
            case 1:
                if (!isOp) {
                    player.sendMessage(translated(bookKey("ui.ftb_sieves.no_permission")));
                    settingsGUI(player);
                    return;
                }
                worldSettingsGUI(player);
                break;
        }
    });
}
function playerSettingsGUI(player) {
    const modalFormData = new ModalFormData()
        .title(translated(bookKey("player")));
    for (let playersetting of PLAYER_SETTINGS) {
        processSettingType(modalFormData, playersetting, playersetting.getValue(player), "player");
    }
    modalFormData.submitButton(translated("ui.ftb_sieves.book_main.button.back"));
    modalFormData.show(player).then(value => {
        if (!value.canceled) {
            for (let i = 0; i < PLAYER_SETTINGS.length; i++) {
                PLAYER_SETTINGS[i].setValue(player, value.formValues[i]);
            }
        }
        settingsGUI(player);
    });
}
function worldSettingsGUI(player) {
    const modalFormData = new ModalFormData()
        .title(translated(bookKey("world")));
    for (let worldSetting of WORLD_SETTINGS) {
        processSettingType(modalFormData, worldSetting, worldSetting.getValue(), "world");
    }
    modalFormData.show(player).then(value => {
        if (!value.canceled) {
            for (let i = 0; i < WORLD_SETTINGS.length; i++) {
                WORLD_SETTINGS[i].setValue(value.formValues[i]);
            }
        }
        settingsGUI(player);
    });
}
function processSettingType(form, type, value, string) {
    const settingType = typeof type.defaultValue;
    switch (settingType) {
        case "boolean":
            form.toggle(translated(bookKey(string + "." + type.key)), {
                defaultValue: value
            });
            break;
        case "number":
            form.slider(translated(bookKey(string + "." + type.key + ".title")), 0, 100, {
                defaultValue: value,
                tooltip: translated(string + "." + type.key + ".tooltip")
            });
            break;
        default:
            console.warn("Type not supported: " + settingType);
    }
}
function bookKey(string) {
    return `ftb_sieves.book.settings.${string}`;
}

export { settingsGUI };
