#!/usr/bin/env python3
"""
GUARDiA Messenger 앱 에셋 생성
- icon.png        1024x1024
- splash.png      1284x2778 (iPhone 14 Pro Max 기준)
- adaptive-icon.png  1024x1024 (Android)
- favicon.png     48x48
- notification-icon.png  96x96
"""
from PIL import Image, ImageDraw, ImageFont
import os

OUT = os.path.dirname(os.path.abspath(__file__)) + '/assets'
os.makedirs(OUT, exist_ok=True)

BRAND   = (26, 58, 107)     # #1a3a6b
ACCENT  = (79, 110, 247)    # #4f6ef7
WHITE   = (255, 255, 255)
LIGHT   = (232, 236, 255)   # #e8ecff

def make_icon(size: int, path: str):
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    d   = ImageDraw.Draw(img)
    r   = size // 6
    # 배경 라운드 사각형
    d.rounded_rectangle([0, 0, size-1, size-1], radius=r, fill=BRAND)
    # 방패 모양 (간단한 버전)
    cx, cy = size // 2, size // 2
    sh = int(size * 0.55)
    sw = int(size * 0.44)
    # 방패 외곽
    pts = [
        (cx, cy - sh//2),
        (cx + sw//2, cy - sh//4),
        (cx + sw//2, cy + sh//8),
        (cx, cy + sh//2),
        (cx - sw//2, cy + sh//8),
        (cx - sw//2, cy - sh//4),
    ]
    d.polygon(pts, fill=WHITE)
    # 방패 내부 (파란색)
    inner = int(0.8)
    pts2 = [(x + (cx - x)*0.18, y + (cy - y)*0.18) for x, y in pts]
    d.polygon(pts2, fill=ACCENT)
    # 중앙 G 텍스트
    font_size = size // 4
    try:
        font = ImageFont.truetype('/Windows/Fonts/arialbd.ttf', font_size)
    except:
        try:
            font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', font_size)
        except:
            font = ImageFont.load_default()
    bbox = d.textbbox((0, 0), 'G', font=font)
    tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
    d.text((cx - tw//2, cy - th//2 - size//12), 'G', fill=WHITE, font=font)
    img.save(path, 'PNG')
    print(f'  {path} ({size}x{size})')

def make_splash(path: str):
    W, H = 1284, 2778
    img = Image.new('RGB', (W, H), BRAND)
    d   = ImageDraw.Draw(img)
    # 배경 그라디언트 효과 (간단히)
    for y in range(H):
        alpha = y / H
        r = int(BRAND[0] * (1 - alpha * 0.3))
        g = int(BRAND[1] * (1 - alpha * 0.3))
        b = int(min(255, BRAND[2] + alpha * 30))
        d.line([(0, y), (W, y)], fill=(r, g, b))
    # 중앙 아이콘 영역
    icon_size = 220
    icon_x = (W - icon_size) // 2
    icon_y = (H - icon_size) // 2 - 80
    tmp_icon = OUT + '/tmp_icon.png'
    make_icon(icon_size, tmp_icon)
    icon = Image.open(tmp_icon).convert('RGBA')
    img.paste(icon, (icon_x, icon_y), icon)
    os.remove(tmp_icon)
    # 텍스트
    cx = W // 2
    ty = icon_y + icon_size + 30
    try:
        font_lg = ImageFont.truetype('/Windows/Fonts/arialbd.ttf', 72)
        font_sm = ImageFont.truetype('/Windows/Fonts/arial.ttf', 36)
    except:
        try:
            font_lg = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 72)
            font_sm = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 36)
        except:
            font_lg = font_sm = ImageFont.load_default()
    d.text((cx, ty), 'GUARDiA', fill=WHITE, font=font_lg, anchor='mt')
    d.text((cx, ty + 86), 'AI 인프라 자율 운영 플랫폼', fill=(170, 196, 232), font=font_sm, anchor='mt')
    d.text((cx, ty + 140), '(주)지오정보기술', fill=(100, 120, 160), font=font_sm, anchor='mt')
    img.save(path, 'PNG')
    print(f'  {path} (1284x2778 splash)')

def make_notification_icon(size: int, path: str):
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    d   = ImageDraw.Draw(img)
    d.ellipse([0, 0, size-1, size-1], fill=BRAND)
    try:
        font = ImageFont.truetype('/Windows/Fonts/arialbd.ttf', size // 2)
    except:
        try:
            font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', size // 2)
        except:
            font = ImageFont.load_default()
    cx, cy = size // 2, size // 2
    bbox = d.textbbox((0, 0), 'G', font=font)
    tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
    d.text((cx - tw//2, cy - th//2), 'G', fill=WHITE, font=font)
    img.save(path, 'PNG')
    print(f'  {path} ({size}x{size})')

print('GUARDiA 앱 에셋 생성 중...')
make_icon(1024, f'{OUT}/icon.png')
make_icon(1024, f'{OUT}/adaptive-icon.png')
make_icon(48,   f'{OUT}/favicon.png')
make_notification_icon(96, f'{OUT}/notification-icon.png')
make_splash(f'{OUT}/splash.png')
print('모든 에셋 생성 완료!')
