import { system, world } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import config from "../config";
import { parameterNames, Settings } from "./scoreboard";
import { Buttons } from "./my_API";
world.beforeEvents.itemUse.subscribe((event) => {
    handleSettingsToolUse(event);
});
function handleSettingsToolUse(event) {
    if (event.itemStack.typeId !== "nps_mot:settings_tool") {
        return;
    }
    const player = event.source;
    const isAdmin = player.hasTag(config.adminTag);
    if (isAdmin) {
        system.run(() => {
            AdminSettingsToolForm(player);
        });
    }
    else {
        system.run(() => {
            SettingsToolForm(player);
        });
    }
}
function SettingsToolForm(player) {
    let form = new ActionFormData();
    form.title({ translate: 'item.nps_mot:settings_tool' });
    form.body({ translate: 'nps_mot:settings_tool.you_are_not_admin' });
    form.button(Buttons.close.rawtext, Buttons.close.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
    });
}
function AdminSettingsToolForm(player) {
    let form = new ActionFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    form.body({ translate: 'nps_mot:form_body.choose_gamerule' });
    const button = Settings.getSetting(parameterNames.apocalysisDifficulty).button;
    const apocalysisDifficulty = Settings.getValue(parameterNames.apocalysisDifficulty);
    switch (apocalysisDifficulty) {
        case 0: {
            const text = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [{ translate: button.rawtext }, { text: '\n§8[§e' }, { translate: Buttons.apocalypse_0.rawtext }, { text: '§8]' }] }
            };
            form.button(text, button.iconPath);
            break;
        }
        case 1: {
            const text = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [{ translate: button.rawtext }, { text: '\n§8[§e' }, { translate: Buttons.apocalypse_1.rawtext }, { text: '§8]' }] }
            };
            form.button(text, button.iconPath);
            break;
        }
        case 2: {
            const text = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [{ translate: button.rawtext }, { text: '\n§8[§e' }, { translate: Buttons.apocalypse_2.rawtext }, { text: '§8]' }] }
            };
            form.button(text, button.iconPath);
            break;
        }
        case 3: {
            const text = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [{ translate: button.rawtext }, { text: '\n§8[§e' }, { translate: Buttons.apocalypse_3.rawtext }, { text: '§8]' }] }
            };
            form.button(text, button.iconPath);
            break;
        }
        case 4: {
            const text = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [{ translate: button.rawtext }, { text: '\n§8[§e' }, { translate: Buttons.apocalypse_4.rawtext }, { text: '§8]' }] }
            };
            form.button(text, button.iconPath);
            break;
        }
    }
    const settingsKeysWithDescriptions = [
        { key: parameterNames.oreGeneration },
        { key: parameterNames.armorEffects },
        { key: parameterNames.jewelDrops },
        { key: parameterNames.monsterDrops },
        { key: parameterNames.repairKitDrops },
        { key: parameterNames.damageIndicators },
        { key: parameterNames.oreMerchantInvocation }
    ];
    settingsKeysWithDescriptions.forEach(({ key }) => {
        const setting = Settings.getSetting(key);
        const currentValue = Settings.getValue(key);
        if (currentValue == 1) {
            const text = {
                translate: "nps_mot:gap_form_text",
                with: {
                    rawtext: [
                        { translate: setting.button.rawtext },
                        { text: '\n' },
                        { translate: 'nps_mot:form_button.toggle_on' }
                    ]
                }
            };
            form.button(text, setting.button.iconPath);
        }
        else {
            const text = {
                translate: "nps_mot:gap_form_text",
                with: {
                    rawtext: [
                        { translate: setting.button.rawtext },
                        { text: '\n' },
                        { translate: 'nps_mot:form_button.toggle_off' }
                    ]
                }
            };
            form.button(text, setting.button.iconPath);
        }
    });
    form.button(Buttons.close.rawtext, Buttons.close.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
        switch (response.selection) {
            case 0: {
                ApocalysisDifficultyForm(player);
                break;
            } /*
            case 1:{
                oreGenerationForm(player);
                break;
            }*/
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7: {
                const selectedSetting = settingsKeysWithDescriptions[response.selection - 1].key;
                SettingsToggleForm(player, selectedSetting);
                break;
            }
            case 8: {
                //close button
                break;
            }
        }
    });
}
function oreGenerationForm(player) {
    let form = new ModalFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    const oreGenerationCurrentValue = Settings.getValue(parameterNames.oreGeneration);
    form.toggle({ translate: Settings.getSetting(parameterNames.oreGeneration).getDescription() }, oreGenerationCurrentValue == 1);
    const oreGenerationDistanceCurrentValue = Settings.getValue(parameterNames.oreGenerationDistance);
    form.slider({ translate: Settings.getSetting(parameterNames.oreGenerationDistance).getDescription() }, 2, 12, 2, oreGenerationDistanceCurrentValue);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        const oreGenerationValue = response.formValues[0];
        if (oreGenerationValue != oreGenerationCurrentValue) {
            const settingId = Settings.getSetting(parameterNames.oreGeneration).id;
            const enableMsg = `nps_mot:${settingId}EnableMessage`;
            const disableMsg = `nps_mot:${settingId}DisableMessage`;
            if (oreGenerationValue) {
                world.sendMessage([config.addonPrefix, { translate: enableMsg }]);
            }
            else {
                world.sendMessage([config.addonPrefix, { translate: disableMsg }]);
            }
            Settings.setValue(parameterNames.oreGeneration, oreGenerationValue);
        }
        const oreGenerationDistance = response.formValues[1];
        if (oreGenerationDistance != oreGenerationDistanceCurrentValue) {
            const message = {
                translate: "nps_mot:oreGenerationDistanceUpdateMessage",
                with: {
                    rawtext: [
                        { text: `${oreGenerationDistance}` }
                    ]
                }
            };
            world.sendMessage([config.addonPrefix, message]);
            Settings.setValue(parameterNames.oreGenerationDistance, oreGenerationDistance);
        }
    });
}
function SettingsToggleForm(player, settingName) {
    let form = new ModalFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    const setting = Settings.getSetting(settingName);
    const currentValue = Settings.getValue(settingName);
    const description = `nps_mot:${setting.id}Description`;
    form.toggle({ translate: description }, currentValue == 1);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        const toggleResponse = response.formValues[0];
        if (toggleResponse != currentValue) {
            const enableMsg = `nps_mot:${setting.id}EnableMessage`;
            const disableMsg = `nps_mot:${setting.id}DisableMessage`;
            if (toggleResponse) {
                world.sendMessage([config.addonPrefix, { translate: enableMsg }]);
            }
            else {
                world.sendMessage([config.addonPrefix, { translate: disableMsg }]);
            }
            Settings.setValue(settingName, toggleResponse);
        }
    });
}
function ApocalysisDifficultyForm(player) {
    let form = new ActionFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    const apocalysisDifficulty = Settings.getValue(parameterNames.apocalysisDifficulty);
    switch (apocalysisDifficulty) {
        case 0: {
            const bodyText = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [
                        { text: '§7Your current difficulty level is: §e' }, { translate: Buttons.apocalypse_0.rawtext }, { text: '§7.\n\n' },
                        { text: "If you'd like to change it, please choose a new difficulty level from the options below:" }
                    ] }
            };
            form.body(bodyText);
            break;
        }
        case 1: {
            const bodyText = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [
                        { text: '§7Your current difficulty level is: §e' }, { translate: Buttons.apocalypse_1.rawtext }, { text: '§7.\n\n' },
                        { text: "If you'd like to change it, please choose a new difficulty level from the options below:" }
                    ] }
            };
            form.body(bodyText);
            break;
        }
        case 2: {
            const bodyText = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [
                        { text: '§7Your current difficulty level is: §e' }, { translate: Buttons.apocalypse_2.rawtext }, { text: '§7.\n\n' },
                        { text: "If you'd like to change it, please choose a new difficulty level from the options below:" }
                    ] }
            };
            form.body(bodyText);
            break;
        }
        case 3: {
            const bodyText = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [
                        { text: '§7Your current difficulty level is: §e' }, { translate: Buttons.apocalypse_3.rawtext }, { text: '§7.\n\n' },
                        { text: "If you'd like to change it, please choose a new difficulty level from the options below:" }
                    ] }
            };
            form.body(bodyText);
            break;
        }
        case 4: {
            const bodyText = {
                translate: "nps_mot:gap_form_text",
                with: { rawtext: [
                        { text: '§7Your current difficulty level is: §e' }, { translate: Buttons.apocalypse_4.rawtext }, { text: '§7.\n\n' },
                        { text: "If you'd like to change it, please choose a new difficulty level from the options below:" }
                    ] }
            };
            form.body(bodyText);
            break;
        }
    }
    form.button(Buttons.apocalypse_0.rawtext, Buttons.apocalypse_0.iconPath);
    form.button(Buttons.apocalypse_1.rawtext, Buttons.apocalypse_1.iconPath);
    form.button(Buttons.apocalypse_2.rawtext, Buttons.apocalypse_2.iconPath);
    form.button(Buttons.apocalypse_3.rawtext, Buttons.apocalypse_3.iconPath);
    form.button(Buttons.apocalypse_4.rawtext, Buttons.apocalypse_4.iconPath);
    form.button(Buttons.back.rawtext, Buttons.back.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
        switch (response.selection) {
            case 0: {
                zeroApocalypseForm(player);
                break;
            }
            case 1: {
                impossibleApocalypseForm(player);
                break;
            }
            case 2: {
                veteranApocalypseForm(player);
                break;
            }
            case 3: {
                dementialApocalypseForm(player);
                break;
            }
            case 4: {
                ultimateApocalypseForm(player);
                break;
            }
            case 5: {
                SettingsToolForm(player);
                break;
            }
        }
    });
}
function zeroApocalypseForm(player) {
    let form = new ActionFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    form.body("§l§6Looking for an adventure without hurdles?§r\n" +
        "§7This mode is for you. No additional difficulties will hinder your progress.§r\n" +
        "§7But don't worry, there will still be plenty of rewards to reap!§r\n\n" +
        "§eMOT Loot Quality: §6+10%%§r\n" +
        "§eMob Resistance: §6+0%%§r\n" +
        "§cMob Attack: Coming Soon");
    form.button(Buttons.apocalypse_0.rawtext, Buttons.apocalypse_0.iconPath);
    form.button(Buttons.back.rawtext, Buttons.back.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
        switch (response.selection) {
            case 0: {
                world.sendMessage([config.addonPrefix, { translate: `nps_mot:apocalypse0Set` }]);
                Settings.setValue(parameterNames.apocalysisDifficulty, 0);
                break;
            }
            case 1: {
                ApocalysisDifficultyForm(player);
                break;
            }
        }
    });
}
function impossibleApocalypseForm(player) {
    let form = new ActionFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    form.body("§l§6Finding the game too easy?§r\n" +
        "§7Take on the challenge with this mode. Expect a few extra obstacles to spice up the adventure, but nothing too frightening.§r\n" +
        "§7Creepers shouldn't make you tremble... at least, not too much.§r\n\n" +
        "§eMOT Loot Quality: §6+40%%§r\n" +
        "§eMob Resistance: §6+37%%§r\n" +
        "§cMob Attack: Coming Soon");
    form.button(Buttons.apocalypse_1.rawtext, Buttons.apocalypse_1.iconPath);
    form.button(Buttons.back.rawtext, Buttons.back.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
        switch (response.selection) {
            case 0: {
                world.sendMessage([config.addonPrefix, { translate: `nps_mot:apocalypse1Set` }]);
                Settings.setValue(parameterNames.apocalysisDifficulty, 1);
                break;
            }
            case 1: {
                ApocalysisDifficultyForm(player);
                break;
            }
        }
    });
}
function veteranApocalypseForm(player) {
    let form = new ActionFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    form.body("§l§6Master Minecraft like the back of your hand?§r\n" +
        "§7It's time to prove your worth. This mode offers a real challenge, designed for those who seek excellence and difficulty. Are you ready to take up the gauntlet?§r\n\n" +
        "§eMOT Loot Quality: §6+100%%§r\n" +
        "§eMob Resistance: §6+83%%§r\n" +
        "§cMob Attack: Coming Soon");
    form.button(Buttons.apocalypse_2.rawtext, Buttons.apocalypse_2.iconPath);
    form.button(Buttons.back.rawtext, Buttons.back.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
        switch (response.selection) {
            case 0: {
                world.sendMessage([config.addonPrefix, { translate: `nps_mot:apocalypse2Set` }]);
                Settings.setValue(parameterNames.apocalysisDifficulty, 2);
                break;
            }
            case 1: {
                ApocalysisDifficultyForm(player);
                break;
            }
        }
    });
}
function dementialApocalypseForm(player) {
    let form = new ActionFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    form.body("§l§6Nothing scares you anymore?§r\n" +
        "§7Dive into this extreme mode where every creature is a deadly threat. Beware: here, even the smallest mob can make you regret logging in. Only the bravest will survive!§r\n\n" +
        "§eMOT Loot Quality: §c+200%%§r\n" +
        "§eMob Resistance: §c+175%%§r\n" +
        "§cMob Attack: Coming Soon");
    form.button(Buttons.apocalypse_3.rawtext, Buttons.apocalypse_3.iconPath);
    form.button(Buttons.back.rawtext, Buttons.back.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
        switch (response.selection) {
            case 0: {
                world.sendMessage([config.addonPrefix, { translate: `nps_mot:apocalypse3Set` }]);
                Settings.setValue(parameterNames.apocalysisDifficulty, 3);
                break;
            }
            case 1: {
                ApocalysisDifficultyForm(player);
                break;
            }
        }
    });
}
function ultimateApocalypseForm(player) {
    let form = new ActionFormData();
    form.title({ translate: `item.nps_mot:settings_tool` });
    form.body("§l§6Ready to dive into total chaos?§r\n" +
        "§7This mode is for the truly daring. The challenges are relentless, and even the smallest mistake can be fatal. Every moment is a fight for survival, where perfection is your only ally. Will you dare to face the Ultimate Apocalypse: Carnage and Perfection?§r\n\n" +
        "§eMOT Loot Quality: §c+500%%§r\n" +
        "§eMob Resistance: §c+450%%§r\n" +
        "§cMob Attack: Coming Soon");
    form.button(Buttons.apocalypse_4.rawtext, Buttons.apocalypse_4.iconPath);
    form.button(Buttons.back.rawtext, Buttons.back.iconPath);
    form.show(player).then(response => {
        if (response.canceled) {
            return;
        }
        if (response.selection == undefined) {
            return;
        }
        switch (response.selection) {
            case 0: {
                world.sendMessage([config.addonPrefix, { translate: `nps_mot:apocalypse4Set` }]);
                Settings.setValue(parameterNames.apocalysisDifficulty, 4);
                break;
            }
            case 1: {
                ApocalysisDifficultyForm(player);
                break;
            }
        }
    });
}
