#!/usr/bin/env python3
"""Generate the generic, mobile-first welcome-video poster."""

from pathlib import Path

from PIL import Image, ImageDraw, ImageFont


ROOT = Path(__file__).resolve().parents[2]
OUTPUT = ROOT / "apps/web/public/images/default-welcome-poster.png"
WIDTH, HEIGHT = 720, 1280


def font(size: int, bold: bool = False):
    candidates = [
        "/System/Library/Fonts/Supplemental/Georgia Bold.ttf" if bold else "/System/Library/Fonts/Supplemental/Georgia.ttf",
        "/System/Library/Fonts/Supplemental/Arial Bold.ttf" if bold else "/System/Library/Fonts/Supplemental/Arial.ttf",
    ]
    for candidate in candidates:
        if Path(candidate).is_file():
            return ImageFont.truetype(candidate, size)
    return ImageFont.load_default()


def centered(draw: ImageDraw.ImageDraw, y: int, text: str, selected_font, fill: str):
    box = draw.textbbox((0, 0), text, font=selected_font)
    draw.text(((WIDTH - (box[2] - box[0])) / 2, y), text, font=selected_font, fill=fill)


def main():
    image = Image.new("RGB", (WIDTH, HEIGHT))
    pixels = image.load()
    start, middle, end = (255, 248, 251), (246, 215, 230), (217, 239, 231)
    for y in range(HEIGHT):
        ratio = y / (HEIGHT - 1)
        left, right, blend = (start, middle, ratio / 0.55) if ratio < 0.55 else (middle, end, (ratio - 0.55) / 0.45)
        color = tuple(round(left[i] + (right[i] - left[i]) * blend) for i in range(3))
        for x in range(WIDTH):
            pixels[x, y] = color

    draw = ImageDraw.Draw(image)
    draw.ellipse((170, 242, 550, 622), fill="#fffdfa")
    draw.polygon([(360, 615), (218, 484), (187, 405), (205, 343), (270, 304), (360, 332), (450, 304), (515, 343), (533, 405), (502, 484)], fill="#7b2d58")
    draw.ellipse((254, 389, 302, 437), fill="#fffdfa")
    draw.ellipse((418, 389, 466, 437), fill="#fffdfa")
    centered(draw, 740, "Welcome to", font(62, True), "#241725")
    centered(draw, 820, "our wedding", font(70, True), "#241725")
    draw.line((220, 902, 500, 902), fill="#9d6382", width=3)
    centered(draw, 950, "MADE IN HEAVEN", font(28), "#3f6f73")
    centered(draw, 998, "WEDDING REELS", font(24), "#7b2d58")
    image.save(OUTPUT, "PNG", optimize=True)


if __name__ == "__main__":
    main()
