# Copie delle immagini degli eventi per le varianti eventi-l3-a/b/c (mockup 30/10).
# Regola Owner (30/10): ogni immagine in una carta e una copia ~2x della misura mostrata, fatta UNA volta con Lanczos, stesso ritaglio centrato
# (object-fit cover); il browser non riduce mai piu di ~1,5x. Se la fonte e piu piccola del 2x si tiene la misura della fonte (mai ingrandire).
# Fonti: arte ufficiale Steam (library_hero_2x 3840x1240, library_600x900_2x 1200x1800) e foto Unsplash = simulazione delle immagini del gestore.
import os, sys, urllib.request
from PIL import Image
HERE = os.path.dirname(os.path.abspath(__file__)); SRC = os.path.join(HERE, 'src')
STEAM = 'https://cdn.cloudflare.steamstatic.com/steam/apps/{}/{}.jpg'
UNS = 'https://images.unsplash.com/photo-{}?w=3840&q=88&auto=format&fm=jpg'
EVENTS = {  # chiave: (tipo, id)
  'rl': ('steam', 252950), 'cs2': ('steam', 730), 'doom': ('steam', 2280), 'lol': ('uns', '1542751371-adc38448a05e'),
  'chill': ('uns', '1511512578047-dfb367046420'), 'valo': ('uns', '1538481199705-c710c4e965fc'),
  'dota': ('steam', 570), 'apex': ('steam', 1172470), 'pubg': ('steam', 578080), 'gta': ('steam', 271590),
  'dbd': ('steam', 381210), 'mhw': ('steam', 582010), 'w3': ('steam', 292030), 'retro': ('uns', '1550745165-9bc0b252726f'),
}
# misure MOSTRATE (la copia e il doppio): poster A, hero A, banda del calendario B, pannello B, righe C e Tornei, pannello C, fascia della pagina del torneo
SIZES = [('poster', 560, 840), ('hero', 1140, 972), ('band', 310, 80), ('pb', 736, 414), ('thumb', 176, 110), ('pc', 1234, 520), ('banner', 1400, 260)]
def get(url, path):
    if not os.path.exists(path):
        req = urllib.request.Request(url, headers={'user-agent': 'Mozilla/5.0'})
        with urllib.request.urlopen(req, timeout=60) as r, open(path, 'wb') as f: f.write(r.read())
    return Image.open(path).convert('RGB')
def cover(im, w, h):
    r = w / h; W, H = im.size
    if W / H > r: cw, ch = round(H * r), H
    else: cw, ch = W, round(W / r)
    x, y = (W - cw) // 2, (H - ch) // 2
    crop = im.crop((x, y, x + cw, y + ch))
    tw, th = (w * 2, h * 2) if cw >= w * 2 else (cw, ch)   # mai ingrandire
    return crop.resize((tw, th), Image.LANCZOS)
os.makedirs(SRC, exist_ok=True)
for key, (kind, ident) in EVENTS.items():
    if kind == 'steam':
        land = get(STEAM.format(ident, 'library_hero_2x'), os.path.join(SRC, f'{key}-land.jpg'))
        port = get(STEAM.format(ident, 'library_600x900_2x'), os.path.join(SRC, f'{key}-port.jpg'))
    else:
        land = port = get(UNS.format(ident), os.path.join(SRC, f'{key}-land.jpg'))
    for name, w, h in SIZES:
        out = cover(port if name == 'poster' else land, w, h)
        out.save(os.path.join(HERE, f'{key}-{name}.jpg'), quality=84, optimize=True, progressive=True)
        print(key, name, f'{w}x{h}', '->', out.size)
