"""Generate 10 color variants of the MixBoard icon for iOS alternate icons."""
from PIL import Image, ImageEnhance
import numpy as np
import os

SRC = "/Users/vdrobkov/Misc/Documents/Copilot/MixBoard/Assets.xcassets/AppIcon.appiconset/icon_1024.png"
OUT_DIR = "/Users/vdrobkov/Misc/Documents/Copilot/Mixboard iOS/Sources/Resources/Assets.xcassets"

# Load source icon
src = Image.open(SRC).convert("RGBA")
pixels = np.array(src, dtype=np.float32)

# The green accent color in the original is approximately (80, 190, 60) 
# We'll shift the hue of green pixels to create variants

# 10 colors across the spectrum
COLORS = {
    "Green":   (0.35, 0.85, 0.25),   # original
    "Lime":    (0.55, 0.95, 0.15),
    "Cyan":    (0.15, 0.85, 0.85),
    "Blue":    (0.25, 0.45, 0.95),
    "Purple":  (0.6, 0.3, 0.9),
    "Pink":    (0.95, 0.3, 0.6),
    "Red":     (0.95, 0.25, 0.25),
    "Orange":  (0.95, 0.55, 0.15),
    "Gold":    (0.95, 0.8, 0.15),
    "White":   (0.9, 0.9, 0.92),
}

def recolor_icon(src_pixels, target_rgb):
    """Replace green-ish pixels with the target color, keeping brightness."""
    result = src_pixels.copy()
    r, g, b, a = result[:,:,0], result[:,:,1], result[:,:,2], result[:,:,3]
    
    # Detect green-dominant pixels (the accent color)
    # Green channel is significantly higher than red and blue
    green_mask = (g > r * 1.2) & (g > b * 1.2) & (g > 80)
    
    # Also include bright green pixels
    bright_green = (g > 100) & (g > r) & (g > b)
    mask = green_mask | bright_green
    
    # Calculate brightness of original green pixels
    brightness = g[mask] / 255.0
    
    # Apply new color with same brightness
    tr, tg, tb = target_rgb
    result[:,:,0][mask] = np.clip(brightness * tr * 255, 0, 255)
    result[:,:,1][mask] = np.clip(brightness * tg * 255, 0, 255)
    result[:,:,2][mask] = np.clip(brightness * tb * 255, 0, 255)
    
    return result

for name, color in COLORS.items():
    recolored = recolor_icon(pixels, color)
    img = Image.fromarray(recolored.astype(np.uint8), "RGBA")
    
    # Save as alternate icon
    icon_dir = os.path.join(OUT_DIR, f"AppIcon-{name}.appiconset")
    os.makedirs(icon_dir, exist_ok=True)
    img.save(os.path.join(icon_dir, "icon_1024.png"), "PNG")
    
    # Write Contents.json
    with open(os.path.join(icon_dir, "Contents.json"), "w") as f:
        f.write('''{
  "images" : [
    { "filename" : "icon_1024.png", "idiom" : "universal", "platform" : "ios", "size" : "1024x1024" }
  ],
  "info" : {
    "author" : "xcode",
    "version" : 1
  }
}''')
    
    print(f"Generated: {name} → {icon_dir}")

print(f"\nDone! {len(COLORS)} icon variants generated.")
