import * as Minecraft from "@minecraft/server"
import * as MinecraftUi from "@minecraft/server-ui"
import { showSettings, Overlay } from "./shield.js";

/**
 * 
 * @param {Minecraft.Player} player 
 */


// === CONSTANTS ===
const VERSION = { translate: "aria_ah.ui.interface.constant.version", with: ["1.0.0"] };
const WIKI_LINK = { translate: "aria_ah.ui.interface.constant.wiki", with: ["ariamc.link/wiki"] };
const DISCORD_LINK = { translate: "aria_ah.ui.interface.constant.discord", with: ["ariamc.link/discord"] };
const WELCOME_TEXT = { rawtext: [WIKI_LINK, DISCORD_LINK] };

const CONTACT_EMAIL = "contact@ariacreations.net"
const CONTACT_WEBSITE = "ariacreations.net"


//§7\n${VERSION}\n by AriaCreations!§r.\n §rWelcome to §c§lAnimal Horror Add-on! Explore all features, customize your gameplay, and connect with our community


// === MAIN MENU TRIGGER ===
Minecraft.world.afterEvents.itemUse.subscribe((event) => {
    if (event.itemStack.typeId === "aria_ah:menu") {
        openMainMenu(event.source)
    }
})

// === MAIN MENU ===
function openMainMenu(player) {
    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.main" }] })
        .body(WELCOME_TEXT)
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.main.how_to_play" }]}, "textures/aria/ah/ui/play")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.main.settings" }]}, "textures/aria/ah/ui/settings")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.main.stats" }]}, "textures/aria/ah/ui/stats")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.main.contact" }]}, "textures/aria/ah/ui/contact")
        .show(player).then(res => {
            if (res.canceled) return;

            if (res.selection === 0) {
                openHowToPlay(player);
            } else if (res.selection === 1) {
                openSettingsMenu(player);
            } else if (res.selection === 2) {
                openStatisticSettings(player);
            } else if (res.selection === 3) {
                openContact(player);
            }
        });
}

function openHowToPlay(player) {
    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.how_to_play" }]})
        .body({
            rawtext: [
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_1"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_2"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_3"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_4"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_5"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_6"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_7"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_8"
                },
                {
                    translate: "aria_ah.ui.interface.description.how_to_play.part_9"
                }
            ]
        })
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then(res => {
            if (res.canceled || res.selection === 0) openMainMenu(player);
        });
}

function openContact(player) {
    const contactText = {
        rawtext: [
            VERSION,
            DISCORD_LINK,
            WIKI_LINK,
            { translate: "aria_ah.ui.interface.description.contact.website", with: [CONTACT_WEBSITE] },
            { translate: "aria_ah.ui.interface.description.contact.email", with: [CONTACT_EMAIL] },
            { translate: "aria_ah.ui.interface.description.contact.join_discord" },
            { translate: "aria_ah.ui.interface.description.contact.personal_support" }
        ]
    };

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.contact" }]})
        .body(contactText)
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player)
        .then(res => {
            if (res.canceled || res.selection === 0) {
                openMainMenu(player, false);
            }
        });
}

function openSettingsMenu(player) {
    const generalScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.general");
    const optionsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.options");
    const difficulty = optionsScoreboard.getScore("difficulty");
    const jumpScareSetting = optionsScoreboard.getScore("jump_scare");
    const fogSetting = generalScoreboard.getScore("fog_setting")

    const form = new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.settings" }]})
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.difficulty", with: { rawtext: [{ translate: "aria_ah.ui.interface.button.settings.difficulty." + ["spooky", "horror", "nightmare"][difficulty - 1] }] } }]
        }, "textures/aria/ah/ui/difficulty")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.jump_scare", with: { rawtext: [{ translate: "aria_ah.ui.interface.general." + ["off", "on"][jumpScareSetting] }] } }]
        }, "textures/aria/ah/ui/jumpscare")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.sky_moon_vfx", with: { rawtext: [{ text: Overlay.getRenderDistance(player).toString() }] } }]
        }, "textures/aria/ah/ui/moon")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.distant_fog", with: { rawtext: [{ translate: "aria_ah.ui.interface.button.settings.distant_fog." + ["no_fog", "spooky", "horror", "nightmare", "dynamic"][fogSetting] }] } }]
        }, "textures/aria/ah/ui/fog")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.particles" }]
        }, "textures/aria/ah/ui/particles")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.spawn_rules" }]
        }, "textures/aria/ah/ui/slider")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.night_weight" }]
        }, "textures/aria/ah/ui/weight")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.general" }]
        }, "textures/aria/ah/ui/settings")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.horror_biome" }]
        }, "textures/aria/ah/ui/biome")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.sounds" }]
        }, "textures/aria/ah/ui/sound")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.buffs" }]
        }, "textures/aria/ah/ui/buffs");

    if (player.getGameMode() == Minecraft.GameMode.Creative) {
        form.button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.settings.cheats" }]
        }, "textures/aria/ah/ui/jumpscare");
    }

    form.button({
        rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]
    }, "textures/aria/ah/ui/back");

    form.show(player).then((res) => {
        if (res.canceled) return;

        const isCreative = player.getGameMode() == Minecraft.GameMode.Creative;
        const backIndex = isCreative ? 12 : 11;
        const selection = res.selection;

        if (selection === 0) openDifficultySettings(player);
        else if (selection === 1) openJumpScareSettings(player);
        else if (selection === 2) showSettings(player);
        else if (selection === 3) openFogSettings(player);
        else if (selection === 4) openParticlesSettings(player);
        else if (selection === 5) openSpawnRulesMenu(player);
        else if (selection === 6) openNightWeightSettings(player);
        else if (selection === 7) openGeneralSettings(player);
        else if (selection === 8) openHorrorBiomeSettings(player); // New handler
        else if (selection === 9) openSoundsSettings(player);
        else if (selection === 10) openBuffsSettings(player);
        else if (selection === 11 && isCreative) openCheatsSettings(player);
        else if (selection === backIndex) openMainMenu(player);
    });
}

