#!/usr/bin/env python3
"""Generate artwork templates that match the firmware's HUD geometry.

Reproduces the line math from display_proto.ino's drawHousing() (and the
reserved element regions) so artwork can be built to align exactly.

    ./.venv/bin/python make_template.py

Outputs (172x320, transparent background):
    border_template.png  - just the trapezoidal/hex border lines
    layout_guide.png     - border + shaded regions the HUD draws over, labeled
"""

from PIL import Image, ImageDraw, ImageFont

W, H = 172, 320
CYAN = (0, 255, 255, 255)
TEAL = (0, 87, 95, 255)


def t(x):
    """Mimic C's truncation when assigning a double to an int (positive vals)."""
    return int(x)


def border_pairs():
    """The exact endpoint pairs drawHousing() draws (each a 1px line)."""
    top_line_width = t(W * 0.5)              # 86
    top_inset = (W - top_line_width) // 2    # 43
    bottom_line_width = t(W * 0.70)          # 120
    bottom_inset = (W - bottom_line_width) // 2  # 26
    hex_pivot = t(H * 0.66)                  # 211

    return [
        # top edge (50% width)
        ((top_inset, 0), (top_line_width + top_inset, 0)),
        ((top_inset, 1), (top_line_width + top_inset, 1)),
        # bottom edge (70% width)
        ((bottom_inset, H - 1), (bottom_line_width + bottom_inset, H - 1)),
        ((bottom_inset, H - 2), (bottom_line_width + bottom_inset, H - 2)),
        # top slants down to the widest point (hex_pivot)
        ((top_inset, 0), (0, hex_pivot)),
        ((top_inset + 1, 0), (1, hex_pivot)),
        ((top_inset + top_line_width, 0), (W - 1, hex_pivot)),
        ((top_inset + top_line_width - 1, 0), (W - 2, hex_pivot)),
        # bottom slants
        ((0, hex_pivot), (bottom_inset, H - 1)),
        ((1, hex_pivot), (bottom_inset + 1, H - 2)),
        ((W - 1, hex_pivot), (bottom_inset + bottom_line_width, H - 1)),
        ((W - 2, hex_pivot), (bottom_inset + bottom_line_width - 1, H - 1)),
    ]


def draw_border(d):
    for a, b in border_pairs():
        d.line([a, b], fill=CYAN, width=1)


def draw_border_template():
    img = Image.new("RGBA", (W, H), (0, 0, 0, 0))
    draw_border(ImageDraw.Draw(img))
    img.save("border_template.png")


def draw_layout_guide():
    img = Image.new("RGBA", (W, H), (0, 0, 0, 0))
    d = ImageDraw.Draw(img, "RGBA")

    try:
        font = ImageFont.load_default()
    except Exception:
        font = None

    # Reserved element regions (matches updateDisplay/draw* in the firmware).
    # Ammo panel: solid teal at runtime.
    d.rectangle([20, 160, 20 + 132 - 1, 160 + 100 - 1],
                fill=(0, 87, 95, 90), outline=TEAL)
    # Compass box: blitted as a solid BLACK 70x70 square at runtime.
    d.rectangle([51, 51, 51 + 70 - 1, 51 + 70 - 1],
                fill=(255, 255, 255, 35), outline=(160, 160, 160, 255))
    # Battery bar: 40% width, y 6..8.
    bw = t(W * 0.40); bi = (W - bw) // 2
    d.rectangle([bi, 6, bi + bw, 8], fill=(0, 255, 255, 170))
    # Load bar: 50% width, y 132..134.
    lw = t(W * 0.50); li = (W - lw) // 2
    d.rectangle([li, 132, li + lw, 134], fill=(255, 255, 0, 170))
    # Bullet/X graphic bounding box, y 280..300.
    d.rectangle([35, 280, 137, 300], outline=(0, 255, 255, 170))

    draw_border(d)

    if font:
        d.text((54, 84), "COMPASS", fill=(200, 200, 200, 255), font=font)
        d.text((40, 205), "AMMO", fill=(120, 220, 230, 255), font=font)
        d.text((bi, 11), "batt", fill=(0, 255, 255, 255), font=font)
        d.text((li, 137), "load", fill=(220, 220, 0, 255), font=font)
        d.text((46, 303), "BULLET", fill=(0, 255, 255, 255), font=font)

    img.save("layout_guide.png")


if __name__ == "__main__":
    draw_border_template()
    draw_layout_guide()
    print("Wrote border_template.png and layout_guide.png (172x320, transparent)")
