#!/usr/bin/env python3
"""AMARI ROOF storyboard — overlay teks presisi di atas visual Replicate (9:16, 1080x1920)."""
from PIL import Image, ImageDraw, ImageFont, ImageOps

W, H = 1080, 1920
RED = (198, 40, 40)        # #C62828
DARK = (33, 33, 33)        # dark gray
GRAY = (117, 117, 117)     # light gray text
LGRAY = (238, 238, 238)    # light gray bg
WHITE = (255, 255, 255)

FONT = '/home/mantapman/.fonts/Poppins-'
def F(name, size):
    return ImageFont.truetype(FONT + name + '.ttf', size)

f_brand  = F('SemiBold', 34)
f_scene  = F('Bold', 44)
f_title  = F('Bold', 78)
f_title2 = F('Bold', 64)
f_sub    = F('Regular', 38)
f_call   = F('SemiBold', 36)
f_small  = F('Regular', 32)
f_tiny   = F('Regular', 28)
f_big    = F('Bold', 130)

def new_canvas():
    img = Image.new('RGB', (W, H), WHITE)
    d = ImageDraw.Draw(img)
    d.rectangle([0, 0, W-1, H-1], outline=RED, width=8)
    return img, d

def scene_num(d, n, x=70, y=52):
    d.text((x, y), f"{n:02d}", font=f_scene, fill=RED)

def brand(d, x=70, y=60, anchor=None):
    d.text((x, y), "AMARI ROOF", font=f_brand, fill=GRAY)

def paste_visual(img, vpath, top, bot, x_margin=90):
    """Paste visual dengan aspect fit di area vertikal [top,bot]."""
    v = Image.open(vpath).convert('RGB')
    avail_h = bot - top
    avail_w = W - 2*x_margin
    scale = min(avail_w / v.width, avail_h / v.height)
    nw, nh = int(v.width*scale), int(v.height*scale)
    v = v.resize((nw, nh), Image.LANCZOS)
    x = (W - nw)//2
    y = top + (avail_h - nh)//2
    img.paste(v, (x, y))

def wrap(d, text, font, max_w):
    words = text.split()
    lines, cur = [], ""
    for w_ in words:
        t = (cur + " " + w_).strip()
        if d.textlength(t, font=font) <= max_w:
            cur = t
        else:
            if cur: lines.append(cur)
            cur = w_
    if cur: lines.append(cur)
    return lines

def draw_lines(d, lines, font, x, y, fill, gap=12, center=False):
    yy = y
    for ln in lines:
        if center:
            w_ = d.textlength(ln, font=font)
            d.text((x - w_/2, yy), ln, font=font, fill=fill)
        else:
            d.text((x, yy), ln, font=font, fill=fill)
        yy += font.size + gap
    return yy

# ============ FRAME 1 — OPENING ============
def frame1():
    img, d = new_canvas()
    scene_num(d, 1)
    brand(d)
    # Judul
    t1 = "Single Layer"
    t2 = "vs Double Layer"
    d.text((70, 130), t1, font=f_title, fill=DARK)
    w1 = d.textlength(t1, font=f_title)
    d.text((70 + w1 + 20, 130), t2, font=f_title, fill=RED)
    sub = "Which one is right for your building?"
    d.text((70, 240), sub, font=f_sub, fill=GRAY)
    d.line([70, 330, 1010, 330], fill=RED, width=4)
    paste_visual(img, 'video_orang/amari_v1.png', 380, 1760)
    img.save('video_orang/amari_s1.png')
    print("s1 ok")

# ============ FRAME 2 — SINGLE LAYER ============
def frame2():
    img, d = new_canvas()
    scene_num(d, 2)
    brand(d)
    d.text((70, 130), "Single Layer", font=f_title, fill=DARK)
    calls = ["1 UPVC Layer", "Lightweight", "Affordable", "Easy Installation"]
    x = 70
    y = 250
    for c in calls:
        w_ = d.textlength(c, font=f_call) + 46
        d.rounded_rectangle([x, y, x+w_, y+64], radius=32, fill=LGRAY)
        d.text((x+23, y+9), c, font=f_call, fill=DARK)
        x += w_ + 16
    paste_visual(img, 'video_orang/amari_v2.png', 360, 1520)
    # Aplikasi
    d.line([70, 1560, 1010, 1560], fill=RED, width=3)
    apps = ["Garage", "Workshop", "Canopy", "Warehouse", "Farm"]
    d.text((70, 1600), "Perfect for:", font=f_small, fill=GRAY)
    d.text((70, 1660), "  ·  ".join(apps), font=f_call, fill=DARK)
    img.save('video_orang/amari_s2.png')
    print("s2 ok")