// ===============================
// === HORROR BIOME SETTINGS   ===
// ===============================
function openHorrorBiomeSettings(player) {
    const biomeScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.biome_horror");
    const fog = biomeScoreboard.getScore("fog_env_fill");
    const cat = biomeScoreboard.getScore("animal_cat");
    const catMorph = biomeScoreboard.getScore("animal_cat_morph");
    const bossArmor = biomeScoreboard.getScore("boss_armor");
    const greatSword = biomeScoreboard.getScore("great_sword");
    const tentacleSword = biomeScoreboard.getScore("tentacle_sword");
    const generator = biomeScoreboard.getScore("generator");
    const bomb = biomeScoreboard.getScore("bomb");
    const extinguishLight = biomeScoreboard.getScore("extinguish_light");

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.horror_biome" }]})
        .body({ rawtext: [{ translate: "aria_ah.ui.interface.description.horror_biome" }]})
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.fog" }]
        }, fog == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.cat" }]
        }, cat == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.cat_morph" }]
        }, catMorph == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.boss_armor" }]
        }, bossArmor == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.great_sword" }]
        }, greatSword == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.tentacle_sword" }]
        }, tentacleSword == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.wave_generator" }]
        }, generator == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.bomb" }]
        }, bomb == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.horror_biome.extinguish_light" }]
        }, extinguishLight == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.general.enable_all" }]
        }, "textures/aria/ah/ui/on")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.general.disable_all" }]
        }, "textures/aria/ah/ui/off")
        .button({
            rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]
        }, "textures/aria/ah/ui/back")
        .show(player).then(res => {
            if (res.canceled) return;

            if (res.selection === 0) {
                biomeScoreboard.setScore("fog_env_fill", fog == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 1) {
                biomeScoreboard.setScore("animal_cat", cat == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 2) {
                biomeScoreboard.setScore("animal_cat_morph", catMorph == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 3) {
                biomeScoreboard.setScore("boss_armor", bossArmor == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 4) {
                biomeScoreboard.setScore("great_sword", greatSword == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 5) {
                biomeScoreboard.setScore("tentacle_sword", tentacleSword == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 6) {
                biomeScoreboard.setScore("generator", generator == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 7) {
                biomeScoreboard.setScore("bomb", bomb == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 8) {
                biomeScoreboard.setScore("extinguish_light", extinguishLight == 1 ? 0 : 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 9) {
                // Enable all toggles
                biomeScoreboard.setScore("fog_env_fill", 1);
                biomeScoreboard.setScore("animal_cat", 1);
                biomeScoreboard.setScore("animal_cat_morph", 1);
                biomeScoreboard.setScore("boss_armor", 1);
                biomeScoreboard.setScore("great_sword", 1);
                biomeScoreboard.setScore("tentacle_sword", 1);
                biomeScoreboard.setScore("generator", 1);
                biomeScoreboard.setScore("bomb", 1);
                biomeScoreboard.setScore("extinguish_light", 1);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 10) {
                // Disable all toggles
                biomeScoreboard.setScore("fog_env_fill", 0);
                biomeScoreboard.setScore("animal_cat", 0);
                biomeScoreboard.setScore("animal_cat_morph", 0);
                biomeScoreboard.setScore("boss_armor", 0);
                biomeScoreboard.setScore("great_sword", 0);
                biomeScoreboard.setScore("tentacle_sword", 0);
                biomeScoreboard.setScore("generator", 0);
                biomeScoreboard.setScore("bomb", 0);
                biomeScoreboard.setScore("extinguish_light", 0);
                openHorrorBiomeSettings(player);
            } else if (res.selection === 11) {
                openSettingsMenu(player);
            }
        });
}

// === SKY+MOON VFX SETTINGS ===
// ===========================
function openSkyMoonVFXSettings(player) {
    
    const currentFlags = player.getDynamicProperty(SettingsProperty) || 0;
    const settings = MoonVFXHandler.parseFlags(currentFlags);

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.sky_moon_vfx" }]})
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.skybox_effect" }]}, { defaultValue: settings.skybox })
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.moon_glow" }]}, { defaultValue: settings.moon })
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.lens_flare" }]}, { defaultValue: settings.flare })
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.light_glare" }]}, { defaultValue: settings.glare })
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.celestial_shapes" }]}, { defaultValue: settings.shape })
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.high_resolution" }]}, { defaultValue: settings.resolution })
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then(res => {
            if (res.canceled) return;
            
            if (res.selection === 6) { // Back button
                openSettingsMenu(player);
                return;
            }

            // Update settings from toggles
            const newSettings = {
                skybox: res.formValues[0],
                moon: res.formValues[1],
                flare: res.formValues[2],
                glare: res.formValues[3],
                shape: res.formValues[4],
                resolution: res.formValues[5]
            };

            // Save updated flags
            const newFlags = MoonVFXHandler.serializeFlags(newSettings);
            player.setDynamicProperty(SettingsProperty, newFlags);

            // Refresh VFX entities
            MoonVFXHandler.refreshPlayerVFX(player);
            
            openSkyMoonVFXSettings(player);
        });
}


// ===========================
// === SPAWN RULES SYSTEM ===
// ===========================
function openSpawnRulesMenu(player) {
    const generalScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.general");
    const disableSpawningSetting = generalScoreboard.getScore("disable_spawning");

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.spawn_rules" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.spawn_rules.blood_night" }]}, "textures/aria/ah/ui/blood")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.spawn_rules.cyan_night" }]}, "textures/aria/ah/ui/cyan")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.spawn_rules.blackhole_night" }]}, "textures/aria/ah/ui/black")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.spawn_rules.pink_night" }]}, "textures/aria/ah/ui/pink")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.spawn_rules.invisible_night" }]}, "textures/aria/ah/ui/invisible")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.spawn_rules.reset_all" }]}, "textures/aria/ah/ui/reset")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.fog_spawning" }]}, disableSpawningSetting == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then(res => {
            if (res.canceled) return;
            switch(res.selection) {
                case 0: 
                    openBloodNightSettings(player);
                    break;
                case 1: 
                    openCyanNightSettings(player);
                    break;
                case 2: 
                    openBlackholeNightSettings(player);
                    break;
                case 3: 
                    openPinkNightSettings(player);
                    break;
                case 4: 
                    openInvisibleNightSettings(player);
                    break;
                case 5: 
                    // Confirmation dialog for reset
                    new MinecraftUi.MessageFormData()
                        .body({ rawtext: [{ translate: "aria_ah.ui.interface.dialog.reset_spawn_rules" }]})
                        .button1({ rawtext: [{ translate: "aria_ah.ui.interface.general.cancel" }]})
                        .button2({ rawtext: [{ translate: "aria_ah.ui.interface.general.confirm" }]})
                        .show(player).then((res) => {
                            if (res.canceled) return;
                            if (res.selection == 1) {
                                player.runCommand("function aria/ah/spawnrules_reset");
                                player.sendMessage({ rawtext: [{ translate: "aria_ah.ui.interface.message.spawn_rules_reset" }]});
                            }
                        });
                    break;
                case 6:
                    generalScoreboard.setScore("disable_spawning", 1 - disableSpawningSetting);
                    openSpawnRulesMenu(player);
                    break;
                case 7: 
                    openSettingsMenu(player);
                    break;
            }
        });
}

