"""Feature overview for the listing: the hero render with labelled call-outs (system python: numpy + Pillow)."""
import os
import sys

import numpy as np
from PIL import Image, ImageDraw, ImageFont

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
from render import Camera, render  # noqa: E402
from render_views import parts, stand_parts, BASE_Y  # noqa: E402

G = os.path.normpath(os.path.join(HERE, "..", "render", "gallery"))
W, H = 2000, 1250
INK, MUTED, ACCENT = (36, 40, 48), (96, 102, 112), (232, 96, 76)
FONT = "/System/Library/Fonts/Helvetica.ttc"


def font(size, bold=False):
    try:
        return ImageFont.truetype(FONT, size, index=1 if bold else 0)
    except OSError:
        return ImageFont.load_default()


# (anchor point in the device frame, label side, label y in px, title, detail)
CALLOUTS = [
    ((45.4, 37.0, 17.8), "right", 230, "Twist privacy shutter", "Turn 60° and two blades click shut"),
    ((45.4, 0.0, 13.25), "right", 560, "Hidden status light", "LEDs glow through a 0.4 mm skin"),
    ((22.5, -5.6, 6.5), "right", 860, "Every port usable", "USB-C, USB-A, Qwiic, microSD, I2S"),
    ((-6.3, 25.6, 1.0), "left", 470, "microSD scoop", "Swap cards without opening the case"),
    ((26.0, -17.1, -16.7), "left", 760, "Tilt joint, -35° to +45°", "GoPro standard, M5 bolt and knob"),
    ((26.6, -45.0, -16.7), "left", 1040, "One rail, 5 mounts", "Desk stand shown; GoPro, tripod, wall, magnet"),
]


def main():
    cam = Camera(eye=(-150, 62, 255), target=(30.0, 4.0, 0.0), fov=33, width=W, height=H)
    im = render(parts() + stand_parts(), cam, ss=2, ground_y=BASE_Y).convert("RGB")
    d = ImageDraw.Draw(im)
    ft, fd = font(38, True), font(28)
    for (p, side, ly, title, detail) in CALLOUTS:
        sx, sy, _ = cam.project(np.array([p], float))
        ax, ay = float(sx[0]), float(sy[0])
        tx = W - 600 if side == "right" else 70
        lx = tx - 24 if side == "right" else tx + 470
        d.line([(ax, ay), (lx, ly + 22)], fill=MUTED, width=3)
        d.ellipse([ax - 8, ay - 8, ax + 8, ay + 8], fill=ACCENT, outline=(255, 255, 255), width=3)
        d.text((tx, ly), title, fill=INK, font=ft)
        d.text((tx, ly + 48), detail, fill=MUTED, font=fd)
    d.text((70, 70), "Coral Eye", fill=INK, font=font(64, True))
    d.text((70, 150), "AI camera case for the Coralboard SL · no supports · built on Google's reference CAD",
           fill=MUTED, font=font(30))
    p = os.path.join(G, "00_features.png")
    im.save(p)
    print("saved", p)


if __name__ == "__main__":
    main()
