#!/usr/bin/env python3
"""
StudioSoft Carousel Generator — 3 Slide (Cover, Kenalan, Layanan)
Spec:
- Resolusi: 1080×1350 (4:5)
- BG: #1E40AF (Biru)
- Font: #FFFFFF (Putih)
- Subtitle: #B4C8F0 (Soft Blue)
- Margin: 50px rata semua sisi
- Konten: Vertical-middle (center)
- Font Judul: Poppins Bold 100px
- Font Body: Poppins Regular 28px
- Font Header/Footer: Bold 36px / Regular 20px
- Separator: 1 garis putus-putus, 60px dari judul → 55px ke isi
- Aturan Teks: Setiap kalimat titik (.) + enter
- Footer: www.studiosoft.id kiri — Swipe → kanan (slide 1)
"""

import os
from PIL import Image, ImageDraw, ImageFont

W, H = 1080, 1350
BG = "#1E40AF"
FONT_COLOR = "#FFFFFF"
SUB_COLOR = "#B4C8F0"
MARGIN = 50
OUT_DIR = os.path.dirname(os.path.abspath(__file__))

FONT_DIR = "/usr/share/fonts/truetype/poppins"

def load_font(name, size):
    path = os.path.join(FONT_DIR, name)
    if not os.path.exists(path):
        # fallback search
        for root, dirs, files in os.walk("/usr/share/fonts"):
            for f in files:
                if f.lower() == name.lower():
                    path = os.path.join(root, f)
                    break
    return ImageFont.truetype(path, size)

def draw_text_centered(draw, text, font, color, y_start, max_w, line_spacing=8):
    """Draw multiline text vertically centered within a width."""
    lines = []
    for line_text in text.split("\n"):
        words = line_text.split(" ")
        current = ""
        for word in words:
            test = current + " " + word if current else word
            bbox = draw.textbbox((0, 0), test, font=font)
            if bbox[2] - bbox[0] <= max_w:
                current = test
            else:
                lines.append(current)
                current = word
        if current:
            lines.append(current)
    
    total_h = sum(draw.textbbox((0, 0), l, font=font)[3] - draw.textbbox((0, 0), l, font=font)[1] for l in lines) + line_spacing * (len(lines) - 1)
    y = y_start - total_h // 2
    
    for line in lines:
        bbox = draw.textbbox((0, 0), line, font=font)
        lh = bbox[3] - bbox[1]
        x = (W - (bbox[2] - bbox[0])) // 2
        draw.text((x, y), line, font=font, fill=color)
        y += lh + line_spacing

def draw_dashed_line(draw, y, color, width=1, dash_len=20, gap_len=10):
    x_start = MARGIN
    x_end = W - MARGIN
    x = x_start
    while x < x_end:
        x2 = min(x + dash_len, x_end)
        draw.line([(x, y), (x2, y)], fill=color, width=width)
        x = x2 + gap_len

def create_slide(number, title_text, body_lines, is_cover=False):
    img = Image.new("RGB", (W, H), BG)
    draw = ImageDraw.Draw(img)
    
    # Fonts
    font_header = load_font("Poppins-Bold.ttf", 36)
    font_footer = load_font("Poppins-Regular.ttf", 20)
    font_title = load_font("Poppins-Bold.ttf", 100)
    font_body = load_font("Poppins-Regular.ttf", 28)
    
    # --- Header ---
    draw.text((MARGIN, MARGIN), "StudioSoft", font=font_header, fill=FONT_COLOR)
    
    # Page number top-right
    page_text = f"{number}/3"
    pb = draw.textbbox((0, 0), page_text, font=font_footer)
    draw.text((W - MARGIN - (pb[2] - pb[0]), MARGIN + 8), page_text, font=font_footer, fill=SUB_COLOR)
    
    # --- Content Area (vertical middle) ---
    # Header height ~ 50px, Footer height ~ 60px
    content_top = MARGIN + 60  # below header
    content_bot = H - MARGIN - 60  # above footer
    content_h = content_bot - content_top
    
    if is_cover:
        # Slide 1: Cover
        # Title
        draw_text_centered(draw, title_text, font_title, FONT_COLOR, content_top + content_h // 3, W - 2 * MARGIN)
        
        # Separator
        sep_y = content_top + content_h // 2 + 20
        draw_dashed_line(draw, sep_y, FONT_COLOR, width=2)
        
        # Subtitle
        sub_font = load_font("Poppins-Regular.ttf", 36)
        draw_text_centered(draw, body_lines, sub_font, SUB_COLOR, sep_y + 80, W - 2 * MARGIN)
        
    else:
        # Slides 2-3: Judul di atas, body di bawah
        # Title
        draw_text_centered(draw, title_text, font_title, FONT_COLOR, content_top + content_h // 3 - 30, W - 2 * MARGIN)
        
        # Separator
        sep_y = content_top + content_h // 2 + 10
        draw_dashed_line(draw, sep_y, FONT_COLOR, width=2)
        
        # Body
        body_font = load_font("Poppins-Regular.ttf", 28)
        draw_text_centered(draw, body_lines, body_font, FONT_COLOR, sep_y + 80, W - 2 * MARGIN)
    
    # --- Footer ---
    draw.text((MARGIN, H - MARGIN - 20), "www.studiosoft.id", font=font_footer, fill=FONT_COLOR)
    
    if is_cover:
        swipe_text = "Swipe →"
        sb = draw.textbbox((0, 0), swipe_text, font=font_footer)
        draw.text((W - MARGIN - (sb[2] - sb[0]), H - MARGIN - 20), swipe_text, font=font_footer, fill=SUB_COLOR)
    
    return img

def main():
    # Define slide content
    # Slide 1: Cover
    cover_title = "StudioSoft"
    cover_sub = "Partner Pertumbuhan\nBisnis Berbasis Data"
    
    # Slide 2: Kenalan
    kenalan_title = "Kenalan Yuk!"
    kenalan_body = "StudioSoft adalah Digital Agency\nSurabaya yang fokus bantu bisnis\ntumbuh lewat strategi digital.\nData-driven. Terukur. Terpercaya."
    
    # Slide 3: Layanan
    layanan_title = "Layanan Kami"
    layanan_body = "Digital Marketing\nSEO & SEM\nWeb Development\nMobile Apps\nSocial Media Management\neCommerce Solution"
    
    slides = [
        (1, cover_title, cover_sub, True),
        (2, kenalan_title, kenalan_body, False),
        (3, layanan_title, layanan_body, False),
    ]
    
    for num, title, body, is_cover in slides:
        img = create_slide(num, title, body, is_cover)
        fname = f"slide_{num:02d}_{'cover' if is_cover else 'konten'}.png"
        path = os.path.join(OUT_DIR, fname)
        img.save(path)
        print(f"✅ {fname} ({img.size[0]}×{img.size[1]})")
    
    print("\n✨ Selesai! Semua slide ada di folder studiosoft_carousel/")

if __name__ == "__main__":
    main()
