"""Deterministic original inputs for a reversible Studio banner test."""
from pathlib import Path
import hashlib
import json
import random
from PIL import Image, ImageDraw, ImageFont

ROOT = Path(__file__).resolve().parent
FONT = '/System/Library/Fonts/Supplemental/Arial.ttf'

def ruler(width, height, filename):
    image = Image.new('RGB', (width, height), '#f4f0df')
    draw = ImageDraw.Draw(image)
    for x in range(0, width, 100):
        draw.rectangle((x, 0, min(x + 99, width - 1), height - 1), fill=('#ddebd6' if x // 100 % 2 else '#e7e5d1'))
        draw.line((x, 0, x, height - 1), fill='#586750', width=2)
        draw.text((x + 5, 32), str(x), fill='#17382b', font=ImageFont.truetype(FONT, 24))
    for y in range(0, height, 100):
        draw.line((0, y, width - 1, y), fill='#788477', width=2)
        draw.text((5, y + 4), f'y={y}', fill='#17382b', font=ImageFont.truetype(FONT, 24))
    scale = width / 2048
    sw, sh = 1235 * scale, 338 * scale
    x, y = (width - sw) / 2, (height - sh) / 2
    draw.rectangle((round(x), round(y), round(x + sw), round(y + sh)), outline='#156b46', width=8)
    center = width // 2, height // 2
    draw.line((center[0] - 50, center[1], center[0] + 50, center[1]), fill='#b92d31', width=6)
    draw.line((center[0], center[1] - 50, center[0], center[1] + 50), fill='#b92d31', width=6)
    draw.text((center[0] - 200, center[1] + 80), 'CENTER / 100 px grid', fill='#17382b', font=ImageFont.truetype(FONT, 30))
    draw.text((130, height - 110), f'{width} x {height} / Original test input / Green = centered model', fill='#17382b', font=ImageFont.truetype(FONT, 30))
    image.save(ROOT / filename)

ruler(2560, 1440, 'banner-ruler-2560x1440.png')
ruler(2048, 1152, 'banner-ruler-2048x1152.png')
ruler(1920, 1080, 'banner-undersize-1920x1080.png')
ruler(2560, 1600, 'banner-other-ratio-2560x1600.png')
# Native RGB noise gives a valid, decodable PNG above both common MB interpretations.
Image.frombytes('RGB', (2560, 1440), random.Random(328).randbytes(2560 * 1440 * 3)).save(ROOT / 'banner-oversize-2560x1440.png')
manifest = []
for path in sorted(ROOT.glob('*.png')):
    with Image.open(path) as image:
        image.verify()
    with Image.open(path) as image:
        manifest.append({'file': path.name, 'width': image.width, 'height': image.height,
                         'bytes': path.stat().st_size, 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()})
assert next(x['bytes'] for x in manifest if x['file'].startswith('banner-oversize')) > 6 * 1024 * 1024
assert all(x['bytes'] < 6000000 for x in manifest if not x['file'].startswith('banner-oversize'))
(ROOT / 'manifest.json').write_text(json.dumps(manifest, indent=2) + '\n')
print(json.dumps(manifest, indent=2))
