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


export function initCraftingTable() {

    
    const CHECK_INTERVAL = 6000; 
    const EVENT_CHANCE = 1 / 24; 
    const forebodingCheck = fo.scoreChapter4; 
    const PLACEMENT_RADIUS = 120; 
    const MIN_PLAYER_DISTANCE = 64; 
    const LOCATION_CHECKS = 40; 
    const DEPTH_CHECKS = 30; 


    
    function tryTriggerEvent() {
        
        if (fo.forebodingScore < forebodingCheck && Math.random() < (EVENT_CHANCE * fo.netProbabilityModifier)) {
            
            placeCraftingTableNearRandomPlayer();
        } else {
            
        }
    }

    
    function placeCraftingTableNearRandomPlayer() {
        const players = fo.overworld.getPlayers();
        if (players.length === 0) {
            
            return; 
        }

        const player = players[Math.floor(Math.random() * players.length)];
        let playerPos = player.location;
        

        let locationsChecked = 0;

        while (locationsChecked < LOCATION_CHECKS) {
            
            let tablePos = {
                x: playerPos.x + Math.floor(Math.random() * PLACEMENT_RADIUS * 2) - PLACEMENT_RADIUS,
                z: playerPos.z + Math.floor(Math.random() * PLACEMENT_RADIUS * 2) - PLACEMENT_RADIUS
            };
            

            
            let isValidPosition = true;
            for (let p of players) {
                let dx = p.location.x - tablePos.x;
                let dz = p.location.z - tablePos.z;
                let distance = Math.sqrt(dx * dx + dz * dz);

                if (distance < MIN_PLAYER_DISTANCE) {
                    isValidPosition = false;
                    break;
                }
            }

            if (!isValidPosition) {
                
                continue; 
            }

            
            locationsChecked++;
            

            
            let dimension = fo.overworld;
            let foundValidSpot = false;

            for (let y = 60; y >= 60 - DEPTH_CHECKS; y--) { 
                let block = dimension.getBlock({ x: tablePos.x, y: y, z: tablePos.z });
                let blockAbove = dimension.getBlock({ x: tablePos.x, y: y + 1, z: tablePos.z });

                
                if (block && blockAbove) {
                    
                    if (!block.isAir && !block.isLiquid && blockAbove.isAir) {
                        tablePos.y = y + 1; 
                        foundValidSpot = true;
                        
                        break;
                    }
                } else {
                    
                }
            }

            if (foundValidSpot) {
                
                let block = dimension.getBlock(tablePos);
                if (block && block.isAir) {
                    block.setPermutation(BlockPermutation.resolve("minecraft:crafting_table"));
                    
                    return; 
                } else {
                    
                }
            } else {
                
            }
        }

        
    }

    
    system.runInterval(tryTriggerEvent, CHECK_INTERVAL);

}