"""Render the cinematic page's Discover laptop screen (#discUI) flat, for /my and /sg.

Usage (W = a scratch folder; needs a venv with playwright, opencv-python-headless, numpy):
    python dom.py W                       -> W/dom-my.png, W/dom-sg.png  (2880 x 1954)
    ffmpeg -i public/cinematic/assets/lid-open.mp4 -vsync 0 W/lid/f%03d.png
    python lidcomp2.py W film             -> W/lid/o%03d.png  (then encode like the other films:
        ffmpeg -framerate 24 -i W/lid/o%03d.png -c:v libx264 -profile:v high -pix_fmt yuv420p -g 4
        -bf 0 -b:v 7M -an -movflags +faststart public/cinematic/sg/assets/lid-open.mp4)
    python lidcomp2.py W still public/cinematic/assets/lap_open.webp W/lap.png  (-> save as WebP)
See docs/modules_handbook/main/cinematic-site/readMe.md, "The Singapore page".
"""
import time, sys
from playwright.sync_api import sync_playwright
S = sys.argv[1]
JS = """()=>{
  const w=document.getElementById('lapLive'); document.body.appendChild(w);
  w.style.cssText+='position:fixed;left:0;top:0;transform:none;opacity:1;visibility:visible;z-index:2147483647;display:block';
  [...w.children].forEach(c=>{const on=c.id==='discUI'; c.style.display=on?'flex':'none'; if(on){c.style.opacity='1';c.style.visibility='visible';c.style.transform='none';}});
  w.querySelectorAll('#discUI *').forEach(e=>{if(getComputedStyle(e).opacity==='0') e.style.opacity='1';});
}"""
with sync_playwright() as p:
    b = p.chromium.launch(executable_path='/usr/bin/google-chrome', args=['--use-angle=swiftshader', '--enable-unsafe-swiftshader'])
    for code in ['my', 'sg']:
        ctx = b.new_context(viewport={'width': 1600, 'height': 1100}, device_scale_factor=2)
        pg = ctx.new_page(); pg.goto(f'https://wk.propertylab.com.my/{code}', wait_until='load'); time.sleep(8)
        pg.evaluate(JS); time.sleep(3)
        pg.locator('#lapLive').screenshot(path=f'{S}/dom-{code}.png')
        ctx.close()
    b.close()
