#!/usr/bin/env python3
"""
StudioSoft — IG Carousel (3 Slides) — BG PUTIH
Margin 50px rata • Konten vertical-middle
Judul 100px Poppins Bold • Body 28px Poppins Regular
1080x1350 (4:5) • BG Putih #FFFFFF • Font Biru #1E40AF
"""

from PIL import Image, ImageDraw, ImageFont
import os

OUT = os.path.expanduser("~/.openclaw/workspace-atun/studiosoft_carousel")
os.makedirs(OUT, exist_ok=True)

W, H = 1080, 1350
BG = (255, 255, 255)
WHITE = (30, 64, 175)
SOFT = (100, 130, 200)  # soft biru buat subtitle
M = 50
CONTENT_W = W - 2*M  # 980

FD = os.path.expanduser("~/.fonts")

J100 = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 100)
B36  = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 36)
R28  = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 28)
R22  = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 22)
R20  = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 20)

def fw(d, text, font):
    return d.textbbox((0,0), text, font=font)[2]

def fh(d, text, font):
    b = d.textbbox((0,0), text, font=font)
    return b[3] - b[1]

def wrap(d, text, font, mx):
    lines = []
    for p in text.split("\n"):
        ws = p.split()
        cur = ""
        for w in ws:
            t = (cur + " " + w) if cur else w
            if fw(d, t, font) > mx and cur:
                lines.append(cur)
                cur = w
            else:
                cur = t
        if cur: lines.append(cur)
    return lines

def block_h(d, content, font, mx, ls=1.35):
    """Total height of a wrapped block"""
    lines = wrap(d, content, font, mx)
    total = 0
    for line in lines:
        lh = fh(d, line, font)
        total += int(lh * ls)
    return total

def draw_block(d, text, x, y, font, fill, mx, ls=1.35):
    for line in wrap(d, text, font, mx):
        lh = fh(d, line, font)
        d.text((x, y), line, font=font, fill=fill)
        y += int(lh * ls)
    return y

def header(d, page):
    d.text((M, M), "STUDIOSOFT", font=B36, fill=WHITE)
    d.text((W-M, M), page, font=R22, fill=WHITE, anchor="rt")

def divider(d, x, y, w=300):
    dx = x
    while dx < x + w:
        e = min(dx + 14, x + w)
        d.line([(dx, y), (e, y)], fill=WHITE, width=2)
        dx += 24

def footer(d, left="www.studiosoft.id", right=""):
    d.text((M, H-130), left, font=R20, fill=WHITE)
    if right:
        d.text((W-M, H-130), right, font=R20, fill=WHITE, anchor="rt")

def clean_edges(im):
    px = im.load()
    for y in range(H):
        for x in range(3): px[x,y] = BG
        for x in range(W-3, W): px[x,y] = BG
    for y in range(H-5, H):
        for x in range(W): px[x,y] = BG
    for x in range(W):
        for y in range(3): px[x,y] = BG

# ==========================================
# SLIDE 1 — COVER
# ==========================================
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "01/03")

# Calculate total content height
hdr_h = fh(d, "STUDIOSOFT", B36)  # ~44px
total_h = 0

# Title lines
for t in ["PARTNER", "PERTUMBUHAN", "BISNIS", "BERBASIS DATA"]:
    total_h += int(fh(d, t, J100) * 1.15)

# Divider between titles and explanation — centered
divider_w = 300
divider_h = 5
total_h += 60 + 5 + 55  # 30px judul + 25px isi = 55px

# Subtitle
sub_h = block_h(d, "Digital Strategy  •  Branding  • Creative  •  AI  •  Automation", R28, CONTENT_W)
total_h += sub_h + 20

# Check items
check_h = 0
for item in ["☑  Digital Strategy & Consulting",
             "☑  Brand Identity & Creative",
             "☑  AI-Driven Automation"]:
    check_h += int(fh(d, item, R28) * 1.3)
total_h += check_h

# Header at y=M, footer at y=H-M-10
header_footer_h = hdr_h + int(fh(d, "", R20))  # rough
usable = H - 2*M - header_footer_h
start_y = M + hdr_h + 20 + (usable - total_h) // 2

# Render
ty = start_y
for t in ["PARTNER", "PERTUMBUHAN", "BISNIS", "BERBASIS DATA"]:
    d.text((M, ty), t, font=J100, fill=WHITE)
    ty += int(fh(d, t, J100) * 1.15)

