#!/usr/bin/env python3
"""
Generate Android app icons from logo.jpg
Requires: pip install Pillow
"""

import os
from pathlib import Path
try:
    from PIL import Image
except ImportError:
    print("Error: Pillow not installed. Install it with: pip install Pillow")
    exit(1)

# Paths
logo_path = Path("assets/images/logo.jpg")
android_res_path = Path("android/app/src/main/res")

# Icon sizes for different densities
sizes = {
    "mipmap-mdpi": 48,
    "mipmap-hdpi": 72,
    "mipmap-xhdpi": 96,
    "mipmap-xxhdpi": 144,
    "mipmap-xxxhdpi": 192,
}

def generate_icon(logo_path, output_path, size):
    """Generate a square icon from the logo"""
    try:
        # Open and resize logo
        img = Image.open(logo_path)
        
        # Convert to RGBA if needed
        if img.mode != 'RGBA':
            img = img.convert('RGBA')
        
        # Create a square image with transparent background
        square_img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
        
        # Calculate scaling to fit within square while maintaining aspect ratio
        img.thumbnail((size, size), Image.Resampling.LANCZOS)
        
        # Center the image
        x_offset = (size - img.width) // 2
        y_offset = (size - img.height) // 2
        
        square_img.paste(img, (x_offset, y_offset), img if img.mode == 'RGBA' else None)
        
        # Save as PNG
        output_path.parent.mkdir(parents=True, exist_ok=True)
        square_img.save(output_path, 'PNG')
        return True
    except Exception as e:
        print(f"Error generating {output_path}: {e}")
        return False

def main():
    # Check if logo exists
    if not logo_path.exists():
        print(f"Error: Logo not found at {logo_path}")
        print(f"Please place your logo.jpg in {logo_path.parent}")
        return 1
    
    print(f"Generating app icons from {logo_path}...")
    print(f"Output directory: {android_res_path}\n")
    
    success_count = 0
    for folder, size in sizes.items():
        output_dir = android_res_path / folder
        output_path = output_dir / "ic_launcher.png"
        
        print(f"Generating {output_path} ({size}x{size})...", end=" ")
        
        if generate_icon(logo_path, output_path, size):
            print("OK")
            success_count += 1
        else:
            print("FAILED")
    
    print(f"\nSuccessfully generated {success_count}/{len(sizes)} icons!")
    print(f"Icons saved in: {android_res_path}")
    return 0

if __name__ == "__main__":
    exit(main())

