#!/bin/bash

# Argument 1: Path to magick executable
MAGICK_BIN="$1"

# Fallback if not provided
if [ -z "$MAGICK_BIN" ]; then
    MAGICK_BIN="magick"
fi

# Assumes running from Project Root
SOURCE="assets/icon_source.png"

if [ ! -f "$SOURCE" ]; then
    echo "Error: Source image '$SOURCE' not found in $(pwd)"
    exit 1
fi

# Verify magick works
"$MAGICK_BIN" -version >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "Error: ImageMagick tool '$MAGICK_BIN' not found or not working."
    exit 1
fi

# --- macOS ---
# Keep macOS icons in assets/icons as they are linked explicitly
MACOS_OUT_DIR="assets/icons"
mkdir -p "$MACOS_OUT_DIR"
ICONSET="$MACOS_OUT_DIR/icon.iconset"
mkdir -p "$ICONSET"

echo "[ICONS] Generating macOS iconset (Pixel Perfect)..."
"$MAGICK_BIN" "$SOURCE" -sample 16x16      "$ICONSET/icon_16x16.png"
"$MAGICK_BIN" "$SOURCE" -sample 32x32      "$ICONSET/icon_16x16@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 32x32      "$ICONSET/icon_32x32.png"
"$MAGICK_BIN" "$SOURCE" -sample 64x64      "$ICONSET/icon_32x32@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 128x128    "$ICONSET/icon_128x128.png"
"$MAGICK_BIN" "$SOURCE" -sample 256x256    "$ICONSET/icon_128x128@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 256x256    "$ICONSET/icon_256x256.png"
"$MAGICK_BIN" "$SOURCE" -sample 512x512    "$ICONSET/icon_256x256@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 512x512    "$ICONSET/icon_512x512.png"
"$MAGICK_BIN" "$SOURCE" -sample 1024x1024  "$ICONSET/icon_512x512@2x.png"

iconutil -c icns "$ICONSET" -o "$MACOS_OUT_DIR/app_icon.icns"
rm -rf "$ICONSET"

# --- Windows ---
echo "[ICONS] Generating Windows .ico (Pixel Perfect)..."
# Updated to match the high-quality logic from the Windows Batch file
# (Removed -alpha off and -colors 256 which would ruin transparency)
"$MAGICK_BIN" "$SOURCE" \
    -background none -alpha on \
    \( -clone 0 -sample 256x256 \) \
    \( -clone 0 -sample 128x128 \) \
    \( -clone 0 -sample 64x64 \) \
    \( -clone 0 -sample 48x48 \) \
    \( -clone 0 -sample 32x32 \) \
    \( -clone 0 -sample 16x16 \) \
    -delete 0 "$MACOS_OUT_DIR/app_icon.ico"

# --- Android ---
# Output directly to android/res so QT_ANDROID_PACKAGE_SOURCE_DIR picks it up
echo "[ICONS] Generating Android mipmaps (Pixel Perfect)..."
ANDROID_DIR="android/res"

mkdir -p "$ANDROID_DIR/mipmap-mdpi"
"$MAGICK_BIN" "$SOURCE" -sample 48x48 "$ANDROID_DIR/mipmap-mdpi/ic_launcher.png"

mkdir -p "$ANDROID_DIR/mipmap-hdpi"
"$MAGICK_BIN" "$SOURCE" -sample 72x72 "$ANDROID_DIR/mipmap-hdpi/ic_launcher.png"

mkdir -p "$ANDROID_DIR/mipmap-xhdpi"
"$MAGICK_BIN" "$SOURCE" -sample 96x96 "$ANDROID_DIR/mipmap-xhdpi/ic_launcher.png"

mkdir -p "$ANDROID_DIR/mipmap-xxhdpi"
"$MAGICK_BIN" "$SOURCE" -sample 144x144 "$ANDROID_DIR/mipmap-xxhdpi/ic_launcher.png"

mkdir -p "$ANDROID_DIR/mipmap-xxxhdpi"
"$MAGICK_BIN" "$SOURCE" -sample 192x192 "$ANDROID_DIR/mipmap-xxxhdpi/ic_launcher.png"

