# Unified Flamenco Worker Connection Script (STANDARD VERSION)
Write-Host "====================================" -ForegroundColor Cyan
Write-Host "    FLAMENCO WORKER LAUNCHER" -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host

# Define available systems
$systems = @(
    @{ ID = 1; Name = "masterbox"; ScriptPath = "run_masterbox_worker.ps1"; BatchPath = "run_masterbox_worker.bat" },
    @{ ID = 2; Name = "i9-13ks"; ScriptPath = "run_i9-13ks_worker.ps1"; BatchPath = "run_i9-13ks_worker.bat" },
    @{ ID = 3; Name = "max"; ScriptPath = "run_max_worker.ps1"; BatchPath = "run_max_worker.bat" },
    @{ ID = 4; Name = "echo"; ScriptPath = "run_echo_worker.ps1"; BatchPath = "run_echo_worker.bat" },
    @{ ID = 5; Name = "blender-boss"; ScriptPath = "run_blender-boss_worker.ps1"; BatchPath = "run_blender-boss_worker.bat" }
)

# Print menu options
Write-Host "Select a system to connect to:" -ForegroundColor Yellow
foreach ($system in $systems) {
    Write-Host "$($system.ID). $($system.Name)" -ForegroundColor Green
}
Write-Host "0. Connect to ALL systems (separate windows)" -ForegroundColor Magenta
Write-Host

# Get user selection
$selection = Read-Host "Enter your selection (0-$($systems.Count))"

# Process selection
if ($selection -eq "0") {
    # Launch all systems in separate windows
    Write-Host "Launching all worker scripts in separate windows..." -ForegroundColor Cyan
    
    foreach ($system in $systems) {
        $scriptPath = Join-Path (Get-Location) $system.BatchPath
        Start-Process "cmd.exe" -ArgumentList "/c $scriptPath" -WindowStyle Normal
        Write-Host "Started $($system.Name) worker in a new window." -ForegroundColor Green
        Start-Sleep -Seconds 2  # Add a small delay between launches
    }
    
    Write-Host "`nAll worker scripts have been launched." -ForegroundColor Cyan
    Write-Host "Each worker is running in its own command window." -ForegroundColor Yellow
}
else {
    # Find the selected system
    $selectedSystem = $systems | Where-Object { $_.ID -eq [int]$selection }
    
    if ($selectedSystem) {
        Write-Host "Launching $($selectedSystem.Name) worker script..." -ForegroundColor Cyan
        
        # Execute the PowerShell script directly
        $scriptPath = Join-Path (Get-Location) $selectedSystem.ScriptPath
        & $scriptPath
    }
    else {
        Write-Host "Invalid selection. Please run the script again." -ForegroundColor Red
    }
}

Write-Host "`nPress Enter to exit..."
$null = Read-Host 