from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
import re, sys, textwrap
root=Path(__file__).parent
side=sys.argv[1]
sha={'before':'049e83f234','after':'e4885cf15a'}[side]
labels=[]
for line in (root/f'{side}-accessibility.txt').read_text().splitlines():
    if '[button]' in line and 'reset credit' in line:
        labels.append(re.search(r'"(.*)"',line).group(1))
assert len(labels)==4, labels
canvas=Image.new('RGB',(1500,1400),'#f5f6f8')
d=ImageDraw.Draw(canvas)
font=lambda n:ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',n)
d.text((30,22),f'{side.upper()}  {sha}  |  Pixel 9 / Android 36 / English / light theme',fill='#14181c',font=font(26))
shot=Image.open(root/f'{side}.png').convert('RGB'); shot.thumbnail((560,1257))
canvas.paste(shot,(25,105))
d.text((630,110),'Accessibility labels from the running app',fill='#14181c',font=font(29))
d.text((630,160),'Unmodified Android accessibility tree excerpts',fill='#59636d',font=font(22))
for i,label in enumerate(labels):
    y=235+i*245
    d.text((630,y),f'Legend row {i+1}',fill='#59636d',font=font(23))
    for j,line in enumerate(textwrap.wrap(label,43)):
        color=('#ae2525' if side=='before' else '#147847') if '1 reset credit' in line else '#14181c'
        d.text((630,y+43+j*40),line,fill=color,font=font(28))
d.text((630,1255),'Left: actual app screenshot (scaled).',fill='#59636d',font=font(21))
d.text((630,1290),'Right: captured labels; this panel is an annotation.',fill='#59636d',font=font(21))
canvas.save(root/f'{side}-evidence.png')
(root/f'{side}-labels.txt').write_text('\n'.join(labels)+'\n')
