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



export function initMineRestore() {

    
    const RESTORATION_THRESHOLD = fo.scoreChapter3 + (fo.durationChapter3 * 0.95);
    const Y_STONE_MIN = -5;
    const Y_STONE_MAX = 30;
    const Y_DEEPSLATE_MAX = -6;
    const RADIUS_MIN = 16;
    const RADIUS_MAX = 160;
    const BLOCKS_PER_TICK = 40; 

    
    const MINED_BLOCKS_KEY = "minedBlocks";

    
    function isPlayerBelowY50(player) {
        return player.location.y < 50;
    }

    
    function isAnyPlayerBelowY50() {
        const players = world.getPlayers();
        return players.some(player => isPlayerBelowY50(player));
    }

    
    function restoreBlocks(player) {
        const minedBlocks = JSON.parse(world.getDynamicProperty(MINED_BLOCKS_KEY) || "[]");
        const updatedMinedBlocks = [];

        if (minedBlocks.length === 0) {
            
            return;
        }

        
        

        
        const blocksToProcess = minedBlocks.splice(0, BLOCKS_PER_TICK);

        for (const blockLocation of blocksToProcess) {
            const distance = Math.sqrt(
                Math.pow(player.location.x - blockLocation.x, 2) +
                
                Math.pow(player.location.z - blockLocation.z, 2)
            );

            
            

            if (distance >= RADIUS_MIN && distance <= RADIUS_MAX) {
                
                const blockPos = { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z };
                const block = fo.overworld.getBlock(blockPos);

                if (block) {
                    

                    if (block.typeId === "minecraft:air") {
                        const newBlockId = blockLocation.y < Y_DEEPSLATE_MAX ? "minecraft:deepslate" :
                            blockLocation.y >= Y_STONE_MIN && blockLocation.y <= Y_STONE_MAX ? "minecraft:stone" : null;

                        if (newBlockId) {
                            
                            block.setType(newBlockId);
                            
                            
                            continue;
                        } else {
                            
                        }
                    } else {
                        
                        
                        continue;
                    }
                } else {
                    
                }
            } else {
                
            }

            
            updatedMinedBlocks.push(blockLocation);
        }

        
        const remainingBlocks = updatedMinedBlocks.concat(minedBlocks);

        
        world.setDynamicProperty(MINED_BLOCKS_KEY, JSON.stringify(remainingBlocks));
        
    }

    
    world.afterEvents.playerSpawn.subscribe((eventData) => {
        let { player, initialSpawn } = eventData;

        const forebodingScore = fo.forebodingScore;
        if (initialSpawn && forebodingScore >= RESTORATION_THRESHOLD) {
            

            if (!isAnyPlayerBelowY50()) {
                
                
                system.runInterval(() => {
                    if (!isAnyPlayerBelowY50()) {
                        restoreBlocks(player);
                    } else {
                        
                    }
                }, 20); 
            } else {
                
            }
        }
    });

}