// ======================
// === BLOOD NIGHT ===
// ======================
function openBloodNightSettings(player) {
    const spawnRules = Minecraft.world.scoreboard.getObjective("aria_ah.spawn_rules");
    const options = Minecraft.world.scoreboard.getObjective("aria_ah.options");

    new MinecraftUi.ModalFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.blood_night_rules" }]})
        // Grouped fog settings
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fog_max_amount" }]}, 0, 10, { defaultValue: spawnRules.getScore("blood_fog_amount") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fog_spawn_chance" }]}, 0, 100, { defaultValue: spawnRules.getScore("blood_fog_spawn_chance") })
        // Creature settings
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.creature_max_amount" }]}, 0, 40, { defaultValue: spawnRules.getScore("blood_creature_amount") })
        .show(player).then(res => {
            if (res.canceled) return;
            
            const values = res.formValues;
            spawnRules.setScore("blood_fog_amount", values[0]);
            spawnRules.setScore("blood_fog_spawn_chance", values[1]);
            spawnRules.setScore("blood_creature_amount", values[2]);
            
            openSpawnRulesMenu(player);
        });
}


// ======================
// === CYAN NIGHT ===
// ======================
function openCyanNightSettings(player) {
    const spawnRules = Minecraft.world.scoreboard.getObjective("aria_ah.spawn_rules");

    new MinecraftUi.ModalFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.cyan_night_rules" }]})
        // Grouped: Creature amount and spawn chance
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.creature_max_amount_50" }]}, 0, 50, { defaultValue: spawnRules.getScore("cyan_creature_amount") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.creature_spawn_chance" }]}, 0, 100, { defaultValue: spawnRules.getScore("cyan_creature_spawn_chance") })
        // Animal weights, all on one line for each
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.cat_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_cat_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.chicken_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_chicken_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.pig_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_pig_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.sheep_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_sheep_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.cow_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_cow_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fox_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_fox_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.horse_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_horse_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.rabbit_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_rabbit_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.wolf_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cyan_wolf_weight") })
        .show(player).then(res => {
            if (res.canceled) return;
            
            const values = res.formValues;
            spawnRules.setScore("cyan_creature_amount", values[0]);
            spawnRules.setScore("cyan_creature_spawn_chance", values[1]);
            spawnRules.setScore("cyan_cat_weight", values[2]);
            spawnRules.setScore("cyan_chicken_weight", values[3]);
            spawnRules.setScore("cyan_pig_weight", values[4]);
            spawnRules.setScore("cyan_sheep_weight", values[5]);
            spawnRules.setScore("cyan_cow_weight", values[6]);
            spawnRules.setScore("cyan_fox_weight", values[7]);
            spawnRules.setScore("cyan_horse_weight", values[8]);
            spawnRules.setScore("cyan_rabbit_weight", values[9]);
            spawnRules.setScore("cyan_wolf_weight", values[10]);
            
            openSpawnRulesMenu(player);
        });
}


