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

export function initAllDoors() {

    
    const chance = 1 / 10; 
    const forebodingCheck = fo.scoreChapter5; 
    const interval = 2880; 
    const CHECKINTERVAL = 1; 
    const BLOCKS_PER_TICK = 100; 

    let eventflag = false; 

    
    const woodenDoors = fo.doorIds;

    
    const copperDoor = fo.copperDoorIds;


    
    let cubeState = {
        player: null, 
        blockPositions: [], 
        remainingBlocks: 0, 
        processing: false 
    };

    
    function initializeCubeState() {
        const players = world.getPlayers();
        if (players.length === 0) {
            cubeState.processing = false;
            return;
        }

        
        const randomPlayer = players[Math.floor(Math.random() * players.length)];
        const playerLocation = randomPlayer.location;

        
        const minX = Math.floor(playerLocation.x - 22);
        const maxX = Math.floor(playerLocation.x + 22);
        const minY = Math.min(Math.max(Math.floor(playerLocation.y - 12), -63), 319); 
        const maxY = Math.min(Math.max(Math.floor(playerLocation.y + 12), -63), 319); 
        const minZ = Math.floor(playerLocation.z - 22);
        const maxZ = Math.floor(playerLocation.z + 22);

        
        cubeState.blockPositions = [];
        for (let x = minX; x <= maxX; x++) {
            for (let y = minY; y <= maxY; y++) {
                for (let z = minZ; z <= maxZ; z++) {
                    cubeState.blockPositions.push({ x, y, z });
                }
            }
        }

        
        cubeState.player = randomPlayer;
        cubeState.remainingBlocks = cubeState.blockPositions.length;
        cubeState.processing = true;
    }

    
    function processCubeBlocks() {
        if (!cubeState.processing) return;

        const dimension = cubeState.player.dimension;
        let blocksProcessed = 0;

        
        while (cubeState.remainingBlocks > 0 && blocksProcessed < BLOCKS_PER_TICK) {
            
            const randomIndex = Math.floor(Math.random() * cubeState.remainingBlocks);
            const { x, y, z } = cubeState.blockPositions[randomIndex];

            
            [cubeState.blockPositions[randomIndex], cubeState.blockPositions[cubeState.remainingBlocks - 1]] =
                [cubeState.blockPositions[cubeState.remainingBlocks - 1], cubeState.blockPositions[randomIndex]];

            
            const block = dimension.getBlock({ x, y, z });

            
            if (block && woodenDoors.includes(block.typeId)) {
                const isUpperBlock = block.permutation.getState("upper_block_bit");
                const isClosed = !block.permutation.getState("open_bit");

                
                if (!isUpperBlock && isClosed) {
                    const doorPermutation = block.permutation.withState("open_bit", true);
                    block.setPermutation(doorPermutation);

                    
                    if (copperDoor.includes(block.typeId)) {
                        dimension.playSound("open_door.copper", block.location, { volume: 2 });
                    } else {
                        dimension.playSound("random.door_open", block.location, { volume: 2 });
                    }
                }
            }

            cubeState.remainingBlocks--;
            blocksProcessed++;
        }

        
        if (cubeState.remainingBlocks <= 0) {
            cubeState.processing = false;
            eventflag = false; 
            
        }
    }

    
    system.runInterval(() => {

        if (!eventflag && fo.forebodingScore >= forebodingCheck && Math.random() <= chance) {
            eventflag = true;
            initializeCubeState(); 
            
        }
    }, interval);

    
    system.runInterval(() => {
        if (eventflag && cubeState.processing) {
            processCubeBlocks();
        }
    }, CHECKINTERVAL);

}