# ============ FRAME 3 — DOUBLE LAYER ============
def frame3():
    img, d = new_canvas()
    scene_num(d, 3)
    brand(d)
    d.text((70, 130), "Double Layer", font=f_title, fill=DARK)
    calls = ["Air Chamber", "Better Insulation", "Quieter"]
    x = 70
    y = 250
    for c in calls:
        w_ = d.textlength(c, font=f_call) + 46
        d.rounded_rectangle([x, y, x+w_, y+64], radius=32, fill=LGRAY)
        d.text((x+23, y+9), c, font=f_call, fill=DARK)
        x += w_ + 16
    paste_visual(img, 'video_orang/amari_v3.png', 360, 1520)
    d.line([70, 1560, 1010, 1560], fill=RED, width=3)
    apps = ["House", "Factory", "School", "Commercial Building"]
    d.text((70, 1600), "Perfect for:", font=f_small, fill=GRAY)
    d.text((70, 1660), "  ·  ".join(apps), font=f_call, fill=DARK)
    img.save('video_orang/amari_s3.png')
    print("s3 ok")

# ============ FRAME 4 — ENGINEERING ============
def frame4():
    img, d = new_canvas()
    scene_num(d, 4)
    brand(d)
    d.text((70, 130), "Engineering", font=f_title, fill=DARK)
    d.text((70, 230), "Cross Section", font=f_title, fill=RED)
    d.line([70, 330, 1010, 330], fill=RED, width=4)
    paste_visual(img, 'video_orang/amari_v4.png', 380, 1680)
    # Label air chamber + panah
    d.text((70, 1730), "▲", font=f_call, fill=RED)
    d.text((110, 1722), "Air Chamber — the key insulation layer", font=f_small, fill=DARK)
    img.save('video_orang/amari_s4.png')
    print("s4 ok")

# ============ FRAME 5 — HEAT INSULATION ============
def frame5():
    img, d = new_canvas()
    scene_num(d, 5)
    brand(d)
    d.text((70, 130), "Heat Insulation", font=f_title, fill=DARK)
    d.line([70, 240, 1010, 240], fill=RED, width=4)
    paste_visual(img, 'video_orang/amari_v5.png', 290, 1560)
    # 38 vs 29
    d.text((140, 1620), "38°C", font=f_big, fill=RED)
    d.text((140, 1770), "Single Layer", font=f_call, fill=GRAY)
    w38 = d.textlength("38°C", font=f_big)
    d.text((W - 140 - w38, 1620), "29°C", font=f_big, fill=DARK)
    w29 = d.textlength("29°C", font=f_big)
    d.text((W - 140 - w29, 1770), "Double Layer", font=f_call, fill=GRAY, anchor="ra")
    d.text((W-140, 1770), "Double Layer", font=f_call, fill=GRAY)
    img.save('video_orang/amari_s5.png')
    print("s5 ok")

# ============ FRAME 6 — RAIN SOUND ============
def frame6():
    img, d = new_canvas()
    scene_num(d, 6)
    brand(d)
    d.text((70, 130), "Rain Sound", font=f_title, fill=DARK)
    d.line([70, 240, 1010, 240], fill=RED, width=4)
    paste_visual(img, 'video_orang/amari_v6.png', 290, 1600)
    d.text((90, 1660), "Conventional", font=f_call, fill=GRAY)
    d.text((90, 1720), "Loud 💥", font=f_call, fill=RED)
    d.text((W-90, 1660), "AMARI Double Layer", font=f_call, fill=DARK, anchor="ra")
    d.text((W-90, 1720), "Quiet 🤫", font=f_call, fill=DARK, anchor="ra")
    img.save('video_orang/amari_s6.png')
    print("s6 ok")

