#!/bin/bash
# Test script to verify GNOME integration for pyPhotoAlbum

set -e

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

print_header() {
    echo -e "\n${BLUE}========================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}========================================${NC}\n"
}

print_success() {
    echo -e "${GREEN}✓${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_info() {
    echo -e "${BLUE}ℹ${NC} $1"
}

# Check counter
PASSED=0
FAILED=0
WARNINGS=0

print_header "GNOME Integration Test for pyPhotoAlbum"

# Test 1: Check if desktop file exists
print_header "1. Desktop File Installation"

if [ -f ~/.local/share/applications/pyphotoalbum.desktop ]; then
    print_success "Desktop file found in user directory"
    DESKTOP_FILE=~/.local/share/applications/pyphotoalbum.desktop
    ((PASSED++))
elif [ -f /usr/share/applications/pyphotoalbum.desktop ]; then
    print_success "Desktop file found in system directory"
    DESKTOP_FILE=/usr/share/applications/pyphotoalbum.desktop
    ((PASSED++))
else
    print_error "Desktop file not found"
    print_info "Expected locations:"
    echo "  - ~/.local/share/applications/pyphotoalbum.desktop"
    echo "  - /usr/share/applications/pyphotoalbum.desktop"
    ((FAILED++))
    DESKTOP_FILE=""
fi

# Test 2: Validate desktop file
print_header "2. Desktop File Validation"

if [ -n "$DESKTOP_FILE" ]; then
    if command -v desktop-file-validate &> /dev/null; then
        if desktop-file-validate "$DESKTOP_FILE" 2>/dev/null; then
            print_success "Desktop file is valid"
            ((PASSED++))
        else
            print_warning "Desktop file has validation warnings"
            desktop-file-validate "$DESKTOP_FILE"
            ((WARNINGS++))
        fi
    else
        print_warning "desktop-file-validate not installed (install desktop-file-utils)"
        ((WARNINGS++))
    fi

    # Check StartupWMClass
    if grep -q "StartupWMClass=pyPhotoAlbum" "$DESKTOP_FILE"; then
        print_success "StartupWMClass is set correctly"
        ((PASSED++))
    else
        print_error "StartupWMClass not set correctly"
        ((FAILED++))
    fi

    # Check StartupNotify
    if grep -q "StartupNotify=true" "$DESKTOP_FILE"; then
        print_success "StartupNotify is enabled"
        ((PASSED++))
    else
        print_warning "StartupNotify not enabled (better taskbar feedback)"
        ((WARNINGS++))
    fi
fi

# Test 3: Check icon installation
print_header "3. Icon Installation"

ICON_FOUND=0
ICON_SIZES=(16 22 24 32 48 64 128 256 512)

for size in "${ICON_SIZES[@]}"; do
    USER_ICON=~/.local/share/icons/hicolor/${size}x${size}/apps/pyphotoalbum.png
    SYSTEM_ICON=/usr/share/icons/hicolor/${size}x${size}/apps/pyphotoalbum.png

    if [ -f "$USER_ICON" ]; then
        print_success "Found ${size}x${size} icon (user)"
        ((ICON_FOUND++))
    elif [ -f "$SYSTEM_ICON" ]; then
        print_success "Found ${size}x${size} icon (system)"
        ((ICON_FOUND++))
    fi
done

if [ $ICON_FOUND -eq 0 ]; then
    print_error "No icons found"
    ((FAILED++))
elif [ $ICON_FOUND -lt 5 ]; then
    print_warning "Only $ICON_FOUND icon size(s) found (recommended: multiple sizes)"
    print_info "Run './generate_icons.sh' to create more sizes"
    ((WARNINGS++))
    ((PASSED++))
else
    print_success "Multiple icon sizes installed ($ICON_FOUND sizes)"
    ((PASSED++))
fi

# Test 4: Check if application is in PATH
print_header "4. Application Executable"

if command -v pyphotoalbum &> /dev/null; then
    print_success "pyphotoalbum command found in PATH"
    print_info "Location: $(which pyphotoalbum)"
    ((PASSED++))
