#!/bin/bash

#
#
#

set -e

ICONS_DIR="assets/icons"
mkdir -p "$ICONS_DIR"

# Check if Papirus is installed
if [ -d "/usr/share/icons/Papirus" ]; then
    echo "Using installed Papirus icons..."
    PAPIRUS="/usr/share/icons/Papirus"
else
    echo "Papirus not found. Installing..."
    # Fedora
    if command -v dnf &> /dev/null; then
        sudo dnf install -y papirus-icon-theme
    # Debian/Ubuntu
    elif command -v apt &> /dev/null; then
        sudo apt install -y papirus-icon-theme
    # Arch
    elif command -v pacman &> /dev/null; then
        sudo pacman -S papirus-icon-theme
    else
        echo "Please install papirus-icon-theme manually"
        exit 1
    fi
    PAPIRUS="/usr/share/icons/Papirus"
fi

# Check for rsvg-convert
if ! command -v rsvg-convert &> /dev/null; then
    echo "Installing librsvg2-tools for SVG conversion..."
    if command -v dnf &> /dev/null; then
        sudo dnf install -y librsvg2-tools
    elif command -v apt &> /dev/null; then
        sudo apt install -y librsvg2-bin
    elif command -v pacman &> /dev/null; then
        sudo pacman -S librsvg
    fi
fi

echo "Converting icons to PNG..."

# Function to convert SVG to PNG
convert_icon() {
    local src="$1"
    local dst="$2"
    local size="${3:-24}"

    if [ -f "$src" ]; then
        rsvg-convert -w "$size" -h "$size" "$src" -o "$dst"
        echo "  ✓ $dst"
    else
        echo "  ✗ Not found: $src"
    fi
}

# Battery icons (24x24)
echo "Battery icons..."
convert_icon "$PAPIRUS/24x24/panel/battery-100.svg" "$ICONS_DIR/battery-full.png" 24
convert_icon "$PAPIRUS/24x24/panel/battery-080.svg" "$ICONS_DIR/battery-good.png" 24
convert_icon "$PAPIRUS/24x24/panel/battery-040.svg" "$ICONS_DIR/battery-low.png" 24
convert_icon "$PAPIRUS/24x24/panel/battery-010.svg" "$ICONS_DIR/battery-empty.png" 24
convert_icon "$PAPIRUS/24x24/panel/battery-100-charging.svg" "$ICONS_DIR/battery-charging.png" 24

# WiFi icons
echo "WiFi icons..."
convert_icon "$PAPIRUS/24x24/panel/network-wireless-signal-excellent-symbolic.svg" "$ICONS_DIR/wifi-4.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-wireless-signal-good-symbolic.svg" "$ICONS_DIR/wifi-3.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-wireless-signal-ok-symbolic.svg" "$ICONS_DIR/wifi-2.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-wireless-signal-weak-symbolic.svg" "$ICONS_DIR/wifi-1.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-wireless-offline-symbolic.svg" "$ICONS_DIR/wifi-off.png" 24

# Bluetooth icons
echo "Bluetooth icons..."
convert_icon "$PAPIRUS/24x24/panel/bluetooth-active-symbolic.svg" "$ICONS_DIR/bluetooth-on.png" 24
convert_icon "$PAPIRUS/24x24/panel/bluetooth-disabled-symbolic.svg" "$ICONS_DIR/bluetooth-off.png" 24
convert_icon "$PAPIRUS/24x24/panel/bluetooth-paired-symbolic.svg" "$ICONS_DIR/bluetooth-connected.png" 24

# Cellular icons
echo "Cellular icons..."
convert_icon "$PAPIRUS/24x24/panel/network-cellular-signal-excellent-symbolic.svg" "$ICONS_DIR/cellular-4.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-cellular-signal-good-symbolic.svg" "$ICONS_DIR/cellular-3.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-cellular-signal-ok-symbolic.svg" "$ICONS_DIR/cellular-2.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-cellular-signal-weak-symbolic.svg" "$ICONS_DIR/cellular-1.png" 24
convert_icon "$PAPIRUS/24x24/panel/network-cellular-offline-symbolic.svg" "$ICONS_DIR/cellular-off.png" 24

# UI icons
echo "UI icons..."
convert_icon "$PAPIRUS/24x24/panel/notification-symbolic.svg" "$ICONS_DIR/notification.png" 24
convert_icon "$PAPIRUS/24x24/apps/preferences-system.svg" "$ICONS_DIR/settings.png" 24
convert_icon "$PAPIRUS/24x24/actions/system-search-symbolic.svg" "$ICONS_DIR/search.png" 24
convert_icon "$PAPIRUS/48x48/apps/application-x-executable.svg" "$ICONS_DIR/app-default.png" 48

# Quick settings icons
echo "Quick settings icons..."
convert_icon "$PAPIRUS/24x24/panel/display-brightness-symbolic.svg" "$ICONS_DIR/brightness.png" 24
convert_icon "$PAPIRUS/24x24/panel/audio-volume-high-symbolic.svg" "$ICONS_DIR/volume.png" 24
convert_icon "$PAPIRUS/24x24/panel/audio-volume-muted-symbolic.svg" "$ICONS_DIR/volume-mute.png" 24
convert_icon "$PAPIRUS/24x24/panel/airplane-mode-symbolic.svg" "$ICONS_DIR/airplane.png" 24
convert_icon "$PAPIRUS/24x24/panel/rotation-locked-symbolic.svg" "$ICONS_DIR/rotation.png" 24

# Background (use a solid color PNG as placeholder)
echo "Creating background..."
if command -v convert &> /dev/null; then
    convert -size 1080x1920 xc:'#0a0a14' "$ICONS_DIR/../background.png"
    echo "  ✓ background.png"
else
    echo "  ⚠ ImageMagick not installed, skipping background"
fi

echo ""
echo "Done! Icons saved to $ICONS_DIR/"
ls -la "$ICONS_DIR/"
