import json, sys, zipfile, hashlib
from pathlib import Path
sys.path.insert(0,str(Path(__file__).parent/'python-deps'))
from PIL import Image
r=Path(__file__).parent
bpath=r/'before-compat-1795560942653'
apaths=[r/'after-compat-1795561028486',r/'after-compat-1795561138201']
b=json.load(open(bpath/'compatibility.json'))
a={'side':'after','sha':'fbf3db96503648fb2ac814b5ad9fe1e25c72f6f4','cases':[],'condition':'idle-page correctness, three real pastes per case'}
for p in apaths:a['cases']+=json.load(open(p/'compatibility.json'))['cases']
expected={c['name']:c['samples'][0]['output']['sha256'] for c in b['cases']}
for c in a['cases']:
 c['matchesBase']=all(s['output']['sha256']==expected[c['name']] for s in c['samples'])
 assert c['matchesBase'],(c['mode'],c['name'])
 assert len(c['samples'])==3
 print(c['mode'],c['name'],'MATCH',c['samples'][0]['output']['pixelP3'])
(r/'compatibility-before.json').write_text(json.dumps(b,indent=2));(r/'compatibility-after.json').write_text(json.dumps(a,indent=2))
assert len(a['cases'])==22

def collage(paths,out,cols=2):
 w,h=730,800
 image=Image.new('RGB',(w*cols,h*((len(paths)+cols-1)//cols)), 'white')
 for i,p in enumerate(paths):image.paste(Image.open(p).convert('RGB'),((i%cols)*w,(i//cols)*h))
 image.save(r/out)
collage([bpath/('normal-'+n+'-ready.png') for n in ['srgb.png','p3.png','photo.heic','photo-p3.heic']],'compatibility-before.png')
collage([apaths[0]/('normal-'+n+'-ready.png') for n in ['srgb.png','p3.png','photo.heic','photo-p3.heic']],'compatibility-after.png')
selected=[apaths[0]/'no-worker-p3.png-ready.png',apaths[0]/'constructor-error-photo-p3.heic-ready.png',apaths[1]/'worker-no-offscreen-photo-p3.heic-ready.png',apaths[1]/'no-offscreen-photo-p3.heic-ready.png',apaths[1]/'worker-error-srgb.png-ready.png',apaths[1]/'worker-error-photo-p3.heic-ready.png']
collage(selected,'fallbacks-after.png')
# Contact sheets allow visual inspection of every screenshot included in the archive.
for i,p in enumerate([bpath,*apaths]):
 paths=sorted(p.glob('*-ready.png'));collage(paths,f'inspection-{i}.png')
files={}
for name in ['README.md','driver.cjs','page-driver.js','electron-main.cjs','compatibility.cjs','make-heic.py','comparison.py','comparison.png','compatibility-before.json','compatibility-after.json','synthetic.heic','synthetic-p3.heic']:
 files[name]=r/name
for target,folder in [('before','before-1795560816561'),('after','after-1795560889077'),('repeat-before','before-1795560687631'),('repeat-after','after-1795560761985')]:
 p=r/folder
 for name in ['measurements.json','summary.png','paste-demo.mp4','source-large.png','source-small.png']:
  if(p/name).exists():files[target+'/'+name]=p/name
for target,p in [('compatibility-before',bpath),('compatibility-after-part1',apaths[0]),('compatibility-after-part2',apaths[1])]:
 for file in p.iterdir():
  if file.suffix in ['.png','.jpg','.json'] and file.name!='failure.json':files[target+'/'+file.name]=file
with zipfile.ZipFile(r/'image-paste-proof.zip','w',zipfile.ZIP_DEFLATED) as z:
 for name,p in files.items():z.write(p,name)
print('ZIP', (r/'image-paste-proof.zip').stat().st_size)
