# LoL Flash Timer 自動起動スクリプト
# LeagueClient.exe の起動を監視して Flash Timer を連動起動する

$LOL_PROCESS  = "LeagueClient"
$TIMER_EXE    = Join-Path $PSScriptRoot "LoL-Flash-Timer-1.0.0.exe"
$TIMER_PROCESS = "LoL-Flash-Timer"
$CHECK_INTERVAL = 5  # 秒

Write-Host "=== LoL Flash Timer Watcher 起動 ===" -ForegroundColor Cyan
Write-Host "LeagueClient の起動を監視中..." -ForegroundColor Gray

$wasRunning = $false

while ($true) {
    $lolRunning   = Get-Process -Name $LOL_PROCESS   -ErrorAction SilentlyContinue
    $timerRunning = Get-Process -Name $TIMER_PROCESS  -ErrorAction SilentlyContinue

    if ($lolRunning -and -not $wasRunning) {
        Write-Host "[$(Get-Date -f 'HH:mm:ss')] LoL 検知 → Flash Timer 起動" -ForegroundColor Green

        if (-not $timerRunning) {
            if (Test-Path $TIMER_EXE) {
                Start-Process $TIMER_EXE
            } else {
                # exeがない場合は npm start にフォールバック
                $npmPath = Join-Path $PSScriptRoot "package.json"
                if (Test-Path $npmPath) {
                    Start-Process "cmd" -ArgumentList "/c npm start" -WorkingDirectory $PSScriptRoot -WindowStyle Hidden
                } else {
                    Write-Host "[警告] Flash Timer が見つかりません: $TIMER_EXE" -ForegroundColor Yellow
                }
            }
        }
        $wasRunning = $true
    }

    if (-not $lolRunning -and $wasRunning) {
        Write-Host "[$(Get-Date -f 'HH:mm:ss')] LoL 終了を検知" -ForegroundColor Gray
        $wasRunning = $false
    }

    Start-Sleep -Seconds $CHECK_INTERVAL
}
