"""yohaku — render every piece at the seed its published image used.

    python yohaku-gen.py [out_dir]

Each piece is its own file next to this one (uzu.py and so on) with a
render(seed) function; the shared helpers live in _common.py. Writes the
4K, FHD and thumbnail JPEGs the site serves.
"""
import importlib.util
import os
import sys

from PIL import Image

WORKS = [
    ("kaze-no-kioku", 11),
    ("toukousen", 23),
    ("shiosai", 31),
    ("awa", 47),
    ("koushi", 53),
    ("shoji", 61),
    ("housha", 71),
    ("kiri", 83),
    ("kanshou", 97),
    ("ashiato", 101),
    ("yamanami", 113),
    ("uzu", 127),
]


def load(slug):
    here = os.path.dirname(os.path.abspath(sys.argv[0]))
    sys.path.insert(0, here)
    spec = importlib.util.spec_from_file_location(slug.replace("-", "_"), os.path.join(here, slug + ".py"))
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


def save(img, out, slug):
    img = img.convert("RGB")
    img.save(f"{out}/{slug}-4k.jpg", quality=92, optimize=True, progressive=True)
    img.resize((1920, 1080), Image.LANCZOS).save(f"{out}/{slug}-fhd.jpg", quality=90, optimize=True, progressive=True)
    img.resize((960, 540), Image.LANCZOS).save(f"{out}/{slug}-thumb.jpg", quality=90, optimize=True, progressive=True)


if __name__ == "__main__":
    out = sys.argv[1] if len(sys.argv) > 1 else "out"
    os.makedirs(out, exist_ok=True)
    for slug, seed in WORKS:
        save(load(slug).render(seed), out, slug)
        print("ok", slug)