function openBlackholeNightSettings(player) {
    const spawnRules = Minecraft.world.scoreboard.getObjective("aria_ah.spawn_rules");
    const corpseMob = Minecraft.world.scoreboard.getObjective("aria_ah.corpse_mob");

    // Determine which corpse mob is currently active
    const currentCorpseType = 
        corpseMob.getScore("animal") === 1 ? 0 :
        corpseMob.getScore("mob") === 1 ? 1 : 2;

    new MinecraftUi.ModalFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.blackhole_night_rules" }]})
        // Grouped: Fog amount and spawn chance
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fog_max_amount" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_fog_amount") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fog_spawn_chance" }]}, 0, 100, { defaultValue: spawnRules.getScore("blackhole_fog_spawn_chance") })
        // Grouped: Creature amount and spawn chance
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.creature_max_amount" }]}, 0, 40, { defaultValue: spawnRules.getScore("blackhole_creature_amount") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.creature_spawn_chance" }]}, 0, 100, { defaultValue: spawnRules.getScore("blackhole_creature_spawn_chance") })
        // Animal weights, all on one line for each
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.cat_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_cat_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.chicken_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_chicken_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.pig_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_pig_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.sheep_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_sheep_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.cow_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_cow_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fox_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_fox_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.horse_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_horse_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.rabbit_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_rabbit_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.wolf_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("blackhole_wolf_weight") })
        // Corpse mob dropdown
        .dropdown({ rawtext: [{ translate: "aria_ah.ui.interface.dropdown.fog_attacks_family" }]}, [
            { rawtext: [{ translate: "aria_ah.ui.interface.dropdown.animal" }]}, 
            { rawtext: [{ translate: "aria_ah.ui.interface.dropdown.mob" }]}, 
            { rawtext: [{ translate: "aria_ah.ui.interface.dropdown.monster" }]}
        ], { defaultValueIndex: currentCorpseType })
        .show(player).then(res => {
            if (res.canceled) return;

            const values = res.formValues;
            spawnRules.setScore("blackhole_fog_amount", values[0]);
            spawnRules.setScore("blackhole_fog_spawn_chance", values[1]);
            spawnRules.setScore("blackhole_creature_amount", values[2]);
            spawnRules.setScore("blackhole_creature_spawn_chance", values[3]);
            spawnRules.setScore("blackhole_cat_weight", values[4]);
            spawnRules.setScore("blackhole_chicken_weight", values[5]);
            spawnRules.setScore("blackhole_pig_weight", values[6]);
            spawnRules.setScore("blackhole_sheep_weight", values[7]);
            spawnRules.setScore("blackhole_cow_weight", values[8]);
            spawnRules.setScore("blackhole_fox_weight", values[9]);
            spawnRules.setScore("blackhole_horse_weight", values[10]);
            spawnRules.setScore("blackhole_rabbit_weight", values[11]);
            spawnRules.setScore("blackhole_wolf_weight", values[12]);

            // Set only one corpse mob type to 1
            corpseMob.setScore("animal", 0);
            corpseMob.setScore("mob", 0);
            corpseMob.setScore("monster", 0);
            if (values[13] === 0) corpseMob.setScore("animal", 1);
            else if (values[13] === 1) corpseMob.setScore("mob", 1);
            else corpseMob.setScore("monster", 1);

            openSpawnRulesMenu(player);
        });
}

