"""Builds the pictures of the WinZik site from the headless shots (tools/site/skins.sh first). Run: python3 -I apps/winzik/tools/site/make_site.py apps/winzik apps/winzik/var/screenshots"""
import os
import sys
from PIL import Image, ImageDraw, ImageFont

APP = sys.argv[1]
SHOTS = sys.argv[2]                       # main repo var/screenshots (tests + eq-givre)
SRC = os.path.join(APP, 'var/screenshots/site-src')
OUT = os.path.join(APP, 'site/images')
os.makedirs(os.path.join(OUT, 'skins'), exist_ok=True)

SKINS = 'givre aurore corail menthe ambre sakura neon-84 bois-laiton phosphore carbone classique ampli-2000 chaine-hifi the-skin base win48'.split()


def load(path):
    return Image.open(path).convert('RGB')


def fit(im, width):
    if im.width <= width:
        return im
    return im.resize((width, round(im.height * width / im.width)), Image.LANCZOS)


def webp(im, name, q=82):
    im.save(os.path.join(OUT, name), 'WEBP', quality=q, method=6)


# 16 skins: the player and the top of its play list
tiles = {}
for s in SKINS:
    tile = load(os.path.join(SRC, f'skin-{s}.png')).crop((30, 30, 676, 660))
    tiles[s] = tile
    webp(fit(tile, 323), f'skins/{s}.webp')

# hero: Givre playing, the card of a song
webp(fit(load(f'{SHOTS}/tests/card.png').crop((30, 30, 990, 1255)), 640), 'hero.webp')
# equalizer next to the player
webp(fit(load(f'{SHOTS}/eq-givre.png').crop((30, 30, 1600, 560)), 1000), 'equalizer.webp')
# lyrics bar at the bottom of the screen
webp(fit(load(f'{SHOTS}/eq-givre.png').crop((0, 1640, 1800, 1720)), 1000), 'lyrics-bar.webp', 90)
# full screen karaoke lyrics
webp(fit(load(f'{SHOTS}/tests/lyrics-full.png').crop((300, 0, 1800, 1400)), 700), 'lyrics-full.webp')
# Spotify import
webp(fit(load(f'{SHOTS}/tests/spotify-import.png').crop((300, 300, 1500, 1320)), 700), 'spotify.webp')
# stats and stars (card of the Sakura skin)
webp(fit(load(f'{SHOTS}/tests/skin-sakura.png').crop((20, 440, 1000, 900)), 735), 'stats.webp')
# skin editor
webp(fit(load(f'{SHOTS}/tests/edt.png'), 900), 'skin-editor.webp')


def banner(w, h):
    """The sharing picture: logo, a line of text, five skins."""
    img = Image.new('RGB', (w, h), (0, 128, 128))
    d = ImageDraw.Draw(img)
    k = w / 1200
    logo = load(os.path.join(APP, 'site/images/logo.png'))
    logo = logo.resize((round(488 * k), round(152 * k)), Image.LANCZOS)
    img.paste(logo, (round(40 * k), round(60 * k)))
    bold = ImageFont.truetype(os.path.join(APP, 'assets/fonts/ui-bold.ttf'), round(40 * k))
    font = ImageFont.truetype(os.path.join(APP, 'assets/fonts/ui.ttf'), round(30 * k))
    d.text((round(40 * k), round(250 * k)), 'Le lecteur MP3 à skins', font=bold, fill=(255, 255, 255))
    lines = ['16 skins · égaliseur · paroles karaoké', 'YouTube et Spotify · étoiles et stats',
             'Windows · Linux · Android · navigateur']
    for i, t in enumerate(lines):
        d.text((round(40 * k), round((320 + i * 48) * k)), t, font=font, fill=(255, 255, 160))
    d.rectangle((0, h - round(56 * k), w, h), fill=(0, 0, 0))
    d.text((round(40 * k), h - round(46 * k)), 'WinZik, WinZik, le son revient', font=font, fill=(255, 40, 40))
    # four skins in a grid on the right
    tw = round(250 * k)
    for i, s in enumerate(['givre', 'aurore', 'sakura', 'menthe']):
        t = tiles[s].resize((tw, round(tiles[s].height * tw / tiles[s].width)), Image.LANCZOS)
        x = w - round(36 * k) - (2 - i % 2) * (tw + round(10 * k))
        y = round(28 * k) + (i // 2) * (t.height + round(10 * k))
        d.rectangle((x - 2, y - 2, x + t.width + 1, y + t.height + 1), fill=(0, 0, 0))
        img.paste(t, (x, y))
    return img


banner(1200, 630).save(os.path.join(OUT, 'og.png'), optimize=True)
banner(1280, 720).resize((640, 360), Image.LANCZOS).save(os.path.join(APP, 'assets/thumbnail.png'), optimize=True)
print('ok')
