#!/usr/bin/env python3
"""
Fix App Icon - Add padding to prevent cropping on Android
"""

from PIL import Image, ImageDraw
import os

def create_icon_with_padding(input_path, output_path, padding_percent=15):
    """
    Create icon with padding to fit Android adaptive icon safe zone
    
    Android adaptive icons use a circular mask, so we need to ensure
    the important content fits within a circle of ~80% of the icon size
    """
    print(f"📷 Loading icon: {input_path}")
    
    # Open original image
    img = Image.open(input_path).convert('RGBA')
    original_size = img.size[0]
    
    print(f"   Original size: {original_size}x{original_size}")
    
    # Calculate new size with padding
    padding = int(original_size * (padding_percent / 100))
    new_size = original_size + (padding * 2)
    
    print(f"   Adding padding: {padding}px on each side")
    print(f"   New size: {new_size}x{new_size}")
    
    # Create new image with transparent background
    new_img = Image.new('RGBA', (new_size, new_size), (0, 0, 0, 0))
    
    # Paste original image in center
    new_img.paste(img, (padding, padding), img)
    
    # Save result
    new_img.save(output_path, 'PNG')
    print(f"✅ Icon saved: {output_path}")
    
    return output_path

def create_icon_with_background(input_path, output_path, bg_color='#1976D2', padding_percent=15):
    """
    Create icon with colored background (better for adaptive icons)
    """
    print(f"📷 Loading icon: {input_path}")
    
    # Open original image
    img = Image.open(input_path).convert('RGBA')
    original_size = img.size[0]
    
    print(f"   Original size: {original_size}x{original_size}")
    
    # Calculate new size with padding
    padding = int(original_size * (padding_percent / 100))
    new_size = original_size + (padding * 2)
    
    print(f"   Adding padding: {padding}px on each side")
    print(f"   New size: {new_size}x{new_size}")
    print(f"   Background color: {bg_color}")
    
    # Create new image with colored background
    new_img = Image.new('RGBA', (new_size, new_size), bg_color)
    
    # Paste original image in center
    new_img.paste(img, (padding, padding), img)
    
    # Save result
    new_img.save(output_path, 'PNG')
    print(f"✅ Icon saved: {output_path}")
    
    return output_path

if __name__ == '__main__':
    import sys
    
    print("🔧 Android Adaptive Icon Fixer")
    print("=" * 50)
    print()
    
    # Paths
    input_icon = 'assets/images/app_icon.png'
    
    if not os.path.exists(input_icon):
        print(f"❌ Error: Icon not found at {input_icon}")
        sys.exit(1)
    
    # Backup original
    backup_path = 'assets/images/app_icon_backup.png'
    if not os.path.exists(backup_path):
        Image.open(input_icon).save(backup_path)
        print(f"💾 Backup saved: {backup_path}")
        print()
    
    print("Choose icon style:")
    print("1. Transparent background with padding (default)")
    print("2. Blue background with padding (recommended for Android)")
    print()
    
    choice = input("Enter choice (1 or 2, default=2): ").strip() or "2"
    
    print()
    
    if choice == "1":
        # Transparent with padding
        create_icon_with_padding(
            input_icon, 
            input_icon,
            padding_percent=20
        )
    else:
        # Blue background with padding
        create_icon_with_background(
            input_icon,
            input_icon,
            bg_color='#1976D2',  # Material Blue
            padding_percent=15
        )
    
    print()
    print("=" * 50)
    print("✅ Icon fixed successfully!")
    print()
    print("Next steps:")
    print("1. Run: flutter pub run flutter_launcher_icons")
    print("2. Build APK: flutter build apk --release")
    print("3. Check the new icon on your device")
    print()
    print("If icon still looks wrong, try running this script again")
    print("and choose the other option.")