// ============================
// === INVISIBLE NIGHT ===
// ============================
function openInvisibleNightSettings(player) {
    const spawnRules = Minecraft.world.scoreboard.getObjective("aria_ah.spawn_rules");
    const options = Minecraft.world.scoreboard.getObjective("aria_ah.options");

    new MinecraftUi.ModalFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.invisible_night_rules" }]}) // §8 = dark gray
        // Fog Settings
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fog_max_amount" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_fog_amount") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fog_spawn_chance" }]}, 0, 100, { defaultValue: spawnRules.getScore("cave_fog_spawn_chance") })
        // Creature Settings
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.creature_max_amount" }]}, 0, 40, { defaultValue: spawnRules.getScore("cave_creature_amount") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.creature_spawn_chance" }]}, 0, 100, { defaultValue: spawnRules.getScore("cave_creature_spawn_chance") })
        // Animal Weights
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.cat_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_cat_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.chicken_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_chicken_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.pig_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_pig_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.sheep_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_sheep_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.cow_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_cow_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.fox_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_fox_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.horse_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_horse_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.rabbit_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_rabbit_weight") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.wolf_weight" }]}, 0, 10, { defaultValue: spawnRules.getScore("cave_wolf_weight") })
        .show(player).then(res => {
            if (res.canceled) return;

            const values = res.formValues;
            // Update fog settings
            spawnRules.setScore("cave_fog_amount", values[0]);
            spawnRules.setScore("cave_fog_spawn_chance", values[1]);
            
            // Update creature settings
            spawnRules.setScore("cave_creature_amount", values[2]);
            spawnRules.setScore("cave_creature_spawn_chance", values[3]);
            
            // Update animal weights
            spawnRules.setScore("cave_cat_weight", values[4]);
            spawnRules.setScore("cave_chicken_weight", values[5]);
            spawnRules.setScore("cave_pig_weight", values[6]);
            spawnRules.setScore("cave_sheep_weight", values[7]);
            spawnRules.setScore("cave_cow_weight", values[8]);
            spawnRules.setScore("cave_fox_weight", values[9]);
            spawnRules.setScore("cave_horse_weight", values[10]);
            spawnRules.setScore("cave_rabbit_weight", values[11]);
            spawnRules.setScore("cave_wolf_weight", values[12]);

            openSpawnRulesMenu(player);
        });
}

function openPinkNightSettings(player) {
    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.pink_night" }]})
        .body({ rawtext: [{ translate: "aria_ah.ui.interface.description.pink_night" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then(res => {
            if (!res.canceled) openSpawnRulesMenu(player);
        });
}

// ========================
// === PLACEHOLDER MENUS ===
// ========================
function showPlaceholderMenu(player, nightType) {
    new MinecraftUi.ActionFormData()
        .title(`§l${nightType.toUpperCase()} NIGHT`)
        .body(`§7Configuration for §f${nightType} Night§7\n§7will be available in future updates!`)
        .button("§lBack", "textures/aria/ah/ui/back")
        .show(player).then(res => {
            if (!res.canceled) openSpawnRulesMenu(player);
        });
}
    
function openFogSettings(player) {
    const generalScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.general")
    const fogSetting = generalScoreboard.getScore("fog_setting")

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.fog_settings" }]})
        .body({ rawtext: [{ translate: "aria_ah.ui.interface.description.fog_settings" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.fog.dynamic" }]}, fogSetting == 4 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.fog.nightmare" }]}, fogSetting == 3 ?"textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.fog.horror" }]}, fogSetting == 2 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.fog.spooky" }]}, fogSetting == 1 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.fog.none" }]}, fogSetting == 0 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return

            if (res.selection == 5) {
                openSettingsMenu(player)
                return
            } if (res.selection == 0) {
                generalScoreboard.setScore("fog_setting", 4)
            } else if (res.selection == 1) {
                generalScoreboard.setScore("fog_setting", 3)
            } else if (res.selection == 2) {
                generalScoreboard.setScore("fog_setting", 2)
            } else if (res.selection == 3) {
                generalScoreboard.setScore("fog_setting", 1)
            } else if (res.selection == 4) {
                generalScoreboard.setScore("fog_setting", 0)
            }
            openFogSettings(player)

            player.runCommand("function aria/ah/settings/check_fog")
        })
}

