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

export function initIceOcean() {

    const forebodingCheck = fo.scoreChapter5; 
    const interval = 120; 

    
    const ICE_EVENT_RAN_PROPERTY = "event_ran"; 

    if (!world.getDynamicProperty(ICE_EVENT_RAN_PROPERTY)) {
        world.setDynamicProperty(ICE_EVENT_RAN_PROPERTY, false); 
    }

    if (world.getDynamicProperty(ICE_EVENT_RAN_PROPERTY) === true && fo.forebodingScore >= 1 && fo.forebodingScore < fo.scoreChapter5) { 
        world.setDynamicProperty(ICE_EVENT_RAN_PROPERTY, false);
        
    }


    
    function isPlayerOnIce(player) {
        if (player.location.y < 55 || player.location.y > 70) return;
        const feetPosition = { x: player.location.x, y: Math.floor(player.location.y) - 1, z: player.location.z };
        const block = fo.overworld.getBlock(feetPosition);
        if (!block) {
            
            return false;
        }
        return block.typeId === "minecraft:ice" || block.typeId === "minecraft:packed_ice" || block.typeId === "minecraft:blue_ice";
    }

    
    function isInFrozenOcean(player) {
        const directions = [
            { x: 1, z: 0 },  
            { x: -1, z: 0 }, 
            { x: 0, z: 1 },  
            { x: 0, z: -1 }  
        ];

        for (let dir of directions) {
            for (let i = 1; i <= 10; i++) {
                const checkPosition = {
                    x: Math.floor(player.location.x + dir.x * i),
                    y: Math.floor(player.location.y) - 1,
                    z: Math.floor(player.location.z + dir.z * i)
                };
                const block = fo.overworld.getBlock(checkPosition);
                if (!block) {
                    
                    break;
                }
                if (block.typeId !== "minecraft:ice" && block.typeId !== "minecraft:packed_ice" && block.typeId !== "minecraft:blue_ice") {
                    break;
                }
                if (i === 10) {
                    return true;
                }
            }
        }
        return false;
    }

    
    function breakIceAndPlaySounds(player) {
        const positions = [10, 8, 6, 4];
        positions.forEach((distance, index) => {
            system.runTimeout(() => {
                const breakPosition = {
                    x: Math.floor(player.location.x + distance),
                    y: Math.floor(player.location.y) - 1,
                    z: Math.floor(player.location.z)
                };
                const block = fo.overworld.getBlock(breakPosition);
                if (block.typeId === "minecraft:ice" || block.typeId === "minecraft:packed_ice" || block.typeId === "minecraft:blue_ice") {
                    block.setPermutation(BlockPermutation.resolve("minecraft:water"));
                    fo.overworld.playSound("random.glass", breakPosition);
                    
                } else {
                    
                }
            }, index * 40); 
        });

        
        system.runTimeout(() => {
            const breakPosition = {
                x: Math.floor(player.location.x + 10),
                y: Math.floor(player.location.y) - 1,
                z: Math.floor(player.location.z)
            };
            fo.overworld.playSound("random.splash", breakPosition);
            fo.overworld.playSound("ambient.cave", breakPosition);
            
        }, 25); 

        
        system.runTimeout(() => {
            const breakPosition = {
                x: Math.floor(player.location.x + 10),
                y: Math.floor(player.location.y) - 2,
                z: Math.floor(player.location.z)
            };
            fo.overworld.playSound("random.swim", breakPosition);
            
        }, 50);

        
        for (let i = 0; i < 6; i++) {
            system.runTimeout(() => {
                const randomDirection = {
                    x: Math.floor(Math.random() * 9) - 4, 
                    z: Math.floor(Math.random() * 9) - 4
                };
                const breakPosition = {
                    x: Math.floor(player.location.x + randomDirection.x),
                    y: Math.floor(player.location.y) - 1,
                    z: Math.floor(player.location.z + randomDirection.z)
                };
                const block = fo.overworld.getBlock(breakPosition);
                if (block.typeId === "minecraft:ice" || block.typeId === "minecraft:packed_ice" || block.typeId === "minecraft:blue_ice") {
                    block.setPermutation(BlockPermutation.resolve("minecraft:water"));
                    fo.overworld.playSound("random.glass", breakPosition);
                    
                } else {
                    
                }
            }, 160 + i * 30); 
        }

        
        for (let i = 0; i < 9; i++) {
            system.runTimeout(() => {
                const randomDirection = {
                    x: Math.floor(Math.random() * 9) - 4, 
                    z: Math.floor(Math.random() * 9) - 4
                };
                const breakPosition = {
                    x: Math.floor(player.location.x + randomDirection.x),
                    y: Math.floor(player.location.y) - 3,
                    z: Math.floor(player.location.z + randomDirection.z)
                };
                const block = fo.overworld.getBlock(breakPosition);
                if (block.isLiquid) {
                    fo.overworld.playSound("random.swim", breakPosition);
                    
                } else {
                    
                }
            }, 173 + i * 17); 
        }

        
        world.setDynamicProperty(ICE_EVENT_RAN_PROPERTY, true);
    }

    
    system.runInterval(() => {
        const players = world.getPlayers();
        if (world.getDynamicProperty(ICE_EVENT_RAN_PROPERTY) === true) {
            
            return;
        }
        if (fo.forebodingScore >= forebodingCheck) {
            for (const player of players) {
                if (isPlayerOnIce(player) && isInFrozenOcean(player)) {
                    breakIceAndPlaySounds(player);
                }
            }
        }
    }, interval); 

}