import { world, system, DisplaySlotId } from "@minecraft/server";
import { fo } from './utilities/fo_constants.js';








export function initStartConditionals() {

    const
        forebodingObjective = fo.forebodingObjective,
        markObjective = world.scoreboard.getObjective("Random"),
        markScore = 88, 
        yCheck = -50, 
        areaRadius = 8000, 
        levelCheck = 45, 
        daysStartChecking = 30, 
        chancePerDay = 1 / 100, 

        overworld = fo.overworld;

    
    function markPlayer(player) {
        try {
            world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.BelowName, { objective: markObjective });
            markObjective.setScore(player, markScore);
            
            return true;
        } catch (error) {
            console.log(`[DEBUG] Error marking player: ${error}`);
            return false;
        }
    };

    function harbingerEffect(player) {
        if (player.dimension.id !== "minecraft:overworld") return;
        if (Math.random() < 0.8) fo.overworld.setWeather("Rain", 36000);
        else fo.overworld.setWeather("Thunder", 36000);
    };

    system.runInterval(() => {
        if (fo.forebodingScore !== 0) return;

        const players = overworld.getPlayers();
        if (players.length === 0) return;

        
        const deepPlayers = players.filter(player => player.location.y <= yCheck);
        if (deepPlayers.length > 0) {
            forebodingObjective.addScore("Counter ", 1);
            markPlayer(deepPlayers[0]);
            harbingerEffect(deepPlayers[0]);
            return;
        }

        
        const farPlayers = players.filter(player =>
            Math.abs(player.location.x) >= areaRadius ||
            Math.abs(player.location.z) >= areaRadius
        );
        if (farPlayers.length > 0) {
            forebodingObjective.addScore("Counter ", 1);
            markPlayer(farPlayers[0]);
            harbingerEffect(farPlayers[0]);
            return;
        }

        
        const highLevelPlayers = players.filter(player => player.level >= levelCheck);
        if (highLevelPlayers.length > 0) {
            forebodingObjective.addScore("Counter ", 1);
            markPlayer(highLevelPlayers[0]);
            harbingerEffect(highLevelPlayers[0]);
            return;
        }
    }, 19);

    
    let lastCheckedDay = 0;
    system.runInterval(() => {
        const forebodingScore = forebodingObjective.getScore("Counter ");
        if (forebodingScore >= 1) return;

        const currentDay = world.getDay();
        if (currentDay >= daysStartChecking && currentDay !== lastCheckedDay) {
            lastCheckedDay = currentDay;

            if (Math.random() <= chancePerDay) {
                startForeboding();
                const players = overworld.getPlayers();
                if (players.length > 0) {
                    const randomPlayer = players[Math.floor(Math.random() * players.length)];
                    markPlayer(randomPlayer);
                }
            }
        }
    }, 12000);

    function startForeboding() {
        const forebodingScore = fo.forebodingScore;
        if (forebodingScore === 0 || forebodingScore === -1) {
            forebodingObjective.addScore("Counter ", 1);
        }
    }

}