#!/bin/bash
#
# MPV Manager Uninstaller for macOS
# This script removes MPV Manager from your macOS system.
#
# Usage: ./uninstall-macos.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 (macOS)"
echo "============================================================"
echo ""

# Get user home directory
HOME_DIR="$HOME"
APP_BUNDLE="/Applications/MPV Manager.app"
CONFIG_FILE="$HOME_DIR/.config/mpv/mpv-manager.json"
CONFIG_FILE_PORTABLE="$HOME_DIR/mpv/portable_config/mpv-manager.json"
TEMP_ICON_DIR="$HOME_DIR/.config/mpv/mpv-manager"
MPV_MANAGER_BINARY="$HOME_DIR/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 app bundle: $APP_BUNDLE"
if [ -f "$MPV_MANAGER_BINARY" ]; then
    echo "  - MPV Manager binary: $MPV_MANAGER_BINARY"
fi
echo "  - Temporary icon files (if any)"
echo "  - Configuration files (optional)"
echo ""
echo -e "${BLUE}The following will be PRESERVED:${NC}"
echo "  - MPV installation: /Applications/MPV.app (if installed)"
echo "  - IINA installation: /Applications/IINA.app (if installed)"
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 app bundle
if [ -d "$APP_BUNDLE" ]; then
    echo "Removing MPV Manager app bundle..."
    rm -rf "$APP_BUNDLE"
    if [ -d "$APP_BUNDLE" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $APP_BUNDLE"
        echo "        The app may be in use. Try closing MPV Manager and running again."
        echo "        You may need to drag it to the Trash manually."
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $APP_BUNDLE"
        REMOVED_SOMETHING=true
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} MPV Manager app bundle not found"
fi

# 2. Remove MPV Manager binary (standalone installation)
if [ -f "$MPV_MANAGER_BINARY" ]; then
    echo "Removing MPV Manager binary..."
    rm -f "$MPV_MANAGER_BINARY"
    if [ -f "$MPV_MANAGER_BINARY" ]; then
        echo -e "  ${RED}[WARN]${NC} Could not remove: $MPV_MANAGER_BINARY"
    else
        echo -e "  ${GREEN}[OK]${NC} Removed: $MPV_MANAGER_BINARY"
        REMOVED_SOMETHING=true
    fi
else
    # Only show skip if app bundle was also not found
    if [ ! -d "$APP_BUNDLE" ]; then
        echo -e "  ${BLUE}[SKIP]${NC} MPV Manager binary not found"
    fi
fi

# 3. 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

# 4. Remove config files (if user confirmed)
if [[ "$REMOVE_CONFIG" =~ ^[Yy]$ ]]; then
    # Remove XDG config
    if [ -f "$CONFIG_FILE" ]; then
        echo "Removing configuration file (XDG)..."
        rm -f "$CONFIG_FILE"
        if [ -f "$CONFIG_FILE" ]; then
            echo -e "  ${RED}[WARN]${NC} Could not remove: $CONFIG_FILE"
        else
            echo -e "  ${GREEN}[OK]${NC} Removed: $CONFIG_FILE"
            REMOVED_SOMETHING=true
        fi
    fi
    
    # 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 empty config directories if they exist
    MPV_CONFIG_DIR="$HOME_DIR/.config/mpv"
    if [ -d "$MPV_CONFIG_DIR" ] && [ -z "$(ls -A "$MPV_CONFIG_DIR" 2>/dev/null)" ]; then
        echo "Removing empty config directory..."
        rmdir "$MPV_CONFIG_DIR" 2>/dev/null && echo -e "  ${GREEN}[OK]${NC} Removed: $MPV_CONFIG_DIR" || true
    fi
else
    echo -e "  ${BLUE}[SKIP]${NC} Configuration files preserved (user choice)"
fi

# 5. Clean up empty directories
MPV_DIR="$HOME_DIR/mpv"
if [ -d "$MPV_DIR" ]; then
    # Only remove if empty
    if [ -z "$(ls -A "$MPV_DIR" 2>/dev/null)" ]; then
        echo "Removing empty MPV directory..."
        rmdir "$MPV_DIR" 2>/dev/null && echo -e "  ${GREEN}[OK]${NC} Removed: $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 at:"
echo "  /Applications/MPV.app"
echo "  /Applications/IINA.app"
echo "  $HOME_DIR/.config/mpv/"
echo ""
echo "If you want to completely remove MPV or IINA, drag them to the Trash"
echo "from your Applications folder."
echo ""
echo "Thank you for using MPV Manager!"
echo ""