# --- iOS ---
# Output directly to ios/Assets.xcassets
echo "[ICONS] Generating iOS AppIcon (Pixel Perfect)..."
XCASSETS_DIR="ios/Assets.xcassets"
IOS_DIR="$XCASSETS_DIR/AppIcon.appiconset"
mkdir -p "$IOS_DIR"

cat > "$XCASSETS_DIR/Contents.json" <<EOF
{
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}
EOF

"$MAGICK_BIN" "$SOURCE" -sample 20x20    "$IOS_DIR/Icon-20.png"
"$MAGICK_BIN" "$SOURCE" -sample 40x40    "$IOS_DIR/Icon-20@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 60x60    "$IOS_DIR/Icon-20@3x.png"
"$MAGICK_BIN" "$SOURCE" -sample 29x29    "$IOS_DIR/Icon-29.png"
"$MAGICK_BIN" "$SOURCE" -sample 58x58    "$IOS_DIR/Icon-29@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 87x87    "$IOS_DIR/Icon-29@3x.png"
"$MAGICK_BIN" "$SOURCE" -sample 40x40    "$IOS_DIR/Icon-40.png"
"$MAGICK_BIN" "$SOURCE" -sample 80x80    "$IOS_DIR/Icon-40@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 120x120  "$IOS_DIR/Icon-40@3x.png"
"$MAGICK_BIN" "$SOURCE" -sample 120x120  "$IOS_DIR/Icon-60@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 180x180  "$IOS_DIR/Icon-60@3x.png"
"$MAGICK_BIN" "$SOURCE" -sample 76x76    "$IOS_DIR/Icon-76.png"
"$MAGICK_BIN" "$SOURCE" -sample 152x152  "$IOS_DIR/Icon-76@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 167x167  "$IOS_DIR/Icon-83.5@2x.png"
"$MAGICK_BIN" "$SOURCE" -sample 1024x1024 "$IOS_DIR/Icon-1024.png"

cat > "$IOS_DIR/Contents.json" <<EOF
{
  "images" : [
    { "size" : "20x20", "idiom" : "iphone", "filename" : "Icon-20@2x.png", "scale" : "2x" },
    { "size" : "20x20", "idiom" : "iphone", "filename" : "Icon-20@3x.png", "scale" : "3x" },
    { "size" : "29x29", "idiom" : "iphone", "filename" : "Icon-29.png", "scale" : "1x" },
    { "size" : "29x29", "idiom" : "iphone", "filename" : "Icon-29@2x.png", "scale" : "2x" },
    { "size" : "29x29", "idiom" : "iphone", "filename" : "Icon-29@3x.png", "scale" : "3x" },
    { "size" : "40x40", "idiom" : "iphone", "filename" : "Icon-40@2x.png", "scale" : "2x" },
    { "size" : "40x40", "idiom" : "iphone", "filename" : "Icon-40@3x.png", "scale" : "3x" },
    { "size" : "60x60", "idiom" : "iphone", "filename" : "Icon-60@2x.png", "scale" : "2x" },
    { "size" : "60x60", "idiom" : "iphone", "filename" : "Icon-60@3x.png", "scale" : "3x" },
    { "size" : "20x20", "idiom" : "ipad", "filename" : "Icon-20.png", "scale" : "1x" },
    { "size" : "20x20", "idiom" : "ipad", "filename" : "Icon-20@2x.png", "scale" : "2x" },
    { "size" : "29x29", "idiom" : "ipad", "filename" : "Icon-29.png", "scale" : "1x" },
    { "size" : "29x29", "idiom" : "ipad", "filename" : "Icon-29@2x.png", "scale" : "2x" },
    { "size" : "40x40", "idiom" : "ipad", "filename" : "Icon-40.png", "scale" : "1x" },
    { "size" : "40x40", "idiom" : "ipad", "filename" : "Icon-40@2x.png", "scale" : "2x" },
    { "size" : "76x76", "idiom" : "ipad", "filename" : "Icon-76.png", "scale" : "1x" },
    { "size" : "76x76", "idiom" : "ipad", "filename" : "Icon-76@2x.png", "scale" : "2x" },
    { "size" : "83.5x83.5", "idiom" : "ipad", "filename" : "Icon-83.5@2x.png", "scale" : "2x" },
    { "size" : "1024x1024", "idiom" : "ios-marketing", "filename" : "Icon-1024.png", "scale" : "1x" }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}
EOF

echo "[SUCCESS] All platform icons generated."