function openDifficultySettings(player) {
    const optionsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.options");
    const difficulty = optionsScoreboard.getScore("difficulty");

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.difficulty_settings" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.difficulty.nightmare" }]}, difficulty == 3 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.difficulty.horror" }]}, difficulty == 2 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.difficulty.spooky" }]}, difficulty == 1 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return;

            if (res.selection == 3) {
                openSettingsMenu(player, false);
                return;
            }

            const selectedDifficulty = 3 - res.selection;
            optionsScoreboard.setScore("difficulty", selectedDifficulty);
            openDifficultySettings(player);
        });
}

function openParticlesSettings(player) {
    const generalScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.general");
    const fog_1Setting = generalScoreboard.getScore("fog_1");
    const redPlants = generalScoreboard.getScore("red_plants");
    const darkLeaves = generalScoreboard.getScore("dark_leaves");
    const shieldGenerator = generalScoreboard.getScore("shield_generator");
    const waveGenerator = generalScoreboard.getScore("wave_generator");
    const fireflyBush = generalScoreboard.getScore("firefly_bush");

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.button.settings.particles" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.fog.particle" }]}, fog_1Setting == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.particles.red" }]}, redPlants == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.particles.dark" }]}, darkLeaves == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.particles.shield_generator" }] }, shieldGenerator == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.particles.wave_generator" }]}, waveGenerator == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.particles.firefly_bush" }]}, fireflyBush == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return;

            if (res.selection == 0) {
                generalScoreboard.setScore("fog_1", 1 - fog_1Setting)
            } else if (res.selection == 1) {
                generalScoreboard.setScore("red_plants", 1 - redPlants);
            } else if (res.selection == 2) {
                generalScoreboard.setScore("dark_leaves", 1 - darkLeaves);
            } else if (res.selection == 3) {
                generalScoreboard.setScore("shield_generator", 1 - shieldGenerator);
            } else if (res.selection == 4) {
                generalScoreboard.setScore("wave_generator", 1 - waveGenerator);
            } else if (res.selection == 5) {
                generalScoreboard.setScore("firefly_bush", 1 - fireflyBush);
            } else if (res.selection == 6) {
                openSettingsMenu(player, false);
                return;
            }
            
            openParticlesSettings(player);
        });
}

function openJumpScareSettings(player) {
    const optionsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.options")
    const jumpScare = optionsScoreboard.getScore("jump_scare")

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.jump_scare_settings" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.jump_scare.on" }]}, jumpScare == 1 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.jump_scare.off" }]}, jumpScare == 0 ? "textures/aria/ah/ui/arrow" : "textures/aria/ah/ui/empty")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return

            if (res.selection == 0) {
                optionsScoreboard.setScore("jump_scare", 1)
                openJumpScareSettings(player)
            } else if (res.selection == 1) {
                new MinecraftUi.MessageFormData()
                    .body({ rawtext: [{ translate: "aria_ah.ui.interface.dialog.disable_jump_scare" }]})
                    .button1({ rawtext: [{ translate: "aria_ah.ui.interface.general.cancel" }]})
                    .button2({ rawtext: [{ translate: "aria_ah.ui.interface.general.confirm" }]})
                    .show(player).then((res) => {
                        if (res.canceled) return
                        
                        if (res.selection == 1) {
                            optionsScoreboard.setScore("jump_scare", 0)
                        }

                        openJumpScareSettings(player)
                    })
            } else if (res.selection == 2) {
                openSettingsMenu(player, false)
            }
        })
}

function openSoundsSettings(player) {
    const optionsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.options")
    const nightfallSound = optionsScoreboard.getScore("nightfall_sound")
    const daytimeSound = optionsScoreboard.getScore("daytime_sound")
    const fogSound = optionsScoreboard.getScore("fog_sound")

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.sounds_settings" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.sounds.nightfall" }]}, nightfallSound == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.sounds.daytime" }]}, daytimeSound == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.sounds.fog" }]}, fogSound == 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return

            if (res.selection == 0) {
                optionsScoreboard.setScore("nightfall_sound", 1 - nightfallSound)
            } else if (res.selection == 1) {
                optionsScoreboard.setScore("daytime_sound", 1 - daytimeSound)
            } else if (res.selection == 2) {
                optionsScoreboard.setScore("fog_sound", 1 - fogSound)
            } else if (res.selection == 3) {
                openSettingsMenu(player, false)
                return
            }

            openSoundsSettings(player)
        })
}

