"""Generates the PWA icons and favicons in frontend/public from the festival font.

Usage: python3 scripts/generate_pwa_icons.py frontend/public/fonts/GreedWide-Medium.ttf frontend/public
"""
import sys
from PIL import Image, ImageDraw, ImageFont
font_path, out = sys.argv[1], sys.argv[2]
BG = (15, 23, 42)       # slate-900, the manifest theme colour
FG = (245, 158, 11)     # amber-500, the festival accent
def icon(size, scale, rounded=False):
    s = 1024
    img = Image.new('RGBA', (s, s), (0, 0, 0, 0))
    d = ImageDraw.Draw(img)
    if rounded:
        d.rounded_rectangle([0, 0, s - 1, s - 1], radius=int(s * 0.22), fill=BG)
    else:
        d.rectangle([0, 0, s, s], fill=BG)
    text = 'LFŠ'
    fs = 400
    f = ImageFont.truetype(font_path, fs)
    w = d.textbbox((0, 0), text, font=f)[2]
    fs = int(fs * (s * scale) / w)
    f = ImageFont.truetype(font_path, fs)
    l, t, r, b = d.textbbox((0, 0), text, font=f)
    d.text(((s - (r - l)) / 2 - l, (s - (b - t)) / 2 - t), text, font=f, fill=FG)
    bar_w = int(s * scale * 0.5); bar_h = int(s * 0.03)
    y = (s + (b - t)) / 2 + s * 0.06
    d.rectangle([(s - bar_w) / 2, y, (s + bar_w) / 2, y + bar_h], fill=FG)
    return img.resize((size, size), Image.LANCZOS)
icon(192, 0.72).save(f'{out}/pwa-192x192.png')
icon(512, 0.72).save(f'{out}/pwa-512x512.png')
icon(512, 0.52).save(f'{out}/maskable-512x512.png')   # content inside the 80 % safe zone
icon(180, 0.66).convert('RGB').save(f'{out}/apple-touch-icon.png')
fav = icon(64, 0.8, rounded=True)
fav.save(f'{out}/favicon.ico', sizes=[(16, 16), (32, 32), (48, 48), (64, 64)])
icon(32, 0.8, rounded=True).save(f'{out}/favicon-32x32.png')
