import { Player } from "@minecraft/server";
import { FormCancelationReason, MessageFormData, ModalFormData } from "@minecraft/server-ui";
import {
    getPlayerConfigurations,
    updatePlayerConfigurations,
} from "../controllers/entities_controller";

/**
 *
 * @param {Player} player
 */
export function configurations(player) {
    const UI = new ModalFormData();

    const playerConfigurations = getPlayerConfigurations(player);

    const JumpscareCameraToggle = playerConfigurations.jumpscare_cam;
    const DynamicLightToggle = playerConfigurations.dynamic_light;
    const NauseaToggle = playerConfigurations.enable_mobs_cause_nausea;
    const FogToggle = playerConfigurations.enable_fog;
    const BiomeVisualEffectsToggle = playerConfigurations.enable_biome_visual_effects;
    const BiomeSoundEffectsToggle = playerConfigurations.enable_biome_sound_effects;

    UI.title("CONFIGURATIONS");

    UI.toggle("Enable Jumpscare Camera", JumpscareCameraToggle);
    UI.toggle("Enable Dynamic Light", DynamicLightToggle);
    UI.toggle("Enable Mobs Cause Nausea Effect", NauseaToggle);
    UI.toggle("Enable Fog", FogToggle);
    UI.toggle("Enable Biomes Visual Effects", BiomeVisualEffectsToggle);
    UI.toggle("Enable Biomes Sound Effects", BiomeSoundEffectsToggle);

    UI.submitButton("Confirm");

    UI.show(player).then((response) => {
        if (
            response.canceled == true ||
            response.cancelationReason == FormCancelationReason.UserClosed
        ) {
            return;
        }
        if (response.formValues) {
            const changes = {
                jumpscare_cam: response.formValues[0],
                dynamic_light: response.formValues[1],
                enable_mobs_cause_nausea: response.formValues[2],
                enable_fog: response.formValues[3],
                enable_biome_visual_effects: response.formValues[4],
                enable_biome_sound_effects: response.formValues[5],
            };

            if (response.formValues[0] != undefined) {
                confirmMenu(player, changes);
            }
        }
    });
}

/**
 *
 * @param {Player} player
 */
function confirmMenu(player, changes) {
    const UI = new MessageFormData();

    UI.title("");
    UI.body("Are you sure you want to apply the changes?");

    UI.button1("Yes");
    UI.button2("No");

    UI.show(player).then((response) => {
        if (
            response.canceled == true ||
            response.cancelationReason == FormCancelationReason.UserClosed
        ) {
            return;
        }

        if (response.selection == 0) {
            updatePlayerConfigurations(player, changes);
        }
        if (response.selection == 1) {
            return;
        }
    });
}