function openBuffsSettings(player) {
    const options = Minecraft.world.scoreboard.getObjective("aria_ah.options");
    const redBuff = options.getScore("red_mob_buff");
    const invisibleBuff = options.getScore("invisible_mob_buff");

    new MinecraftUi.ModalFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.buffs" }]})
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.red_buff" }]}, { defaultValue: redBuff === 1 })
        .toggle({ rawtext: [{ translate: "aria_ah.ui.interface.toggle.invisible_buff" }]}, { defaultValue: invisibleBuff === 1 })
        .submitButton({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.save" }]})
        .show(player).then((res) => {
            if (res.canceled) return;

            options.setScore("red_mob_buff", res.formValues[0] ? 1 : 0);
            options.setScore("invisible_mob_buff", res.formValues[1] ? 1 : 0);

            openSettingsMenu(player);
        });
}

function openStatisticSettings(player) {
    const statsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.stats")
    const easyStatsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.stats_easy")
    const normalStatsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.stats_normal")
    const hardStatsScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.stats_hard")

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.statistics" }]})
        .body({
            rawtext: [
                {
                    translate: "aria_ah.ui.interface.description.statistics.dark_fog_transformations",
                    with: [statsScoreboard.getScore("transform").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.nightfall_counter",
                    with: [statsScoreboard.getScore("nightfall_count").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.dark_fog_spotted",
                    with: [statsScoreboard.getScore("dark_fog_spotted").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.creatures_transformed_back",
                    with: [statsScoreboard.getScore("transform_back").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.dark_energy_collected",
                    with: [statsScoreboard.getScore("energy_collect").toString()]
                },

                {
                    translate: "aria_ah.ui.interface.description.statistics.killed_creatures"
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.spooky",
                    with: [easyStatsScoreboard.getScore("killed_creatures").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.horror",
                    with: [normalStatsScoreboard.getScore("killed_creatures").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.nightmare",
                    with: [hardStatsScoreboard.getScore("killed_creatures").toString()]
                },

                {
                    translate: "aria_ah.ui.interface.description.statistics.killed_absorber"
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.spooky",
                    with: [easyStatsScoreboard.getScore("killed_absorber").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.horror",
                    with: [normalStatsScoreboard.getScore("killed_absorber").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.nightmare",
                    with: [hardStatsScoreboard.getScore("killed_absorber").toString()]
                },

                {
                    translate: "aria_ah.ui.interface.description.statistics.killed_shadow_boss"
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.spooky",
                    with: [easyStatsScoreboard.getScore("killed_shadow_boss").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.horror",
                    with: [normalStatsScoreboard.getScore("killed_shadow_boss").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.nightmare",
                    with: [hardStatsScoreboard.getScore("killed_shadow_boss").toString()]
                },

                {
                    translate: "aria_ah.ui.interface.description.statistics.killed_red_buff"
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.spooky",
                    with: [easyStatsScoreboard.getScore("killed_red_buff").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.horror",
                    with: [normalStatsScoreboard.getScore("killed_red_buff").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.nightmare",
                    with: [hardStatsScoreboard.getScore("killed_red_buff").toString()]
                },

                {
                    translate: "aria_ah.ui.interface.description.statistics.killed_invisible_buff"
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.spooky",
                    with: [easyStatsScoreboard.getScore("killed_invisible_buff").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.horror",
                    with: [normalStatsScoreboard.getScore("killed_invisible_buff").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.nightmare",
                    with: [hardStatsScoreboard.getScore("killed_invisible_buff").toString()]
                },

                {
                    translate: "aria_ah.ui.interface.description.statistics.killed_creature"
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.spooky",
                    with: [easyStatsScoreboard.getScore("killed_creature").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.horror",
                    with: [normalStatsScoreboard.getScore("killed_creature").toString()]
                },
                {
                    translate: "aria_ah.ui.interface.description.statistics.difficulty.nightmare",
                    with: [hardStatsScoreboard.getScore("killed_creature").toString()]
                }
            ]
        })
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return

            if (res.selection == 0) {
                openMainMenu(player, false)
            }
        })
}

// ========================
// === NIGHT WEIGHT SETTINGS ===
// ========================
function openNightWeightSettings(player) {
    const nightSetting = Minecraft.world.scoreboard.getObjective("aria_ah.night");
    
    new MinecraftUi.ModalFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.night_weight" }]})
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.night_weight.blood" }]}, 0, 10, { defaultValue: nightSetting.getScore("red") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.night_weight.cyan" }]}, 0, 10, { defaultValue: nightSetting.getScore("cyan") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.night_weight.blackhole" }]}, 0, 10, { defaultValue: nightSetting.getScore("hole") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.night_weight.pink" }]}, 0, 10, { defaultValue: nightSetting.getScore("pink") })
        .slider({ rawtext: [{ translate: "aria_ah.ui.interface.slider.night_weight.invisible" }]}, 0, 10, { defaultValue: nightSetting.getScore("black") })
        .show(player).then((res) => {
            if (res.canceled) return;
            
            nightSetting.setScore("red", res.formValues[0]);
            nightSetting.setScore("cyan", res.formValues[1]);
            nightSetting.setScore("hole", res.formValues[2]);
            nightSetting.setScore("pink", res.formValues[3]);
            nightSetting.setScore("black", res.formValues[4]);
            
            // Optional: Reopen settings menu after adjustment
            openSettingsMenu(player);
        });
}

function openGeneralSettings(player) {
    const generalScoreboard = Minecraft.world.scoreboard.getObjective("aria_ah.general");
    const daylightSetting = generalScoreboard.getScore("no_daylight");
    const transformBackSetting = generalScoreboard.getScore("transform_back");
    const disableSpawningSetting = generalScoreboard.getScore("disable_spawning");
    const fixLightSetting = generalScoreboard.getScore("fix_light");
    const cameraFadeSetting = generalScoreboard.getScore("camera_fade");
    const vvForceSetting = generalScoreboard.getScore("vv_force");
    const displayTitle = generalScoreboard.getScore("display_title");
    const protectTamed = generalScoreboard.getScore("protect_tamed");

    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.general_settings" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.always_night" }]}, daylightSetting === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.transform_back" }]}, transformBackSetting === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.fix_lights" }]}, fixLightSetting === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.camera_fade" }]}, cameraFadeSetting === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.vibrant_visual" }]}, vvForceSetting === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.fog_spawning" }]}, disableSpawningSetting === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.display_title" }]}, displayTitle === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general_settings.protect_tamed" }]}, protectTamed === 1 ? "textures/aria/ah/ui/on" : "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return;

            if (res.selection === 0) {
                // Toggle Always Night
                if (daylightSetting === 0) {
                    Minecraft.world.gameRules.doDayLightCycle = false;
                    Minecraft.world.setTimeOfDay(15500);
                } else if (daylightSetting === 1) {
                    Minecraft.world.gameRules.doDayLightCycle = true;
                }
                generalScoreboard.setScore("no_daylight", 1 - daylightSetting);
                openGeneralSettings(player);
            } else if (res.selection == 1) {
                generalScoreboard.setScore("transform_back", 1 - transformBackSetting);
                openGeneralSettings(player);
            } else if (res.selection == 2) {
                generalScoreboard.setScore("fix_light", 1 - fixLightSetting);
                openGeneralSettings(player);
            } else if (res.selection == 3) {
                generalScoreboard.setScore("camera_fade", 1 - cameraFadeSetting);
                openGeneralSettings(player);
            } else if (res.selection == 4) {
                generalScoreboard.setScore("vv_force", 1 - vvForceSetting);
                generalScoreboard.setScore("vv_active", 0);
                openGeneralSettings(player);
            } else if (res.selection == 5) {
                generalScoreboard.setScore("disable_spawning", 1 - disableSpawningSetting);
                openGeneralSettings(player);
            } else if (res.selection == 6) {
                generalScoreboard.setScore("display_title", 1 - displayTitle);
                openGeneralSettings(player);
            } else if (res.selection === 7) {
                generalScoreboard.setScore("protect_tamed", 1 - protectTamed);
                openGeneralSettings(player);
            } else if (res.selection == 8) {
                openSettingsMenu(player, false);
                return;
            }
        });
}



