import { world, system, LocationOutOfWorldBoundariesError } from "@minecraft/server";
import { Vector } from "../util/vector";
import { FXSequencer } from "../util/fx";
import { settingEnabled } from "../settings";

// Variables
const waterCascadesDelayTicks = 100;

// Register the FX sequence for the particle effect
const waterCascadesFX = FXSequencer.register("squaredreams_realism:cascades");
waterCascadesFX.addParticle(0, "squaredreams_realism:cascades");

// Main loop to check all players at regular intervals
system.runInterval(() => {
    // Grab all players
    const allPlayers = world.getAllPlayers();
    // If no players, return
    if (allPlayers.length === 0) return;

    // Check if setting is enabled
    const waterCascadesSetting = settingEnabled("water_cascades");

    // Determine blockCheckRadius based on the setting value
    const blockCheckRadius =
        waterCascadesSetting === 1 ? 8 :
        waterCascadesSetting === 2 ? 16 :
        waterCascadesSetting === 3 ? 32 : null;

    if (!blockCheckRadius) return;

    // Check every player
    allPlayers.forEach(player => {
        const { dimension, location } = player;
        // Iterate through the region to find the matching block conditions
        for (let x = location.x - blockCheckRadius; x <= location.x + blockCheckRadius; x++) {
            for (let y = location.y - (blockCheckRadius / 2); y <= location.y + (blockCheckRadius / 2); y++) {
                for (let z = location.z - blockCheckRadius; z <= location.z + blockCheckRadius; z++) {
                    let currentBlockLocation = new Vector(Math.round(x), Math.round(y), Math.round(z));
                    let blockBelowLocation = new Vector(Math.round(x), Math.round(y - 1), Math.round(z));

                    try {
                        let currentBlock = dimension.getBlock(currentBlockLocation);
                        let blockBelow = dimension.getBlock(blockBelowLocation);

                        if (currentBlock && blockBelow) {
                            if (currentBlock.typeId === "minecraft:water" && blockBelow.typeId === "minecraft:water") {
                                const currentBlockDepth = currentBlock.permutation.getState("liquid_depth");
                                const blockBelowDepth = blockBelow.permutation.getState("liquid_depth");

                                // Check if the current block is a non-source water block and the block below is a source block
                                if (currentBlockDepth > 0 && blockBelowDepth === 0) {
                                    // Trigger particle effect
                                    FXSequencer.play("squaredreams_realism:cascades", dimension, currentBlockLocation);
                                    break;
                                }
                            }
                        }
                    } catch (exception) {
                        if (exception instanceof LocationOutOfWorldBoundariesError) {
                            continue;
                        } else {
                            console.error(exception);
                        }
                    }
                }
            }
        }
    });
}, waterCascadesDelayTicks);