load("render.star", "render")

# Homepage preview: a configurable countdown value, not a live date calculation.
def at(x, y, child):
    return render.Padding(pad = (x, y, 0, 0), child = child)

# Ray colours from dim to bright; the sun breathes through them and back.
RAY_GLOW = ["#b88340", "#cf954a", "#e5a955", "#f5b961", "#ffd37a"]

def sun_rays(phase):
    # A compact sun with separate rays stays recognisable on a 64x32 panel.
    # The rays brighten and stretch on a 0..7..1 triangle across the loop, so
    # the sun radiates slowly instead of strobing.
    step = phase if phase <= 7 else 14 - phase
    level = step * (len(RAY_GLOW) - 1) // 7
    bright = RAY_GLOW[level]
    dim = RAY_GLOW[max(level - 2, 0)]
    reach = 1 if level >= 3 else 0
    return [
        at(55, 4, render.Box(width = 5, height = 5, color = "#ffd37a")),
        at(57, 1 - reach, render.Box(width = 1, height = 2 + reach, color = bright)),
        at(57, 10, render.Box(width = 1, height = 2 + reach, color = bright)),
        at(52 - reach, 6, render.Box(width = 2 + reach, height = 1, color = bright)),
        at(61, 6, render.Box(width = 2 + reach, height = 1, color = bright)),
        # Diagonals sit one pixel off each corner and trail the main rays.
        at(53, 2, render.Box(width = 1, height = 1, color = dim)),
        at(61, 2, render.Box(width = 1, height = 1, color = dim)),
        at(53, 10, render.Box(width = 1, height = 1, color = dim)),
        at(61, 10, render.Box(width = 1, height = 1, color = dim)),
    ]

def frame(days, title, caption, phase):
    sun = sun_rays(phase)
    water = [
        at(0, 26, render.Box(width = 64, height = 1, color = "#17435c")),
        at(0, 27, render.Box(width = 64, height = 5, color = "#071e2c")),
    ]
    # Slow glints on the water; text never moves or flashes.
    for row in range(2):
        for col in range(5):
            x = (col * 14 + phase + row * 5) % 60
            water.append(at(x, 28 + row * 2, render.Box(
                width = 3, height = 1,
                color = "#236581" if row == 0 else "#123d55",
            )))
    return render.Box(
        width = 64, height = 32, color = "#060d16",
        child = render.Stack(children = [
            at(2, 3, render.Text(title, font = "tb-8", color = "#7bd7ff")),
            at(0, 15, render.Box(width = 64, height = 9, child = render.Row(
                main_align = "center", cross_align = "center",
                children = [
                    render.Text(days, font = "tb-8", color = "#d6f4ff"),
                    render.Padding(pad = (4, 0, 0, 0),
                        child = render.Text(caption, font = "tb-8", color = "#70b6d1")),
                ],
            ))),
        ] + sun + water),
    )

def main(config):
    days = config.get("days", "12")
    title = config.get("title", "VAKANTIE")
    caption = config.get("caption", "DAGEN")
    frames = [frame(days, title, caption, i) for i in range(14)]
    return render.Root(delay = 180, child = render.Animation(children = frames))