function openCheatsSettings(player) {
    new MinecraftUi.ActionFormData()
        .title({ rawtext: [{ translate: "aria_ah.ui.interface.title.cheats" }]})
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.cheats.unlock_morphs" }]}, "textures/aria/ah/ui/reset")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.cheats.reset_addon" }]}, "textures/aria/ah/ui/off")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.cheats.reset_stats" }]}, "textures/aria/ah/ui/stats")
        .button({ rawtext: [{ translate: "aria_ah.ui.interface.button.general.back" }]}, "textures/aria/ah/ui/back")
        .show(player).then((res) => {
            if (res.canceled) return
            if (player.getGameMode() != Minecraft.GameMode.Creative) {
                player.sendMessage({ rawtext: [{ translate: "aria_ah.ui.interface.message.cheats_creative_only" }]});
                return
            }

            if (res.selection == 0) {
                player.runCommand("function aria/ah/morph/unlock_all")
                player.sendMessage({ rawtext: [{ translate: "aria_ah.ui.interface.message.morphs_unlocked" }]});
            } else if (res.selection == 1) {
                new MinecraftUi.MessageFormData()
                    .body({ rawtext: [{ translate: "aria_ah.ui.interface.dialog.reset_addon" }]})
                    .button1({ rawtext: [{ translate: "aria_ah.ui.interface.general.cancel" }]})
                    .button2({ rawtext: [{ translate: "aria_ah.ui.interface.general.confirm" }]})
                    .show(player).then((res) => {
                        if (res.canceled) return
                        
                        if (res.selection == 1) {
                            player.runCommand("function aria/ah/setup_reset")
                            player.sendMessage({ rawtext: [{ translate: "aria_ah.ui.interface.message.addon_reset" }]});
                        }
                    })
            } else if (res.selection == 2) {
                player.runCommand("function aria/ah/statistic_reset")
                player.sendMessage({ rawtext: [{ translate: "aria_ah.ui.interface.message.statistics_reset" }]});
            } else if (res.selection == 3) {
                openSettingsMenu(player, false)
            }
        })
}
