"""One Chromium browser, sequential. Usage: shots.py variant[,variant] [slug,...] [w,...] [theme,...] [outdir]"""
import sys, asyncio
from pathlib import Path
from playwright.async_api import async_playwright
PORT = {"before": 18471, "after": 18472}
SUN = {"midnight": "night", "pearl": "day"}
variants = sys.argv[1].split(","); slugs = sys.argv[2].split(","); widths = [int(w) for w in sys.argv[3].split(",")]
themes = sys.argv[4].split(","); out = Path(sys.argv[5] if len(sys.argv) > 5 else "raw"); out.mkdir(exist_ok=True)
async def main():
    async with async_playwright() as p:
        b = await p.chromium.launch()
        try:
            for w in widths:
                ctx = await b.new_context(viewport={"width": w, "height": 844 if w < 700 else 900}, device_scale_factor=2 if w < 700 else 1,
                                          is_mobile=w < 700, has_touch=w < 700)
                pg = await ctx.new_page()
                for v in variants:
                    for s in slugs:
                        for t in themes:
                            await pg.goto(f"http://127.0.0.1:{PORT[v]}/ato/{s}/?preview-sun={SUN[t]}", wait_until="networkidle")
                            await pg.evaluate("document.fonts.ready")
                            if v == "after":
                                await pg.evaluate("(()=>{const b=document.querySelector('.ob-ttg');if(b)b.click()})()")
                            await pg.wait_for_timeout(700)
                            f = out / f"{v}-{s}-{w}-{t}.png"
                            await pg.screenshot(path=str(f), full_page=True)
                            print(f)
                await ctx.close()
        finally:
            await b.close()
asyncio.run(main())
