"""Assemble the fidex ArUco boards into a TRUE-SCALE A4 print sheet (300 DPI) with a 50mm
calibration square, so printed markers match the CAD pockets exactly. -> fidex_print_sheet.png
"""
import json
from PIL import Image, ImageDraw

D = "/data/cameron/repos/smith300_para_stuff"
DPI = 300
ppm = DPI / 25.4
W, H = int(210 * ppm), int(297 * ppm)
canvas = Image.new("RGB", (W, H), "white")
draw = ImageDraw.Draw(canvas)
cfg = json.load(open(f"{D}/fidex_config.json"))

margin = int(10 * ppm)
x, y = margin, margin
draw.text((x, y), "fidex marker sheet  DICT_4X4_250  (print at 100% / actual size)", fill="black")
y += int(6 * ppm)
sq = int(50 * ppm)
draw.rectangle([x, y, x + sq, y + sq], outline="black", width=3)
draw.text((x + 4, y + 4), "50mm calibration square", fill="black")
y += sq + int(12 * ppm)
rowh = 0
for c in cfg:
    px = int(round(c["board_length"] * 1000 * ppm))
    img = Image.open(f"{D}/fidex_markers/{c['link']}.png").convert("RGB").resize((px, px), Image.NEAREST)
    if x + px > W - margin:
        x = margin
        y += rowh + int(14 * ppm)
        rowh = 0
    canvas.paste(img, (x, y))
    draw.text((x, y + px + 2), f"{c['link']} {c['grid'][0]}x{c['grid'][1]} ids{c['ids'][0]}+ {c['board_length']*1000:.0f}mm", fill="black")
    x += px + int(12 * ppm)
    rowh = max(rowh, px)
canvas.save(f"{D}/fidex_print_sheet.png", dpi=(DPI, DPI))
print(f"wrote fidex_print_sheet.png  {W}x{H}px @ {DPI}dpi (A4)")
