#!/bin/bash
#
# MPV Manager Uninstaller for Linux
# This script removes MPV Manager from your Linux system.
#
# Usage: ./uninstall-linux.sh
#

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo "============================================================"
echo "             MPV Manager Uninstaller (Linux)"
echo "============================================================"
echo ""

# Get user home directory
HOME_DIR="$HOME"
MPV_MANAGER_DIR="$HOME_DIR/.config/mpv"
INSTALLER_BIN="$MPV_MANAGER_DIR/mpv-manager"
DESKTOP_FILE="$HOME_DIR/.local/share/applications/mpv-manager.desktop"
DESKTOP_SYMLINK="$HOME_DIR/Desktop/mpv-manager.desktop"
ICON_FILE="$HOME_DIR/.local/share/icons/hicolor/128x128/apps/mpv-manager.png"
CONFIG_FILE_PORTABLE="$HOME_DIR/mpv/portable_config/mpv-manager.json"
CONFIG_FILE_OLD_BIN="$HOME_DIR/mpv/mpv-manager"
CONFIG_FILE_XDG="$HOME_DIR/.config/mpv/mpv-manager.json"
TEMP_ICON_DIR="$HOME_DIR/.config/mpv/mpv-manager"

echo "This script will remove MPV Manager from your system."
echo ""
echo -e "${YELLOW}The following will be removed:${NC}"
echo "  - MPV Manager binary: $INSTALLER_BIN"
echo "  - Desktop entry: $DESKTOP_FILE"
echo "  - Desktop shortcut: $DESKTOP_SYMLINK"
echo "  - Icon: $ICON_FILE"
echo "  - Configuration files"
echo ""
echo -e "${BLUE}The following will be PRESERVED:${NC}"
echo "  - MPV installation (if installed via package manager/Flatpak/Snap)"
echo "  - MPV configuration: $HOME_DIR/.config/mpv/"
echo "  - Your media player settings"
echo ""

# Ask about config removal
echo -n "Remove MPV Manager configuration files? (y/N): "
read -r REMOVE_CONFIG
echo ""

if [[ ! "$REMOVE_CONFIG" =~ ^[Yy]$ ]]; then
    REMOVE_CONFIG="n"
fi

echo -n "Do you want to continue with the uninstall? (y/N): "
read -r CONFIRM
echo ""

if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
    echo ""
    echo "Uninstall cancelled."
    exit 0
fi

echo ""
echo "============================================================"
echo "                   Starting Uninstall"
echo "============================================================"
echo ""

# Track if anything was removed
REMOVED_SOMETHING=false

# 1. Remove MPV Manager binary
if [ -f "$INSTALLER_BIN" ]; then
    echo "Removing MPV Manager binary..."
    rm -f "$INSTALLER_BIN"
    if [ -f "$INSTALLER_BIN" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $INSTALLER_BIN"
        echo "        Some files may be in use. Try closing MPV Manager and running again."
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $INSTALLER_BIN"
        REMOVED_SOMETHING=true
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} MPV Manager binary not found"
fi

