#!/bin/bash
# Icon generation script for Tauri desktop app
# Generates all required icon sizes from source

set -e

ICONS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SRC_DIR="$ICONS_DIR/icons/src"
OUT_DIR="$ICONS_DIR"

echo "Generating FrenoCorp desktop app icons..."

# Check if ImageMagick is available
if ! command -v convert &> /dev/null; then
    echo "Warning: ImageMagick 'convert' not found. Install with:"
    echo "  macOS: brew install imagemagick"
    echo "  Ubuntu: sudo apt-get install imagemagick"
    echo "  Windows: choco install imagemagick"
    exit 1
fi

# Generate PNG icons at required sizes
echo "Generating PNG icons..."
for size in 32 128; do
    echo "  Creating ${size}x${size}.png"
    convert "$SRC_DIR/app-icon.svg" -resize "${size}x${size}" "$OUT_DIR/${size}x${size}.png"
done

# Generate 128x128@2x (256x256)
echo "  Creating 128x128@2x.png (256x256)"
convert "$SRC_DIR/app-icon.svg" -resize "256x256" "$OUT_DIR/128x128@2x.png"

# Generate square icon (512x512 for general use)
echo "  Creating 512x512.png"
convert "$SRC_DIR/app-icon.svg" -resize "512x512" "$OUT_DIR/512x512.png"

# Generate macOS icon (icns)
echo "Generating macOS icon (icon.icns)..."
mkdir -p "$ICONS_DIR/icon.iconset"
convert "$SRC_DIR/app-icon.svg" -resize "16x16" "$ICONS_DIR/icon.iconset/icon_16x16.png"
convert "$SRC_DIR/app-icon.svg" -resize "32x32" "$ICONS_DIR/icon.iconset/icon_16x16@2x.png"
convert "$SRC_DIR/app-icon.svg" -resize "32x32" "$ICONS_DIR/icon.iconset/icon_32x32.png"
convert "$SRC_DIR/app-icon.svg" -resize "64x64" "$ICONS_DIR/icon.iconset/icon_32x32@2x.png"
convert "$SRC_DIR/app-icon.svg" -resize "128x128" "$ICONS_DIR/icon.iconset/icon_128x128.png"
convert "$SRC_DIR/app-icon.svg" -resize "256x256" "$ICONS_DIR/icon.iconset/icon_128x128@2x.png"
convert "$SRC_DIR/app-icon.svg" -resize "256x256" "$ICONS_DIR/icon.iconset/icon_256x256.png"
convert "$SRC_DIR/app-icon.svg" -resize "512x512" "$ICONS_DIR/icon.iconset/icon_256x256@2x.png"
convert "$SRC_DIR/app-icon.svg" -resize "512x512" "$ICONS_DIR/icon.iconset/icon_512x512.png"
convert "$SRC_DIR/app-icon.svg" -resize "1024x1024" "$ICONS_DIR/icon.iconset/icon_512x512@2x.png"

# Create .icns file (macOS requires iconutil on macOS, fallback to png2icns)
if command -v iconutil &> /dev/null; then
    iconutil -c icns "$ICONS_DIR/icon.iconset" -o "$OUT_DIR/icon.icns"
else
    echo "  Note: iconutil not found (not on macOS). Using png fallback."
fi

# Clean up temporary iconset
rm -rf "$ICONS_DIR/icon.iconset"

# Generate Windows icon (ico) - contains multiple sizes
echo "Generating Windows icon (icon.ico)..."
mkdir -p "$ICONS_DIR/temp_ico"
convert "$SRC_DIR/app-icon.svg" -resize "16x16" "$ICONS_DIR/temp_ico/16.png"
convert "$SRC_DIR/app-icon.svg" -resize "32x32" "$ICONS_DIR/temp_ico/32.png"
convert "$SRC_DIR/app-icon.svg" -resize "48x48" "$ICONS_DIR/temp_ico/48.png"
convert "$SRC_DIR/app-icon.svg" -resize "64x64" "$ICONS_DIR/temp_ico/64.png"
convert "$SRC_DIR/app-icon.svg" -resize "128x128" "$ICONS_DIR/temp_ico/128.png"
convert "$SRC_DIR/app-icon.svg" -resize "256x256" "$ICONS_DIR/temp_ico/256.png"
convert "$ICONS_DIR/temp_ico/*.png" -define icon:auto-resize=256,128,64,48,32,16 "$OUT_DIR/icon.ico"
rm -rf "$ICONS_DIR/temp_ico"

# Generate tray icon
echo "Generating tray icon..."
convert "$SRC_DIR/app-icon.svg" -resize "32x32" -background none "$OUT_DIR/tray-icon.png"

echo ""
echo "✅ Icon generation complete!"
echo ""
echo "Generated files:"
ls -1 "$OUT_DIR"/*.png "$OUT_DIR"/*.ico "$OUT_DIR"/*.icns 2>/dev/null | xargs -I {} basename {}

echo ""
echo "Next steps:"
echo "  1. Review generated icons"
echo "  2. Run: npm run tauri:build"
echo "  3. Test on target platforms"