# ============ FRAME 7 — COMPARISON TABLE ============
def frame7():
    img, d = new_canvas()
    scene_num(d, 7)
    brand(d)
    d.text((70, 130), "Comparison", font=f_title, fill=DARK)
    d.line([70, 240, 1010, 240], fill=RED, width=4)
    paste_visual(img, 'video_orang/amari_v7.png', 290, 1000)
    # Tabel
    rows = [("Price", "Budget friendly", "Premium"),
            ("Weight", "Light", "Light"),
            ("Heat Insulation", "Standard", "Excellent"),
            ("Noise Reduction", "Moderate", "Superior"),
            ("Applications", "Outdoor / Utility", "Home / Commercial"),
            ("Comfort", "Good", "Maximum")]
    x0, x1, x2 = 70, 460, 760
    y0 = 1060
    d.rounded_rectangle([x0-12, y0-12, W-70+12, y0+46+len(rows)*76+12], radius=14, fill=LGRAY)
    d.text((x0, y0), "Feature", font=f_call, fill=RED)
    d.text((x1, y0), "Single Layer", font=f_call, fill=DARK)
    d.text((x2, y0), "Double Layer", font=f_call, fill=RED)
    yy = y0 + 78
    for r, s1, s2 in rows:
        d.text((x0, yy), r, font=f_small, fill=GRAY)
        d.text((x1, yy), s1, font=f_small, fill=DARK)
        d.text((x2, yy), s2, font=f_small, fill=DARK)
        yy += 76
    img.save('video_orang/amari_s7.png')
    print("s7 ok")

# ============ FRAME 8 — WHICH TO CHOOSE ============
def frame8():
    img, d = new_canvas()
    scene_num(d, 8)
    brand(d)
    d.text((70, 130), "Which Should", font=f_title, fill=DARK)
    d.text((70, 230), "You Choose?", font=f_title, fill=RED)
    d.line([70, 330, 1010, 330], fill=RED, width=4)
    paste_visual(img, 'video_orang/amari_v8.png', 380, 1500)
    # 2 kolom
    d.rounded_rectangle([70, 1560, 530, 1830], radius=16, fill=LGRAY)
    d.text((95, 1590), "Single Layer", font=f_call, fill=RED)
    for i, t in enumerate(["Budget priority", "Outdoor building", "Warehouse / Workshop"]):
        d.text((95, 1660 + i*58), f"• {t}", font=f_small, fill=DARK)
    d.rounded_rectangle([550, 1560, 1010, 1830], radius=16, fill=LGRAY)
    d.text((575, 1590), "Double Layer", font=f_call, fill=RED)
    for i, t in enumerate(["House / Office", "Factory / School", "Maximum comfort"]):
        d.text((575, 1660 + i*58), f"• {t}", font=f_small, fill=DARK)
    img.save('video_orang/amari_s8.png')
    print("s8 ok")

# ============ FRAME 9 — CLOSING ============
def frame9():
    img, d = new_canvas()
    scene_num(d, 9)
    paste_visual(img, 'video_orang/amari_v9.png', 80, 1300)
    # Logo AMARI besar
    d.text((70, 1390), "AMARI", font=f_big, fill=RED)
    d.text((70, 1560), "ROOF", font=f_title, fill=DARK)
    d.text((70, 1690), "UPVC Roofing System", font=f_small, fill=GRAY)
    d.line([70, 1780, 1010, 1780], fill=RED, width=4)
    tg = "Cooler. Quieter. Stronger."
    d.text((70, 1810), tg, font=f_sub, fill=DARK)
    img.save('video_orang/amari_s9.png')
    print("s9 ok")

frame1(); frame2(); frame3(); frame4(); frame5()
frame6(); frame7(); frame8(); frame9()

# Kompilasi vertikal
GAP = 14
frames = [Image.open(f'video_orang/amari_s{n}.png') for n in range(1, 10)]
tot = sum(f.height for f in frames) + GAP*8
comp = Image.new('RGB', (W, tot), WHITE)
y = 0
for f in frames:
    comp.paste(f, (0, y))
    y += f.height + GAP
comp.save('video_orang/amari_storyboard_final.png')
print("Kompilasi:", comp.size)
