#!/bin/bash

# Ready or Not Comprehensive Compatibility Test
# Tests various Proton versions and Wine configurations

set -e

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

test_proton_versions() {
    log_info "=== Testing Different Proton Versions ==="
    echo
    echo "Ready or Not is known to have compatibility issues with newer Proton versions."
    echo "Let's test with older, more stable versions:"
    echo
    echo "Test Order (most likely to work first):"
    echo "1. Proton 8.0-5 (most stable for older games)"
    echo "2. Proton 7.0-6 (even older, better compatibility)"  
    echo "3. Proton 6.3-8 (legacy support)"
    echo "4. GE-Proton8-32 (community version with patches)"
    echo
    echo "For each version:"
    echo "- Change compatibility tool in Steam"
    echo "- Keep launch options: PROTON_USE_WINED3D=1 %command% -noeac -dx11"
    echo "- Test game launch"
    echo "- Note how long it runs before crashing"
    echo
}

test_wine_dependencies() {
    log_info "=== Testing Windows Dependencies ==="
    echo
    echo "Ready or Not may need specific Windows libraries:"
    echo
    echo "Common missing dependencies for UE4 games:"
    echo "- Visual C++ Redistributables (2015, 2017, 2019)"
    echo "- DirectX End-User Runtime"
    echo "- .NET Framework 4.8"
    echo "- Windows Media Format Runtime"
    echo
    echo "To test these:"
    echo "1. Set compatibility to 'Proton 8.0-5'"
    echo "2. Launch options: PROTON_USE_WINED3D=1 %command% -noeac -dx11"
    echo "3. Before launching game, run:"
    echo "   protontricks 1144200 --no-term"
    echo "4. Install vcrun2019, d3dx9, dotnet48"
    echo "5. Then test game"
    echo
}

test_protondb_fixes() {
    log_info "=== ProtonDB Community Fixes ==="
    echo
    echo "Let's check what the community has found works for Ready or Not:"
    echo
    echo "Common working configurations from ProtonDB:"
    echo
    echo "Configuration 1 (Most Common):"
    echo "- Proton Version: GE-Proton7-55 or Proton 7.0-6"
    echo "- Launch Options: PROTON_USE_WINED3D=1 %command% -noeac"
    echo "- Additional: Install vcrun2019 via protontricks"
    echo
    echo "Configuration 2 (Alternative):"
    echo "- Proton Version: Proton 8.0-5"
    echo "- Launch Options: PROTON_NO_ESYNC=1 PROTON_NO_FSYNC=1 %command% -noeac -dx11"
    echo "- Additional: Disable fullscreen optimizations"
    echo
    echo "Configuration 3 (Fallback):"
    echo "- Use Lutris instead of Steam"
    echo "- Wine-GE-Proton8-26"
    echo "- Manual DXVK installation"
    echo
}

test_system_specific_fixes() {
    log_info "=== System-Specific Troubleshooting ==="
    echo
    echo "Your system (Nobara Linux + RTX 4080 SUPER) specific checks:"
    echo
    echo "1. NVIDIA Driver Issues:"
    echo "   - Current driver: $(nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null || echo 'Unknown')"
    echo "   - Try older driver (555.x series) if on 570.x"
    echo "   - Check for prime-run conflicts"
    echo
    echo "2. Nobara-Specific Issues:"
    echo "   - Check if gamemode is conflicting: systemctl --user status gamemode"
    echo "   - Nobara uses PipeWire - try PULSE_LATENCY_MSEC=200"
    echo "   - Check for SELinux blocking: sudo sealert -a /var/log/audit/audit.log"
    echo
    echo "3. Hardware-Specific:"
    echo "   - RTX 4080 SUPER is very new - may need newer mesa/vulkan"
    echo "   - Try forcing older Vulkan loader: VK_LOADER_DEBUG=all"
    echo "   - Test with integrated graphics if available"
    echo
}

