#!/bin/bash

# Ready or Not - Direct Launch Script (Bypass Steam)
# Launches Ready or Not directly with mods enabled

set -e

# Game paths
GAME_DIR="/mnt/NV2/SteamLibrary/steamapps/common/Ready Or Not"
GAME_EXE="$GAME_DIR/ReadyOrNot.exe"
WINE_PREFIX="/mnt/NV2/SteamLibrary/steamapps/compatdata/1144200/pfx"
PROTON_PATH="/home/cal/.local/share/Steam/compatibilitytools.d/GE-Proton9-10"

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

launch_direct() {
    log_info "=== Ready or Not Direct Launch ==="
    echo
    
    # Check if game exists
    if [ ! -f "$GAME_EXE" ]; then
        log_error "Game executable not found: $GAME_EXE"
        return 1
    fi
    
    # Check if Proton exists
    if [ ! -f "$PROTON_PATH/proton" ]; then
        log_error "Proton not found: $PROTON_PATH/proton"
        return 1
    fi
    
    # Check if Wine prefix exists
    if [ ! -d "$WINE_PREFIX" ]; then
        log_error "Wine prefix not found: $WINE_PREFIX"
        return 1
    fi
    
    log_info "Game directory: $GAME_DIR"
    log_info "Wine prefix: $WINE_PREFIX"
    log_info "Proton version: GE-Proton9-10"
    echo
    
    # Set environment variables
    export WINEPREFIX="$WINE_PREFIX"
    export WINEDLLOVERRIDES="steam_api64=n"
    export STEAM_COMPAT_DATA_PATH="$WINE_PREFIX"
    export STEAM_COMPAT_CLIENT_INSTALL_PATH="/home/cal/.local/share/Steam"
    
    # Performance settings (matching STL config)
    export DXVK_ASYNC=1
    export DXVK_HDR=1
    export DXVK_FPSLIMIT=165
    export PROTON_ENABLE_NVAPI=1
    export PROTON_FORCE_LARGE_ADDRESS_AWARE=1
    
    log_info "Environment configured for direct launch"
    echo
    
    # Change to game directory
    cd "$GAME_DIR"
    
    # Launch options
    case "${1:-normal}" in
        normal)
            log_info "Launching in NORMAL mode (mods enabled)"
            LAUNCH_ARGS=""
            ;;
        safe)
            log_info "Launching in SAFE mode (mods disabled)"
            LAUNCH_ARGS="-paksafemode"
            ;;
        windowed)
            log_info "Launching in WINDOWED mode (mods enabled)"
            LAUNCH_ARGS="-windowed"
            ;;
        *)
            log_info "Launching with custom args: $1"
            LAUNCH_ARGS="$1"
            ;;
    esac
    
    echo
    log_info "Starting Ready or Not..."
    log_info "Press Ctrl+C to stop"
    echo
    
    # Launch with GameMode if available
    if command -v gamemoderun >/dev/null 2>&1; then
        log_info "Using GameMode for performance"
        exec gamemoderun "$PROTON_PATH/proton" run "$GAME_EXE" $LAUNCH_ARGS
    else
        log_warning "GameMode not available, launching without it"
        exec "$PROTON_PATH/proton" run "$GAME_EXE" $LAUNCH_ARGS
    fi
}

show_help() {
    echo "Ready or Not Direct Launcher"
    echo
    echo "Usage:"
    echo "  $0                    - Launch in normal mode (mods enabled)"
    echo "  $0 normal            - Launch in normal mode (mods enabled)"
    echo "  $0 safe              - Launch in safe mode (mods disabled)"
    echo "  $0 windowed          - Launch in windowed mode (mods enabled)"
    echo "  $0 'custom args'     - Launch with custom arguments"
    echo "  $0 --help           - Show this help"
    echo
    echo "Examples:"
    echo "  $0                           # Normal launch with mods"
    echo "  $0 safe                      # Safe mode without mods"
    echo "  $0 '-windowed -noeac'        # Windowed without anti-cheat"
    echo
}

list_mods() {
    log_info "Installed mods:"
    echo
    
    local mods_dir="/mnt/NV2/SteamLibrary/steamapps/common/Ready Or Not/ReadyOrNot/Content/Paks/Mods"
    if [ -d "$mods_dir" ] && [ "$(ls -A "$mods_dir" 2>/dev/null)" ]; then
        ls -la "$mods_dir"/*.pak 2>/dev/null || log_info "No .pak files found"
    else
        log_info "No mods installed"
    fi
    echo
}

# Main execution
case "${1:-}" in
    --help|-h)
        show_help
        ;;
    --mods|-m)
        list_mods
        ;;
    *)
        launch_direct "$1"
        ;;
esac