# Divider — 30px from judul, 25px from isi = 55px total
ty += 30
divider(d, M, ty, divider_w)
ty += 25

ty = draw_block(d, "Digital Strategy  •  Branding  • Creative  •  AI  •  Automation", M, ty, R28, SOFT, CONTENT_W)
ty += 15

for item in ["☑  Digital Strategy & Consulting",
             "☑  Brand Identity & Creative",
             "☑  AI-Driven Automation"]:
    d.text((M, ty), item, font=R28, fill=WHITE)
    ty += int(fh(d, item, R28) * 1.3)

footer(d, "www.studiosoft.id", "Swipe →")
clean_edges(im)
im.save(f"{OUT}/01_cover.png")
print("✅ 01_cover.png")

# ==========================================
# SLIDE 2 — KENALAN
# ==========================================
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "02/03")

total_h = 0
# Title
j_h = int(fh(d, "STUDIOSOFT", J100) * 1.15)
j2_h = int(fh(d, "ADALAH", J100) * 1.15)
total_h += j_h + 15 + j2_h + 60 + 5 + 55  # titles + divider + gap (55px)

# Description
desc = "Partner pertumbuhan bisnis berbasis data dan teknologi.\nKami bantu bisnis Anda:"
desc_h = block_h(d, desc, R28, CONTENT_W)
total_h += desc_h + 15

# Check items
for item in ["☑  Strategi digital terukur",
             "☑  Branding kuat & konsisten",
             "☑  Optimasi AI & otomatisasi",
             "☑  Hasil terukur & di-scale"]:
    total_h += int(fh(d, item, R28) * 1.3)

usable = H - 2*M - hdr_h - 20
start_y = M + hdr_h + 20 + (usable - total_h) // 2

ty = start_y
d.text((M, ty), "STUDIOSOFT", font=J100, fill=WHITE)
ty += j_h

d.text((M, ty), "ADALAH", font=J100, fill=WHITE)
ty += j2_h

# Divider — 30px from judul, 25px from isi
ty += 30
divider(d, M, ty, 300)
ty += 25

ty = draw_block(d, desc, M, ty, R28, WHITE, CONTENT_W)
ty += 10

for item in ["☑  Strategi digital terukur",
             "☑  Branding kuat & konsisten",
             "☑  Optimasi AI & otomatisasi",
             "☑  Hasil terukur & di-scale"]:
    d.text((M, ty), item, font=R28, fill=WHITE)
    ty += int(fh(d, item, R28) * 1.3)

footer(d, "www.studiosoft.id")
clean_edges(im)
im.save(f"{OUT}/02_kenalan.png")
print("✅ 02_kenalan.png")

# ==========================================
# SLIDE 3 — LAYANAN
# ==========================================
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "03/03")

total_h = 0
titles_3 = ["APA YANG", "KAMI", "LAKUKAN?"]
for t in titles_3:
    total_h += int(fh(d, t, J100) * 1.15)
total_h += 60 + 5 + 55  # 55px total jarak dari judul ke isi

for item in ["☑  Digital Strategy & Consulting",
             "☑  Brand Identity & Creative",
             "☑  AI & Automation Solutions",
             "☑  Social Media Management",
             "☑  Data Analytics & Reporting"]:
    total_h += int(fh(d, item, R28) * 1.3)

usable = H - 2*M - hdr_h - 20
start_y = M + hdr_h + 20 + (usable - total_h) // 2

ty = start_y
for t in titles_3:
    d.text((M, ty), t, font=J100, fill=WHITE)
    ty += int(fh(d, t, J100) * 1.15)

ty += 30
divider(d, M, ty, 300)
ty += 25

for item in ["☑  Digital Strategy & Consulting",
             "☑  Brand Identity & Creative",
             "☑  AI & Automation Solutions",
             "☑  Social Media Management",
             "☑  Data Analytics & Reporting"]:
    d.text((M, ty), item, font=R28, fill=WHITE)
    ty += int(fh(d, item, R28) * 1.3)

footer(d, "www.studiosoft.id")
clean_edges(im)
im.save(f"{OUT}/03_layanan.png")
print("✅ 03_layanan.png")

print("\n📦 Selesai! 3 slides.")
