import { system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
const FROST_RADIUS = 15;
const ICE_RING_STEP = 4;
const SNOW_PATCHES = 15;
const PATCHES_PER_TICK = 1;
function spawnFrostTrail(from, to) {
    const dx = to.x - from.x;
    const dy = to.y - from.y;
    const dz = to.z - from.z;
    const distance = Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2);
    const steps = Math.ceil(distance / 0.25);
    const dim = world.getDimension("overworld");
    for (let i = 0; i <= steps; i++) {
        const t = i / steps;
        const px = from.x + dx * t + 0.5;
        const py = from.y + dy * t + 0.5 + Math.sin(Math.PI * t);
        const pz = from.z + dz * t + 0.5;
        dim.spawnParticle("wildphire_spg:blue_white_trail_particle", { x: px, y: py, z: pz });
    }
}
function getTopWaterBlock(dim, x, y, z) {
    for (let dy = 12; dy >= -12; dy--) {
        const checkY = y + dy;
        const base = dim.getBlock({ x, y: checkY, z });
        const above = dim.getBlock({ x, y: checkY + 1, z });
        if (base?.typeId === "minecraft:water" && above?.typeId === "minecraft:air") {
            return { x, y: checkY, z };
        }
    }
    return null;
}
function getAvailableSnowTargets(dim, origin) {
    const radiusSq = FROST_RADIUS * FROST_RADIUS;
    const spots = [];
    for (let dx = -FROST_RADIUS; dx <= FROST_RADIUS; dx++) {
        for (let dz = -FROST_RADIUS; dz <= FROST_RADIUS; dz++) {
            if (dx * dx + dz * dz > radiusSq)
                continue;
            const x = origin.x + dx;
            const z = origin.z + dz;
            for (let dy = 10; dy >= -10; dy--) {
                const y = origin.y + dy;
                const base = dim.getBlock({ x, y, z });
                const above = dim.getBlock({ x, y: y + 1, z });
                if (base?.typeId === "minecraft:ice" && above?.typeId === "minecraft:air") {
                    spots.push({ x, y: y + 1, z });
                    break;
                }
            }
        }
    }
    for (let i = spots.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [spots[i], spots[j]] = [spots[j], spots[i]];
    }
    return spots.slice(0, SNOW_PATCHES);
}
function placePackedIceCluster(center, dim) {
    const size = Math.floor(Math.random() * 4) + 3; // 3x3 to 6x6
    const radius = Math.floor(size / 2);
    for (let dx = -radius; dx <= radius; dx++) {
        for (let dz = -radius; dz <= radius; dz++) {
            const distSq = dx * dx + dz * dz;
            if (distSq > radius * radius || Math.random() < 0.3)
                continue;
            const x = center.x + dx;
            const z = center.z + dz;
            const y = center.y - 1;
            const above = dim.getBlock({ x, y: y + 1, z });
            if (above?.typeId === "minecraft:air") {
                dim.runCommand(`setblock ${x} ${y} ${z} minecraft:packed_ice`);
            }
        }
    }
}
export function frostSpongePulse(block) {
    // Check if frost sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("frostSponge")) {
        const dim = block.dimension;
        dim.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        // Notify nearby players
        const nearbyPlayers = dim.getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cFrost Sponge is disabled in settings!");
        }
        return;
    }
    const dim = block.dimension;
    const origin = block.location;
    let rippleStep = 0;
    const frostRipple = () => {
        const r = rippleStep * ICE_RING_STEP;
        if (r > FROST_RADIUS) {
            system.runTimeout(() => startSnowPhase(), 5);
            return;
        }
        for (let dx = -r; dx <= r; dx++) {
            const dzLimit = Math.floor(Math.sqrt(r * r - dx * dx));
            for (let dz = -dzLimit; dz <= dzLimit; dz++) {
                const x = origin.x + dx;
                const z = origin.z + dz;
                const pos = getTopWaterBlock(dim, x, origin.y, z);
                if (pos) {
                    dim.runCommand(`setblock ${pos.x} ${pos.y} ${pos.z} minecraft:ice`);
                }
            }
        }
        rippleStep++;
        system.runTimeout(frostRipple, 1);
    };
    const startSnowPhase = () => {
        const snowSpots = getAvailableSnowTargets(dim, origin);
        let i = 0;
        const placePatch = () => {
            if (i >= snowSpots.length) {
                dim.runCommand(`setblock ${origin.x} ${origin.y} ${origin.z} air destroy`);
                return;
            }
            for (let p = 0; p < PATCHES_PER_TICK && i < snowSpots.length; p++, i++) {
                const center = snowSpots[i];
                spawnFrostTrail(origin, center);
                system.runTimeout(() => {
                    const size = Math.floor(Math.random() * 3) + 3;
                    const radius = Math.floor(size / 2);
                    for (let dx = -radius; dx <= radius; dx++) {
                        for (let dz = -radius; dz <= radius; dz++) {
                            const distSq = dx * dx + dz * dz;
                            if (distSq > radius * radius || Math.random() < 0.3)
                                continue;
                            const x = center.x + dx;
                            const z = center.z + dz;
                            const y = center.y;
                            const above = dim.getBlock({ x, y, z });
                            if (above?.typeId === "minecraft:air") {
                                dim.runCommand(`setblock ${x} ${y} ${z} air`);
                                dim.runCommand(`setblock ${x} ${y - 1} ${z} minecraft:snow`);
                                dim.runCommand(`setblock ${x} ${y} ${z} minecraft:snow_layer`);
                            }
                        }
                    }
                    if (Math.random() < 0.3) {
                        placePackedIceCluster(center, dim);
                    }
                }, 2);
                system.runTimeout(placePatch, 3);
            }
        };
        placePatch();
    };
    frostRipple();
    const snowmanChance = Math.random();
    if (snowmanChance < 0.05) { // 5% chance to spawn a snowman
        world.getDimension("overworld").spawnEntity("minecraft:snow_golem", {
            x: origin.x + 0.5,
            y: origin.y + 1,
            z: origin.z + 0.5
        });
    }
}
