import os
from PIL import Image, ImageDraw, ImageFont

FONT = "/usr/share/fonts/truetype/arphic/uming.ttc"
W = H = 1080
MARGIN_H = 80
BOTTOM_MARGIN = 90
BOX_PAD_X = 28
BOX_PAD_Y = 18
BOX_ALPHA = 160  # 0-255
FONT_SIZE = 46

captions = [
  "第一步：装好 Arduino IDE，并用 USB 连接 UNO。",
  "选择开发板 Arduino Uno，端口选到对应串口。",
  "打开示例：文件→示例→基础→Blink。",
  "确认 LED_BUILTIN 对应板载灯（通常是 13 号脚）。",
  "点击上传，等待编译并写入开发板。",
  "上传成功后，板载 LED 就会开始闪烁。",
  "想改节奏，把 delay 的 1000 改成 500。",
  "外置 LED：长脚接 13，短脚经电阻接 GND。",
  "第一课完成；下一步我们读传感器数据。",
]

srcs = [
  "/home/kuhnn/.openclaw/media/inbound/4261603d-6050-4121-82b5-05d0ca67ab0b.jpg",
  "/home/kuhnn/.openclaw/media/inbound/0b5ecf65-94cd-4316-b91b-48e17ae13954.jpg",
  "/home/kuhnn/.openclaw/media/inbound/0b2f24ea-f117-4f2a-bfe0-8272316c6c42.jpg",
  "/home/kuhnn/.openclaw/media/inbound/d340c899-06d3-4978-a424-8c45f8ed30d1.jpg",
  "/home/kuhnn/.openclaw/media/inbound/04001a7b-5e42-472f-994a-5d0e5c44baff.jpg",
  "/home/kuhnn/.openclaw/media/inbound/dfbd5963-0cc4-460e-96df-692fa267dbb7.jpg",
  "/home/kuhnn/.openclaw/media/inbound/2f18b02a-4011-41a3-9d6f-b0fec8e7ac0a.jpg",
  "/home/kuhnn/.openclaw/media/inbound/42b1d0ff-8582-4b99-9ca0-8edacf8cb58a.jpg",
  "/home/kuhnn/.openclaw/media/inbound/272ab93e-6967-4f06-9741-441b58560205.jpg",
]

assert len(srcs) == len(captions)

font = ImageFont.truetype(FONT, FONT_SIZE)


def wrap(draw, text, max_w):
    # naive wrap by characters
    lines = []
    cur = ""
    for ch in text:
        test = cur + ch
        if draw.textlength(test, font=font) <= max_w:
            cur = test
        else:
            if cur:
                lines.append(cur)
            cur = ch
    if cur:
        lines.append(cur)
    # merge to at most 2 lines if possible
    return lines


def fit_to_square(im):
    im = im.convert("RGB")
    iw, ih = im.size
    scale = min(W / iw, H / ih)
    nw, nh = int(iw * scale), int(ih * scale)
    im = im.resize((nw, nh), Image.LANCZOS)
    bg = Image.new("RGB", (W, H), (0, 0, 0))
    bg.paste(im, ((W - nw) // 2, (H - nh) // 2))
    return bg


os.makedirs("captioned", exist_ok=True)

for idx, (src, cap) in enumerate(zip(srcs, captions), start=1):
    base = fit_to_square(Image.open(src))
    rgba = base.convert("RGBA")
    overlay = Image.new("RGBA", (W, H), (0, 0, 0, 0))
    d = ImageDraw.Draw(overlay)

    max_w = W - 2 * MARGIN_H
    lines = wrap(d, cap, max_w)
    # limit to 2 lines
    if len(lines) > 2:
        lines = ["".join(lines[:-1]), lines[-1]]

    # measure
    line_h = FONT_SIZE + 10
    text_h = line_h * len(lines)
    text_w = max(d.textlength(line, font=font) for line in lines)

    x0 = (W - text_w) / 2 - BOX_PAD_X
    y0 = H - BOTTOM_MARGIN - text_h - BOX_PAD_Y
    x1 = (W + text_w) / 2 + BOX_PAD_X
    y1 = H - BOTTOM_MARGIN + BOX_PAD_Y

    d.rounded_rectangle([x0, y0, x1, y1], radius=22, fill=(0, 0, 0, BOX_ALPHA))

    y = y0 + BOX_PAD_Y
    for line in lines:
        tw = d.textlength(line, font=font)
        x = (W - tw) / 2
        d.text((x, y), line, font=font, fill=(255, 255, 255, 255))
        y += line_h

    out = Image.alpha_composite(rgba, overlay).convert("RGB")
    out_path = os.path.join("captioned", f"{idx:02d}.jpg")
    out.save(out_path, quality=95)
    print(out_path)
