"""The pictures of the site from the app's own screenshots (python3 -I tools/site/make_images.py, after tools/site/shots.sh)."""
import sys
from pathlib import Path
from PIL import Image

app = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[2]
shots, site = app / 'var/screenshots', app / 'site/images'
site.mkdir(parents=True, exist_ok=True)

chat = Image.open(shots / 'chat.png').convert('RGB')
login = Image.open(shots / 'login.png').convert('RGB')

def fit(img, w):
    return img.resize((w, round(img.height * w / img.width)), Image.LANCZOS)

fit(chat, 1024).save(site / 'chat.png', optimize=True)
fit(login.crop((560, 300, 1490, 1100)), 520).save(site / 'login.png', optimize=True)
# the room with its smileys, close up
fit(chat.crop((0, 80, 1100, 470)), 900).save(site / 'smileys.png', optimize=True)

def cover(img, w, h):
    r = max(w / img.width, h / img.height)
    big = img.resize((round(img.width * r), round(img.height * r)), Image.LANCZOS)
    return big.crop((0, 0, w, h))

for name in ('rooms', 'search', 'private'):
    fit(Image.open(shots / f'tab-{name}.png').convert('RGB'), 1024).save(site / f'{name}.png', optimize=True)
phone = Image.open(shots / 'phone.png').convert('RGB')
fit(phone, 390).save(site / 'phone.png', optimize=True)

cover(chat.crop((0, 0, 1600, 900)), 1200, 630).save(site / 'og.png', optimize=True)
cover(chat.crop((0, 0, 1600, 900)), 1280, 720).save(app / 'assets/thumbnail.png', optimize=True)

# the favicon / icon: the applet's wink, drawn by its own code (tests/fixtures/eyes_01.png), pixels x4
face = Image.open(app / 'tests/fixtures/eyes_01.png').convert('RGBA').crop((0, 0, 24, 24))
icon = face.resize((96, 96), Image.NEAREST)
icon.save(site / 'favicon.png')
icon.resize((192, 192), Image.NEAREST).save(app / 'assets/icon.png')
print('images written')
