#!/usr/bin/env python3
"""Vector base for the one-page Aster System sheet (blooms, tiles, swatches).
PhotoCraft adds the three field renders and all live type on top.

  python build_system_sheet.py <out.svg>
"""
import sys

from aster import SVG, Bloom, bloom_xf, col, PALETTE

W, H = 3600, 2400
M = 150
TILE, GAP = 600, 75
LEVELS = [  # (level, rays, seed, centre)
    (0, 8, 1, 0.22),
    (1, 8, 2, 0.22),
    (2, 8, 3, 0.22),
    (3, 21, 4, 0.27),
    (4, 30, 5, 0.28),
]
SWATCHES = ["dusty_violet", "lavender", "pale_aster", "deep_aster", "golden_center",
            "chai_brown", "dark_roast", "cream", "oat", "spice_orange"]


def tile_x(i):
    return M + i * (TILE + GAP)


def build(out):
    svg = SVG(W, H)
    svg.rect("bg", 0, 0, W, H, col("cream"))
    for i, (lvl, rays, seed, c) in enumerate(LEVELS):
        x = tile_x(i)
        svg.rect(f"tile-{lvl}", x, 470, TILE, TILE, "#EAE0CD")
        b = Bloom(petals=rays, abstraction=lvl, seed=seed, center=c)
        svg.open_group(f"bloom-level-{lvl}")
        for name, (fill, pb) in b.layers(bloom_xf(x + TILE / 2, 470 + TILE / 2, 250)).items():
            svg.path(f"l{lvl}-{name}", pb.d(), fill)
        svg.close_group()
    # field frames (PhotoCraft places the renders inside these)
    fw = (W - 2 * M - 2 * GAP) / 3
    for i, light in enumerate(["golden", "dusk", "overcast"]):
        svg.rect(f"frame-{light}", M + i * (fw + GAP), 1300, fw, fw * 9 / 16, "#D9CDB8")
    sw = (W - 2 * M - 9 * 30) / 10
    svg.open_group("palette")
    for i, name in enumerate(SWATCHES):
        fill = col(name)
        svg.rect(f"swatch-{name}", M + i * (sw + 30), 2040, sw, 130, fill)
    svg.close_group()
    # hairline under the cream swatch so it reads on the cream page
    i = SWATCHES.index("cream")
    svg.rect("swatch-cream-keyline", M + i * (sw + 30), 2168, sw, 2, "#CDBFA6")
    svg.save(out)
    return {"field_w": fw, "swatch_w": sw}


if __name__ == "__main__":
    print(build(sys.argv[1]))