# 1b. Remove old binary location (backwards compatibility with pre-2026-02 versions)
if [ -f "$CONFIG_FILE_OLD_BIN" ]; then
    echo "Removing old MPV Manager binary..."
    rm -f "$CONFIG_FILE_OLD_BIN"
    if [ -f "$CONFIG_FILE_OLD_BIN" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $CONFIG_FILE_OLD_BIN"
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $CONFIG_FILE_OLD_BIN"
        REMOVED_SOMETHING=true
    fi
fi

# 2. Remove desktop entry
if [ -f "$DESKTOP_FILE" ]; then
    echo "Removing desktop entry..."
    rm -f "$DESKTOP_FILE"
    if [ -f "$DESKTOP_FILE" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $DESKTOP_FILE"
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $DESKTOP_FILE"
        REMOVED_SOMETHING=true
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} Desktop entry not found"
fi

# 3. Remove desktop symlink
if [ -L "$DESKTOP_SYMLINK" ] || [ -f "$DESKTOP_SYMLINK" ]; then
    echo "Removing desktop shortcut..."
    rm -f "$DESKTOP_SYMLINK"
    if [ -L "$DESKTOP_SYMLINK" ] || [ -f "$DESKTOP_SYMLINK" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $DESKTOP_SYMLINK"
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $DESKTOP_SYMLINK"
        REMOVED_SOMETHING=true
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} Desktop shortcut not found"
fi

# 4. Remove icon
if [ -f "$ICON_FILE" ]; then
    echo "Removing icon..."
    rm -f "$ICON_FILE"
    if [ -f "$ICON_FILE" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $ICON_FILE"
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $ICON_FILE"
        REMOVED_SOMETHING=true
        
        # Update icon cache if possible
        if command -v gtk-update-icon-cache &> /dev/null; then
            ICONS_DIR="$HOME_DIR/.local/share/icons/hicolor"
            gtk-update-icon-cache --quiet --force "$ICONS_DIR" 2>/dev/null || true
        fi
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} Icon not found"
fi

# 5. Remove temp icon directory (used during installation)
if [ -d "$TEMP_ICON_DIR" ]; then
    echo "Removing temporary icon directory..."
    rm -rf "$TEMP_ICON_DIR"
    if [ -d "$TEMP_ICON_DIR" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $TEMP_ICON_DIR"
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $TEMP_ICON_DIR"
        REMOVED_SOMETHING=true
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} Temporary icon directory not found"
fi

# 6. Remove config files (if user confirmed)
if [[ "$REMOVE_CONFIG" =~ ^[Yy]$ ]]; then
    # Remove portable config
    if [ -f "$CONFIG_FILE_PORTABLE" ]; then
        echo "Removing configuration file (portable)..."
        rm -f "$CONFIG_FILE_PORTABLE"
        if [ -f "$CONFIG_FILE_PORTABLE" ]; then
            echo -e "  ${RED}[WARN]${NC} Could not remove: $CONFIG_FILE_PORTABLE"
        else
            echo -e "  ${GREEN}[OK]${NC} Removed: $CONFIG_FILE_PORTABLE"
            REMOVED_SOMETHING=true
        fi
    fi
    
    # Remove XDG config
    if [ -f "$CONFIG_FILE_XDG" ]; then
        echo "Removing configuration file (XDG)..."
        rm -f "$CONFIG_FILE_XDG"
        if [ -f "$CONFIG_FILE_XDG" ]; then
            echo -e "  ${RED}[WARN]${NC} Could not remove: $CONFIG_FILE_XDG"
        else
            echo -e "  ${GREEN}[OK]${NC} Removed: $CONFIG_FILE_XDG"
            REMOVED_SOMETHING=true
        fi
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} Configuration files preserved (user choice)"
fi

# 7. Clean up - note: we don't remove ~/.config/mpv as it's the standard MPV config location
# and may contain user's mpv.conf and other settings
echo "Note: ~/.config/mpv directory preserved (standard MPV config location)"

# 8. Clean up old ~/mpv directory if empty (backwards compatibility)
OLD_MPV_DIR="$HOME_DIR/mpv"
if [ -d "$OLD_MPV_DIR" ]; then
    # Only remove if empty and doesn't contain mpv binary
    OLD_MPV_BINARY="$OLD_MPV_DIR/mpv"
    if [ ! -f "$OLD_MPV_BINARY" ] && [ -z "$(ls -A "$OLD_MPV_DIR" 2>/dev/null)" ]; then
        echo "Removing empty old MPV directory..."
        rmdir "$OLD_MPV_DIR" 2>/dev/null && echo -e "  ${GREEN}[OK]${NC} Removed: $OLD_MPV_DIR" || true
    fi
fi

echo ""
echo "============================================================"
echo "                   Uninstall Complete"
echo "============================================================"
echo ""

if [ "$REMOVED_SOMETHING" = true ]; then
    echo -e "${GREEN}MPV Manager has been removed from your system.${NC}"
else
    echo -e "${YELLOW}No MPV Manager files were found to remove.${NC}"
fi

echo ""
echo "Your MPV installation and settings have been preserved."
echo ""
echo "If you want to completely remove MPV, use your package manager:"
echo "  Ubuntu/Debian: sudo apt remove mpv celluloid"
echo "  Fedora/RHEL:   sudo dnf remove mpv celluloid"
echo "  Arch Linux:    sudo pacman -Rns mpv celluloid"
echo "  Flatpak:       flatpak uninstall io.mpv.Mpv io.github.celluloid_player.Celluloid"
echo "  Snap:          sudo snap remove mpv celluloid"
echo ""
echo "Thank you for using MPV Manager!"
echo ""
