#!/usr/bin/env python3
"""Draw the measured GB-per-hour chart (HTML, 1200 px wide) from measure.txt (output of measure.py).
Usage: python3 make-chart.py measure.txt gb-per-hour-chart.html"""
import html, os, sys

if len(sys.argv) != 3:
    print(__doc__); sys.exit(2)
src, out = sys.argv[1], sys.argv[2]
if not os.path.isfile(src) or os.path.getsize(src) == 0:
    print(f"missing or empty: {src}"); sys.exit(1)
gbh = {}
for line in open(src, encoding="utf-8", errors="replace"):
    p = [x.strip() for x in line.split("|")]
    if len(p) >= 6 and p[0].endswith(".mp4"):
        try:
            gbh[p[0]] = float(p[5])
        except ValueError:
            pass
want = [
    ("c720-yt5000-a384.mp4", "YouTube 720p30", "5,000 + 384 kbps, two-pass", "yt"),
    ("c1080-yt8000-a384.mp4", "YouTube 1080p30", "8,000 + 384 kbps, two-pass", "yt"),
    ("c1080-crf23-a384.mp4", "1080p CRF 23", "quality target, same interview", "crf"),
    ("c1080-sp25000cbr-a128.mp4", "Spotify 1080p", "25 Mbit/s CBR + 128 kbps", "sp"),
    ("k2160-crf23-a384.mp4", "4K CRF 23", "quality target, 4K interview", "crf"),
    ("k2160-yt35000-a384.mp4", "YouTube 4K, low end", "35,000 + 384 kbps, two-pass", "yt"),
    ("k2160-sp35000cbr-a128.mp4", "Spotify 4K", "35 Mbit/s CBR + 128 kbps", "sp"),
    ("k2160-yt45000-a384.mp4", "YouTube 4K, high end", "45,000 + 384 kbps, two-pass", "yt"),
]
rows = [(lab, sub, gbh[f], k) for f, lab, sub, k in want if f in gbh]
if len(rows) != len(want):
    print("missing rows in", src); sys.exit(1)
W, left, right, top, rowh = 1200, 450, 170, 170, 86
H = top + rowh * len(rows) + 100
scale = (W - left - right) / max(r[2] for r in rows)
color = {"yt": "#2f5d8a", "sp": "#1f7a4d", "crf": "#b5651d"}
x10 = left + 10 * scale
svg = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" viewBox="0 0 {W} {H}">',
       f'<rect width="{W}" height="{H}" fill="#ffffff"/>',
       '<text x="40" y="60" font-size="38" font-weight="700" fill="#1a1a1a">One hour of video podcast, measured GB</text>',
       '<text x="40" y="104" font-size="28" fill="#555">H.264 + AAC in MP4. GB = 1,000,000,000 bytes.</text>',
       f'<line x1="{x10:.1f}" y1="{top - 20}" x2="{x10:.1f}" y2="{top + rowh * len(rows)}" stroke="#1f7a4d" '
       'stroke-width="3" stroke-dasharray="10 8"/>',
       f'<text x="{x10 + 10:.1f}" y="{top - 6}" font-size="28" fill="#1f7a4d">Spotify: under 10 GB</text>']
for i, (lab, sub, g, kind) in enumerate(rows):
    y = top + i * rowh
    w = max(g * scale, 3)
    svg.append(f'<text x="40" y="{y+30}" font-size="28" font-weight="600" fill="#1a1a1a">{html.escape(lab)}</text>')
    svg.append(f'<text x="40" y="{y+68}" font-size="28" fill="#666">{html.escape(sub)}</text>')
    svg.append(f'<rect x="{left}" y="{y+14}" width="{w:.1f}" height="40" rx="4" fill="{color[kind]}"/>')
    svg.append(f'<text x="{left + w + 12:.1f}" y="{y+44}" font-size="28" font-weight="700" fill="#1a1a1a">{g:.2f} GB</text>')
svg.append(f'<text x="40" y="{H-36}" font-size="28" fill="#666">Source: measure.txt (60 s test encodes scaled to 1 hour).</text>')
svg.append("</svg>")
doc = ('<!doctype html><html><head><meta charset="utf-8"><style>html,body{margin:0;background:#fff;'
       'font-family:Inter,Helvetica,Arial,sans-serif}</style></head><body>' + "".join(svg) + "</body></html>")
open(out, "w", encoding="utf-8").write(doc)
print(f"wrote {out} {W}x{H} rows={len(rows)}")
