import { Color } from @core:color;
import { Gfx } from @sdk:gfx;

declare global ticks: int = 0;
declare const SCREEN_W: int = 320;
declare const SCREEN_H: int = 180;
declare const CELL: int = 8;

[Init]
fn init() -> void
{
    ticks = 0;
}

fn abs_i(v: int) -> int
{
    if (v < 0) {
        return 0 - v;
    }
    return v;
}

[Frame]
fn frame() -> void
{
    ticks += 1;
    let cx = SCREEN_W / 2;
    let cy = SCREEN_H / 2;
    let phase = ticks / 2;

    Gfx.clear(new Color(33));

    for y: int from 0 until SCREEN_H step CELL {
        let dy = y - cy;
        for x: int from 0 until SCREEN_W step CELL {
            let dx = x - cx;
            let md = abs_i(dx) + abs_i(dy);
            let seed_x = dx / 3 + ((phase % 29) - 14);
            let seed_y = dy / 3 + (((phase * 3) % 23) - 11);

            let zx: int = seed_x;
            let zy: int = seed_y;
            let iter: int = 0;

            while iter < 3 {
                let ax = abs_i(zx);
                let ay = abs_i(zy);
                let nx = ((zx * zx - zy * zy) / 48) + seed_x + ((ay * 3) / 8) - 9;
                let ny = ((ax * zy) / 24) + seed_y + ((phase + iter * 7) % 17) - 8;
                zx = nx;
                zy = ny;
                iter += 1;
            }

            let axf = abs_i(zx);
            let ayf = abs_i(zy);
            let ripple = (md / 9 + phase + ((dx - dy) / 12)) % 64;
            let cloud = ((axf * 2) + (ayf * 3) + ripple + ((x - y + phase) % 29)) % 128;
            let glow = ((abs_i(dx / 3) + abs_i(dy / 2)) / 8 + phase) % 32;
            let energy = (cloud + glow) % 128;

            let r = if energy > 95 {
                6 + ((phase + x / CELL) % 6)
            } else if energy > 63 {
                4 + ((energy + phase) % 8)
            } else {
                (energy / 24) % 3
            };

            let g = if energy > 95 {
                6 + ((phase + y / CELL) % 12)
            } else if energy > 63 {
                8 + ((energy + phase) % 14)
            } else {
                2 + ((energy / 12) % 5)
            };

            let b = if energy > 95 {
                24 + ((phase + x / CELL + y / CELL) % 5)
            } else if energy > 63 {
                18 + ((energy + phase) % 10)
            } else {
                7 + ((energy / 6) % 12)
            };

            let color_raw = r * 2048 + g * 32 + b;
            Gfx.fill_rect(x, y, CELL, CELL, new Color(color_raw));
        }
    }

    let star_a = (phase * 3) % SCREEN_W;
    let star_b = (phase * 5 + 37) % SCREEN_W;
    let star_c = (phase * 7 + 91) % SCREEN_W;
    let star_d = (phase * 11 + 143) % SCREEN_W;
    let star_e = (phase * 13 + 211) % SCREEN_W;
    let star_f = (phase * 17 + 17) % SCREEN_W;

    let star_y0 = 18 + ((phase * 2) % 24);
    let star_y1 = 42 + ((phase * 3) % 18);
    let star_y2 = 70 + ((phase * 4) % 22);
    let star_y3 = 104 + ((phase * 5) % 16);
    let star_y4 = 132 + ((phase * 6) % 20);
    let star_y5 = 154 + ((phase * 7) % 14);

    let twinkle0 = phase % 6;
    let twinkle1 = (phase + 2) % 6;
    let twinkle2 = (phase + 4) % 6;
    let twinkle3 = (phase + 1) % 6;
    let twinkle4 = (phase + 3) % 6;
    let twinkle5 = (phase + 5) % 6;

    let star_s0 = 1 + (twinkle0 / 4);
    let star_s1 = 1 + (twinkle1 / 4);
    let star_s2 = 1 + (twinkle2 / 4);
    let star_s3 = 1 + (twinkle3 / 4);
    let star_s4 = 1 + (twinkle4 / 4);
    let star_s5 = 1 + (twinkle5 / 4);

    let star_c0 = 57023 + twinkle0 * 1056;
    let star_c1 = 48607 + twinkle1 * 1376;
    let star_c2 = 44383 + twinkle2 * 1584;
    let star_c3 = 61279 + twinkle3 * 704;
    let star_c4 = 40159 + twinkle4 * 1696;
    let star_c5 = 52831 + twinkle5 * 992;

    Gfx.fill_rect(star_a, star_y0, star_s0, star_s0, new Color(star_c0));
    Gfx.fill_rect(star_b, star_y1, star_s1, star_s1, new Color(star_c1));
    Gfx.fill_rect(star_c, star_y2, star_s2, star_s2, new Color(star_c2));
    Gfx.fill_rect(star_d, star_y3, star_s3, star_s3, new Color(star_c3));
    Gfx.fill_rect(star_e, star_y4, star_s4, star_s4, new Color(star_c4));
    Gfx.fill_rect(star_f, star_y5, star_s5, star_s5, new Color(star_c5));
}
// Estimated frame cost: ~920 fills + ~2760 fractal-iteration steps + 6 twinkling parallax stars/frame at CELL=8.
