#!/bin/bash

# Ready or Not - Clean Slate Setup
# Removes all previous configurations and starts fresh with the simplest approach

set -e

GAME_ID="1144200"

# 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"; }

clean_all_configs() {
    log_info "=== Cleaning All Previous Configurations ==="
    
    # Remove STL config
    if [ -f "$HOME/.config/steamtinkerlaunch/gamecfgs/$GAME_ID.conf" ]; then
        log_info "Backing up and removing STL config..."
        mv "$HOME/.config/steamtinkerlaunch/gamecfgs/$GAME_ID.conf" \
           "$HOME/.config/steamtinkerlaunch/gamecfgs/$GAME_ID.conf.backup.$(date +%Y%m%d_%H%M%S)"
    fi
    
    # Remove DXVK config
    if [ -f "$HOME/.config/steamtinkerlaunch/dxvk/$GAME_ID.conf" ]; then
        log_info "Removing DXVK config..."
        rm -f "$HOME/.config/steamtinkerlaunch/dxvk/$GAME_ID.conf"
    fi
    
    # Remove compatdata (this forces Steam to recreate everything)
    local compatdata_paths=(
        "$HOME/.steam/steam/steamapps/compatdata/$GAME_ID"
        "$HOME/.local/share/Steam/steamapps/compatdata/$GAME_ID"
        "/mnt/NV2/SteamLibrary/steamapps/compatdata/$GAME_ID"
    )
    
    for path in "${compatdata_paths[@]}"; do
        if [ -d "$path" ]; then
            log_info "Removing compatdata: $path"
            rm -rf "$path"
        fi
    done
    
    log_success "All previous configurations cleaned"
}

simple_steam_setup() {
    log_info "=== Simple Steam Setup Instructions ==="
    echo
    echo "We're going to use the absolute simplest approach:"
    echo
    echo "1. Right-click Ready or Not in Steam"
    echo "2. Properties → Compatibility"
    echo "3. Force compatibility tool: Proton 8.0-5"
    echo "4. Launch Options: %command% -noeac"
    echo "5. Close properties"
    echo
    echo "That's it. No STL, no complex settings, just basic Proton."
    echo
}

test_basic_launch() {
    log_info "=== Testing Basic Launch ==="
    echo
    echo "After setting up the simple configuration:"
    echo "1. Launch Ready or Not from Steam"
    echo "2. Note exactly what happens:"
    echo "   - Does it reach splash screen?"
    echo "   - How long does it run?"
    echo "   - Any error messages?"
    echo
    echo "This baseline test will tell us if the game can run at all"
    echo "with the most basic Proton configuration."
    echo
}

if_basic_fails() {
    log_info "=== If Basic Test Fails ==="
    echo
    echo "If the game still crashes with basic Proton 8.0-5:"
    echo
    echo "Option A: Try Proton 7.0-6"
    echo "- Same setup, just change to Proton 7.0-6"
    echo "- This version has better compatibility with older games"
    echo
    echo "Option B: Add Windows dependencies"
    echo "- Install protontricks if not already installed"
    echo "- Run: protontricks 1144200 vcrun2019"
    echo "- Then test again"
    echo
    echo "Option C: Different launch arguments"
    echo "- Try: %command% -windowed -noeac"
    echo "- Or: %command% -dx11 -noeac"
    echo
}

if_basic_works() {
    log_info "=== If Basic Test Works ==="
    echo
    echo "If the game launches and runs (even if it crashes later):"
    echo
    echo "Great! We have a working baseline."
    echo "We can then gradually add improvements:"
    echo "1. Better graphics settings"
    echo "2. Performance optimizations"
    echo "3. 1440p gaming setup"
    echo "4. DLSS if working well"
    echo
    echo "But first, let's just get it running with minimal settings."
    echo
}

check_prerequisites() {
    log_info "=== Checking Prerequisites ==="
    
    # Check if game is installed
    if [ -f "/mnt/NV2/SteamLibrary/steamapps/common/Ready Or Not/ReadyOrNot.exe" ]; then
        log_success "Ready or Not is installed"
    else
        log_error "Ready or Not not found - please install through Steam first"
        return 1
    fi
    
    # Check Steam is running
    if pgrep -f steam >/dev/null; then
        log_success "Steam is running"
    else
        log_warning "Steam doesn't appear to be running"
    fi
    
    # Check Proton versions available
    if [ -d "$HOME/.steam/steam/steamapps/common" ]; then
        local proton_versions=$(ls "$HOME/.steam/steam/steamapps/common" | grep -i proton | head -3)
        log_info "Available Proton versions:"
        echo "$proton_versions"
    fi
    
    echo
}

main() {
    log_info "Ready or Not - Clean Slate Setup"
    log_info "Starting completely fresh with the simplest possible configuration"
    echo
    
    check_prerequisites
    clean_all_configs
    echo
    simple_steam_setup
    test_basic_launch
    echo
    if_basic_works
    if_basic_fails
    
    echo
    log_success "Clean slate setup complete!"
    log_info "Next step: Configure Steam with the simple settings above and test"
}

main "$@"