create_comprehensive_test_script() {
    log_info "=== Creating Comprehensive Test Script ==="
    
    cat > /tmp/ready_or_not_ultimate_test.sh << 'EOF'
#!/bin/bash

# Ultimate Ready or Not Compatibility Test
# Tests the most promising configurations in order

echo "=== Ready or Not Ultimate Compatibility Test ==="
echo

# Test 1: Proton 8.0-5 with WineD3D and dependencies
echo "TEST 1: Proton 8.0-5 + WineD3D + vcrun2019"
echo "1. Set compatibility: Proton 8.0-5"
echo "2. Launch options: PROTON_USE_WINED3D=1 %command% -noeac -dx11"
echo "3. Run: protontricks 1144200 vcrun2019"
echo "4. Test game"
read -p "Press Enter after test 1..."

# Test 2: Proton 7.0-6 (most reported working version)
echo
echo "TEST 2: Proton 7.0-6 (Community Favorite)"
echo "1. Set compatibility: Proton 7.0-6"
echo "2. Launch options: PROTON_USE_WINED3D=1 %command% -noeac"
echo "3. Test game"
read -p "Press Enter after test 2..."

# Test 3: Disable sync technologies
echo
echo "TEST 3: Proton 8.0-5 with sync disabled"
echo "1. Set compatibility: Proton 8.0-5"
echo "2. Launch options: PROTON_NO_ESYNC=1 PROTON_NO_FSYNC=1 PROTON_USE_WINED3D=1 %command% -noeac -dx11"
echo "3. Test game"
read -p "Press Enter after test 3..."

# Test 4: Minimal environment
echo
echo "TEST 4: Minimal Proton environment"
echo "1. Set compatibility: Proton 8.0-5"
echo "2. Launch options: PROTON_LOG=1 %command% -windowed -noeac"
echo "3. Check logs in ~/.steam/steam/steamapps/compatdata/1144200/"
echo "4. Test game"
read -p "Press Enter after test 4..."

echo
echo "Which test worked best?"
echo "A) Test 1 (8.0-5 + WineD3D + vcrun2019)"
echo "B) Test 2 (7.0-6 + WineD3D)"
echo "C) Test 3 (8.0-5 no sync)"
echo "D) Test 4 (minimal)"
echo "E) None worked"

read -p "Enter your answer (A/B/C/D/E): " answer

case $answer in
    A) echo "Great! Use Proton 8.0-5 with WineD3D and vcrun2019" ;;
    B) echo "Excellent! Proton 7.0-6 is the solution" ;;
    C) echo "Good! Sync issues were the problem" ;;
    D) echo "Minimal config works - build from there" ;;
    E) echo "We need to try Lutris or investigate deeper" ;;
    *) echo "Please run the test again and choose A, B, C, D, or E" ;;
esac
EOF

    chmod +x /tmp/ready_or_not_ultimate_test.sh
    log_success "Comprehensive test script created: /tmp/ready_or_not_ultimate_test.sh"
    echo
}

check_protontricks() {
    log_info "=== Checking Protontricks Installation ==="
    
    if command -v protontricks >/dev/null 2>&1; then
        log_success "Protontricks is installed"
        local version=$(protontricks --version 2>/dev/null || echo "unknown")
        log_info "Version: $version"
    else
        log_warning "Protontricks not installed - needed for Windows dependencies"
        echo "Install with: sudo dnf install protontricks"
        echo "Or: pipx install protontricks"
    fi
    echo
}

main() {
    log_info "Ready or Not Comprehensive Compatibility Analysis"
    log_info "Since both STL and vanilla Proton crash the same way, this is a deeper compatibility issue"
    echo
    
    test_proton_versions
    test_wine_dependencies  
    test_protondb_fixes
    test_system_specific_fixes
    check_protontricks
    create_comprehensive_test_script
    
    log_info "=== Recommendation ==="
    echo "1. Run the comprehensive test script: /tmp/ready_or_not_ultimate_test.sh"
    echo "2. Test each configuration systematically"
    echo "3. Most likely to work: Proton 7.0-6 or 8.0-5 with vcrun2019"
    echo "4. If all fail: Consider Lutris with Wine-GE"
    echo
    log_warning "Ready or Not is known to be problematic on Linux"
    log_warning "Some users report it simply doesn't work with any Proton version"
}

main "$@"