#!/bin/bash

# Test Ready or Not with vanilla Proton (bypass STL)
# This will help identify if the issue is STL-specific or general Proton compatibility

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_vanilla_proton() {
    log_info "Testing Ready or Not with vanilla Proton (bypassing STL)..."
    echo
    
    log_warning "This test requires temporarily changing Steam settings:"
    echo "1. Right-click Ready or Not in Steam"
    echo "2. Properties → Compatibility"
    echo "3. Change from 'Steam Tinker Launch' to 'Proton 9.0 (Beta)'"
    echo "4. In Launch Options, add: PROTON_USE_WINED3D=1 %command% -noeac -dx11"
    echo "5. Launch the game"
    echo
    
    log_info "What this test tells us:"
    echo "- If game works: STL configuration issue"
    echo "- If game still crashes: Deeper Proton/Wine compatibility issue"
    echo "- If game runs longer: STL is interfering with something"
    echo
    
    log_info "Expected behavior with vanilla Proton + WineD3D:"
    echo "- Should bypass most DXVK issues"
    echo "- May have lower performance but better compatibility"
    echo "- EasyAntiCheat should be disabled with -noeac"
    echo
    
    read -p "Press Enter after you've made these changes and tested the game..."
    echo
}

analyze_results() {
    log_info "Test Results Analysis:"
    echo
    echo "What happened when you tested with vanilla Proton?"
    echo
    echo "A) Game worked fine:"
    echo "   → Issue is with STL configuration"
    echo "   → We need to debug STL settings"
    echo
    echo "B) Game crashed the same way:"
    echo "   → Deeper compatibility issue with Ready or Not"
    echo "   → May need different Proton version or Wine settings"
    echo
    echo "C) Game ran longer but still crashed:"
    echo "   → Partial improvement, may need additional Wine/Proton tweaks"
    echo
    echo "D) Game crashed differently:"
    echo "   → New error pattern, need to investigate specific failure"
    echo
}

suggest_next_steps() {
    log_info "Based on the vanilla Proton test results:"
    echo
    echo "If Option A (worked fine):"
    echo "- Reset STL to absolute minimum settings"
    echo "- Gradually add features back one by one"
    echo "- Focus on argument passing in STL"
    echo
    echo "If Option B (same crash):"
    echo "- Try older Proton versions (8.0-5, 7.0-6)"
    echo "- Install Windows dependencies via Winetricks"
    echo "- Check ProtonDB for Ready or Not specific fixes"
    echo
    echo "If Option C (improvement):"
    echo "- Apply same settings in STL that worked in vanilla"
    echo "- Fine-tune Wine environment variables"
    echo "- Consider hybrid approach"
    echo
    echo "If Option D (different crash):"
    echo "- Analyze new error pattern"
    echo "- May indicate progress toward solution"
    echo
}

restore_stl() {
    log_info "To restore STL after testing:"
    echo "1. Right-click Ready or Not in Steam"
    echo "2. Properties → Compatibility" 
    echo "3. Change back to 'Steam Tinker Launch'"
    echo "4. Clear Launch Options field"
    echo "5. STL will use our configured settings"
}

main() {
    log_info "Vanilla Proton Testing for Ready or Not"
    log_info "This will help isolate STL vs general compatibility issues"
    echo
    
    test_vanilla_proton
    analyze_results
    suggest_next_steps
    echo
    restore_stl
}

main "$@"