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


let caveFogIntervalId = null;

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

    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.runCommandAsync(`fog @s push "dungeon:cave_fog" dcave_fog`);
                        player.runCommandAsync(`playsound ambient.cave @s ~ ~ ~ 1 0.8`).catch(() => { });
                        player.runCommandAsync(`playsound ambient.basalt_deltas.loop @s ~ ~ ~ 0.01 0.7`).catch(() => { });
                        player.runCommandAsync(`effect @s slowness 6 1 true`).catch(() => { });

                        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.runCommandAsync(`playsound ambient.cave @s ~ ~ ~ ${volume} ${pitch}`).catch(() => { });
                        }
                    }
                } else if (hasFog) {
                    player.removeTag("cave_fog_active");
                    player.runCommandAsync("fog @s remove dcave_fog").catch(() => { });
                }

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

            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.runCommandAsync("fog @s remove dcave_fog").catch(() => { });
                player.runCommandAsync(`effect @s clear`).catch(() => { });
            }
        }
        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();
        }
}
