#!/usr/bin/env python3
"""
Generate AeThex Engine branding assets
"""

from PIL import Image, ImageDraw, ImageFont
import math

# AeThex brand colors
CYAN = (0, 217, 255)
PURPLE = (139, 92, 246)
DARK_BG = (30, 30, 46)
WHITE = (255, 255, 255)

def draw_hexagon(draw, center, size, fill, outline=None, width=1):
    """Draw a hexagon shape"""
    x, y = center
    angles = [i * 60 for i in range(6)]
    points = []
    for angle in angles:
        rad = math.radians(angle - 30)
        px = x + size * math.cos(rad)
        py = y + size * math.sin(rad)
        points.append((px, py))
    
    if fill:
        draw.polygon(points, fill=fill)
    if outline:
        draw.polygon(points, outline=outline, width=width)
    
    return points

def create_icon(size=256, filename="icon.png"):
    """Create AeThex hexagon icon with 'A'"""
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    center = (size // 2, size // 2)
    hex_size = size * 0.38
    
    # Draw hexagon with gradient effect
    draw_hexagon(draw, center, hex_size + 4, None, CYAN, 8)
    draw_hexagon(draw, center, hex_size, DARK_BG)
    
    # Draw 'A' letter
    try:
        font_size = int(size * 0.5)
        font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
    except:
        font = ImageFont.load_default()
    
    # Center the text
    text = "A"
    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    text_x = center[0] - text_width // 2
    text_y = center[1] - text_height // 2 - (bbox[1] + bbox[3]) // 2
    
    # Draw text with gradient colors
    draw.text((text_x, text_y), text, font=font, fill=CYAN)
    
    img.save(filename)
    print(f"✓ Created {filename} ({size}x{size})")
    return img

def create_logo(width=620, height=300, filename="logo.png"):
    """Create AeThex Engine logo with text"""
    img = Image.new('RGBA', (width, height), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # Draw hexagon icon on left
    icon_size = int(height * 0.6)
    icon_center_x = int(height * 0.4)
    icon_center_y = height // 2
    hex_size = icon_size * 0.4
    
    draw_hexagon(draw, (icon_center_x, icon_center_y), hex_size + 3, None, CYAN, 6)
    draw_hexagon(draw, (icon_center_x, icon_center_y), hex_size, DARK_BG)
    
    # Draw 'A' in hexagon
    try:
        icon_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", int(icon_size * 0.5))
    except:
        icon_font = ImageFont.load_default()
    
    bbox = draw.textbbox((0, 0), "A", font=icon_font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    text_x = icon_center_x - text_width // 2
    text_y = icon_center_y - text_height // 2 - (bbox[1] + bbox[3]) // 2
    draw.text((text_x, text_y), "A", font=icon_font, fill=CYAN)
    
    # Draw "AeThex Engine" text
    try:
        title_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", int(height * 0.35))
        subtitle_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", int(height * 0.18))
    except:
        title_font = ImageFont.load_default()
        subtitle_font = ImageFont.load_default()
    
    # "AeThex" in cyan
    text_x = int(height * 0.7)
    title_y = height // 2 - int(height * 0.15)
    draw.text((text_x, title_y), "AeThex", font=title_font, fill=CYAN)
    
    # "Engine" in white
    bbox = draw.textbbox((text_x, title_y), "AeThex ", font=title_font)
    engine_x = bbox[2]
    draw.text((engine_x, title_y), "Engine", font=title_font, fill=WHITE)
    
    img.save(filename)
    print(f"✓ Created {filename} ({width}x{height})")
    return img

def create_splash(width=620, height=300, filename="splash.png"):
    """Create splash screen"""
    img = Image.new('RGB', (width, height), DARK_BG)
    draw = ImageDraw.Draw(img)
    
    # Same as logo but with background
    # Draw hexagon icon
    icon_size = int(height * 0.6)
    icon_center_x = int(height * 0.4)
    icon_center_y = height // 2
    hex_size = icon_size * 0.4
    
    draw_hexagon(draw, (icon_center_x, icon_center_y), hex_size + 3, None, CYAN, 6)
    draw_hexagon(draw, (icon_center_x, icon_center_y), hex_size, (20, 20, 30))
    
    # Draw 'A'
    try:
        icon_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", int(icon_size * 0.5))
        title_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", int(height * 0.35))
    except:
        icon_font = ImageFont.load_default()
        title_font = ImageFont.load_default()
    
    bbox = draw.textbbox((0, 0), "A", font=icon_font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    text_x = icon_center_x - text_width // 2
    text_y = icon_center_y - text_height // 2 - (bbox[1] + bbox[3]) // 2
    draw.text((text_x, text_y), "A", font=icon_font, fill=CYAN)
    
    # Draw text
    text_x = int(height * 0.7)
    title_y = height // 2 - int(height * 0.15)
    draw.text((text_x, title_y), "AeThex", font=title_font, fill=CYAN)
    
    bbox = draw.textbbox((text_x, title_y), "AeThex ", font=title_font)
    engine_x = bbox[2]
    draw.text((engine_x, title_y), "Engine", font=title_font, fill=WHITE)
    
    img.save(filename)
    print(f"✓ Created {filename} ({width}x{height})")
    return img

if __name__ == "__main__":
    print("🎨 Generating AeThex Engine branding assets...\n")
    
    # Main icons
    create_icon(256, "icon.png")
    create_icon(128, "icon_128.png")
    create_icon(64, "icon_64.png")
    create_icon(256, "main/app_icon.png")
    
    # Logos
    create_logo(620, 300, "logo.png")
    
    # Splash
    create_splash(620, 300, "main/splash.png")
    
    print("\n✅ All branding assets generated!")
    print("\n📝 Files created:")
    print("   - icon.png (256x256)")
    print("   - icon_128.png")
    print("   - icon_64.png")
    print("   - logo.png (620x300)")
    print("   - main/app_icon.png (256x256)")
    print("   - main/splash.png (620x300)")
    print("\n🔧 Next step: Rebuild the engine to see the new branding!")
