#!/usr/bin/env python3
"""
Generate placeholder icons for ZPTBox application
"""

from PIL import Image, ImageDraw, ImageFont
import os

# Create icons directory if it doesn't exist
os.makedirs('icons', exist_ok=True)
os.makedirs('images', exist_ok=True)

def create_icon(text, color, filename, size=(32, 32)):
    """Create a simple icon with text"""
    img = Image.new('RGBA', size, (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    
    # Draw background
    draw.rectangle([0, 0, size[0]-1, size[1]-1], fill=color, outline=(100, 100, 100))
    
    # Try to use a font, fallback to default if not available
    try:
        font = ImageFont.truetype("arial.ttf", min(size)//3)
    except:
        font = ImageFont.load_default()
    
    # Calculate text position
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]
    text_x = (size[0] - text_width) // 2
    text_y = (size[1] - text_height) // 2
    
    # Draw text
    draw.text((text_x, text_y), text, fill=(255, 255, 255), font=font)
    
    img.save(filename)
    print(f"Created: {filename}")

def create_app_icon():
    """Create main application icon in multiple sizes"""
    sizes = [(16, 16), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)]
    
    for size in sizes:
        img = Image.new('RGBA', size, (255, 255, 255, 0))
        draw = ImageDraw.Draw(img)
        
        # Draw gradient background
        for i in range(size[1]):
            color_value = int(50 + (150 * i / size[1]))
            draw.rectangle([0, i, size[0], i+1], fill=(0, color_value, 200, 255))
        
        # Draw "ZPT" text
        try:
            font_size = max(8, size[0]//4)
            font = ImageFont.truetype("arial.ttf", font_size)
        except:
            font = ImageFont.load_default()
        
        text = "ZPT"
        text_bbox = draw.textbbox((0, 0), text, font=font)
        text_width = text_bbox[2] - text_bbox[0]
        text_height = text_bbox[3] - text_bbox[1]
        text_x = (size[0] - text_width) // 2
        text_y = (size[1] - text_height) // 2
        
        draw.text((text_x, text_y), text, fill=(255, 255, 255), font=font)
        
        if size == (32, 32):
            img.save('icons/app_icon.png')
        elif size == (256, 256):
            img.save('icons/app_icon_256.png')
    
    # Create ICO file with multiple sizes
    img_16 = Image.new('RGBA', (16, 16), (0, 100, 200, 255))
    img_32 = Image.new('RGBA', (32, 32), (0, 100, 200, 255))
    img_48 = Image.new('RGBA', (48, 48), (0, 100, 200, 255))
    
    # Save as ICO (Windows icon format)
    img_32.save('icons/app_icon.ico', format='ICO', sizes=[(16, 16), (32, 32), (48, 48)])
    print("Created: icons/app_icon.ico")

def create_splash_screen():
    """Create splash screen image"""
    img = Image.new('RGBA', (600, 400), (240, 240, 240, 255))
    draw = ImageDraw.Draw(img)
    
    # Draw gradient background
    for i in range(400):
        color_value = int(200 - (100 * i / 400))
        draw.rectangle([0, i, 600, i+1], fill=(color_value, color_value, 255, 255))
    
    # Draw title
    try:
        title_font = ImageFont.truetype("arial.ttf", 48)
        subtitle_font = ImageFont.truetype("arial.ttf", 18)
    except:
        title_font = ImageFont.load_default()
        subtitle_font = ImageFont.load_default()
    
    # Draw main title
    title = "ZPTBox"
    text_bbox = draw.textbbox((0, 0), title, font=title_font)
    text_width = text_bbox[2] - text_bbox[0]
    draw.text(((600 - text_width) // 2, 150), title, fill=(255, 255, 255), font=title_font)
    
    # Draw subtitle
    subtitle = "Scientific Computing Environment"
    text_bbox = draw.textbbox((0, 0), subtitle, font=subtitle_font)
    text_width = text_bbox[2] - text_bbox[0]
    draw.text(((600 - text_width) // 2, 220), subtitle, fill=(255, 255, 255), font=subtitle_font)
    
    # Draw version
    version = "Version 1.0.0"
    text_bbox = draw.textbbox((0, 0), version, font=subtitle_font)
    text_width = text_bbox[2] - text_bbox[0]
    draw.text(((600 - text_width) // 2, 250), version, fill=(200, 200, 200), font=subtitle_font)
    
    img.save('images/splash.png')
    print("Created: images/splash.png")

# Define icon specifications
icons = {
    # File operations
    'icons/new.png': ('N', (100, 200, 100)),
    'icons/open.png': ('O', (100, 150, 200)),
    'icons/save.png': ('S', (200, 150, 100)),
    'icons/save_as.png': ('SA', (200, 150, 150)),
    'icons/close.png': ('X', (200, 100, 100)),
    
    # Edit operations
    'icons/cut.png': ('✂', (200, 100, 100)),
    'icons/copy.png': ('C', (100, 150, 200)),
    'icons/paste.png': ('P', (150, 200, 100)),
    'icons/undo.png': ('↶', (150, 150, 200)),
    'icons/redo.png': ('↷', (150, 200, 150)),
    'icons/find.png': ('F', (200, 200, 100)),
    'icons/replace.png': ('R', (200, 150, 100)),
    
    # Run operations
    'icons/run.png': ('▶', (100, 200, 100)),
    'icons/run_selection.png': ('▷', (150, 200, 100)),
    'icons/stop.png': ('■', (200, 100, 100)),
    'icons/restart.png': ('↻', (200, 150, 100)),
    'icons/clear.png': ('CL', (150, 150, 150)),
    
    # View operations
    'icons/console.png': ('>', (100, 100, 200)),
    'icons/workspace.png': ('W', (150, 100, 200)),
    'icons/graphics.png': ('G', (100, 200, 200)),
    'icons/project.png': ('P', (200, 150, 100)),
    'icons/fullscreen.png': ('⛶', (100, 150, 150)),
    
    # Tools
    'icons/settings.png': ('⚙', (150, 150, 150)),
    'icons/plugins.png': ('🔌', (200, 150, 200)),
    'icons/package.png': ('📦', (150, 200, 150)),
    
    # Help
    'icons/help.png': ('?', (100, 150, 200)),
    'icons/about.png': ('i', (100, 200, 150)),
    'icons/update.png': ('↓', (200, 150, 100)),
    
    # Window operations
    'icons/cascade.png': ('⊞', (150, 150, 200)),
    'icons/tile.png': ('⊟', (150, 200, 150)),
    'icons/close_all.png': ('XX', (200, 100, 100)),
    
    # File types
    'icons/python.png': ('PY', (50, 150, 50)),
    'icons/matlab.png': ('M', (200, 100, 50)),
    'icons/text.png': ('T', (150, 150, 150)),
    'icons/folder.png': ('📁', (200, 200, 100)),
    'icons/folder_open.png': ('📂', (200, 200, 150)),
}

# Generate all icons
print("Generating icons...")
create_app_icon()
create_splash_screen()

for filename, (text, color) in icons.items():
    create_icon(text, color, filename)

print("\nAll icons generated successfully!")
print("\nNote: These are placeholder icons. You should replace them with")
print("professional icons for production use.")