else
    print_error "pyphotoalbum command not found in PATH"
    print_info "Make sure ~/.local/bin is in your PATH"
    echo "  Add to ~/.bashrc: export PATH=\"\$HOME/.local/bin:\$PATH\""
    ((FAILED++))
fi

# Test 5: Check icon cache
print_header "5. Icon Cache Status"

if command -v gtk-update-icon-cache &> /dev/null; then
    print_success "gtk-update-icon-cache is available"
    ((PASSED++))

    # Check cache timestamp
    if [ -f ~/.local/share/icons/hicolor/icon-theme.cache ]; then
        print_success "User icon cache exists"
        CACHE_TIME=$(stat -c %Y ~/.local/share/icons/hicolor/icon-theme.cache 2>/dev/null || stat -f %m ~/.local/share/icons/hicolor/icon-theme.cache 2>/dev/null)
        print_info "Last updated: $(date -d @$CACHE_TIME 2>/dev/null || date -r $CACHE_TIME 2>/dev/null)"
        ((PASSED++))
    else
        print_warning "User icon cache not found (may need update)"
        print_info "Run: gtk-update-icon-cache ~/.local/share/icons/hicolor/"
        ((WARNINGS++))
    fi
else
    print_warning "gtk-update-icon-cache not found"
    print_info "Install gtk3 or gtk4 package"
    ((WARNINGS++))
fi

# Test 6: Check desktop database
print_header "6. Desktop Database"

if command -v update-desktop-database &> /dev/null; then
    print_success "update-desktop-database is available"
    ((PASSED++))
else
    print_warning "update-desktop-database not found"
    print_info "Install desktop-file-utils package"
    ((WARNINGS++))
fi

# Test 7: Check GNOME Shell
print_header "7. GNOME Environment"

if [ "$XDG_CURRENT_DESKTOP" = "GNOME" ]; then
    print_success "Running in GNOME desktop environment"
    ((PASSED++))
else
    print_info "Not running GNOME (detected: ${XDG_CURRENT_DESKTOP:-unknown})"
    print_info "This test is designed for GNOME but should work on other DEs"
fi

if [ -n "$WAYLAND_DISPLAY" ]; then
    print_info "Running Wayland session"
elif [ -n "$DISPLAY" ]; then
    print_info "Running X11 session"
fi

# Test 8: Test application launch
print_header "8. Application Launch Test"

if command -v pyphotoalbum &> /dev/null; then
    print_info "To test the application, run: pyphotoalbum"
    print_info "Check if:"
    echo "  1. Icon appears in taskbar"
    echo "  2. Window title matches application name"
    echo "  3. Alt+Tab shows correct icon and name"
    echo "  4. Application appears in GNOME Activities search"
else
    print_error "Cannot test - application not installed"
    ((FAILED++))
fi

# Summary
print_header "Test Summary"

TOTAL=$((PASSED + FAILED))
echo -e "${GREEN}Passed:${NC}   $PASSED"
echo -e "${RED}Failed:${NC}   $FAILED"
echo -e "${YELLOW}Warnings:${NC} $WARNINGS"
echo ""

if [ $FAILED -eq 0 ]; then
    if [ $WARNINGS -eq 0 ]; then
        echo -e "${GREEN}✓ All tests passed! GNOME integration is properly configured.${NC}"
    else
        echo -e "${YELLOW}⚠ Tests passed with warnings. Integration should work but can be improved.${NC}"
    fi
else
    echo -e "${RED}✗ Some tests failed. Please fix the issues above.${NC}"
    exit 1
fi

# Additional recommendations
print_header "Recommendations"

echo "For best GNOME integration:"
echo "  1. Generate multiple icon sizes:"
echo "     ./generate_icons.sh"
echo ""
echo "  2. After installation, log out and log back in"
echo "     or restart GNOME Shell (Alt+F2, type 'r', Enter)"
echo ""
echo "  3. If icon doesn't appear, clear icon cache:"
echo "     rm ~/.cache/icon-cache.kcache"
echo "     gtk-update-icon-cache -f ~/.local/share/icons/hicolor/"
echo ""
echo "  4. Search for 'pyPhotoAlbum' in GNOME Activities"
echo ""
echo "  5. Pin to favorites by right-clicking the icon in Activities"
echo ""
