import { system, world } from "@minecraft/server";


let caveFogIntervalId = null;

export function cave_event() {
    const FOG_MIN_Y = -64;
    const FOG_MAX_Y = 5;

    const EVENT_START = 1000;
    const EVENT_END = 1500;
    const EVENT_RESET = 1500;

    function startFogLoop() {
        if (caveFogIntervalId !== null) return;

        caveFogIntervalId = system.runInterval(() => {
            const players = world.getPlayers();
            if (players.length === 0) return;

            const objective = world.scoreboard.getObjective("cave_fog_timer");
            const score = objective?.getScore("cave_event") ?? 0;

            let playersInCave = 0;

            for (const player of players) {
                const y = Math.floor(player.location.y);
                const inCave = y >= FOG_MIN_Y && y <= FOG_MAX_Y;
                const hasFog = player.hasTag("cave_fog_active");

                if (inCave) {
                    playersInCave++;

                    if (!hasFog && score >= EVENT_START && score <= EVENT_END) {
                        player.addTag("cave_fog_active");
                        player.runCommand(`fog @s push "dungeon:cave_fog" dcave_fog`);
                        player.runCommand(`playsound ambient.cave @s ~ ~ ~ 1 0.8`);
                        player.runCommand(`playsound ambient.basalt_deltas.loop @s ~ ~ ~ 0.01 0.7`);
                        player.runCommand(`effect @s slowness 6 1 true`);

                        if (Math.random() < 0.5) {
                            const volume = (Math.random() * 0.4 + 0.2).toFixed(2);
                            const pitch = (Math.random() * 0.4 + 0.6).toFixed(2);
                            player.runCommand(`playsound ambient.cave @s ~ ~ ~ ${volume} ${pitch}`);
                        }
                    }
                } else if (hasFog) {
                    player.removeTag("cave_fog_active");
                    player.runCommand("fog @s remove dcave_fog");
                }

                // Evento RESET
                if (hasFog && score >= EVENT_RESET) {
                    player.removeTag("cave_fog_active");
                    player.runCommand("fog @s remove dcave_fog");
                }
            }

            if (playersInCave === 0) {
                stopFogLoop(players);
            }
        }, 19);
    }
/**
 * @param {import('@minecraft/server').Entity[]} players
 */
    function stopFogLoop(players) {
        for (const player of players) {
            if (player.hasTag("cave_fog_active")) {
                player.removeTag("cave_fog_active");
                player.runCommand("fog @s remove dcave_fog");
                player.runCommand(`effect @s clear`);
            }
        }
        caveFogIntervalId = null;
    }

    // Activador
        if (caveFogIntervalId !== null) return;

        const players = world.getPlayers();
        if (players.some(p => {
            const y = Math.floor(p.location.y);
            return y >= FOG_MIN_Y && y <= FOG_MAX_Y;
        })) {
            startFogLoop();
        }
}
