param(
    [switch]$Force = $false,
    [string]$Framework = "net10.0-windows10.0.19041.0",
    [ValidateSet("Debug", "Release")]
    [string]$Configuration = "Debug",
    [switch]$SkipWebBuild = $false
)

$ErrorActionPreference = "Stop"

Write-Host "====================================" -ForegroundColor Cyan
Write-Host "  TodoList.Maui 启动脚本" -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""

$mauiProjectPath = Join-Path $PSScriptRoot "src\TodoList.Maui"
$mauiProjectFile = Join-Path $mauiProjectPath "TodoList.Maui.csproj"

if (!(Test-Path $mauiProjectFile)) {
    Write-Host "❌ MAUI 项目文件不存在: $mauiProjectFile" -ForegroundColor Red
    exit 1
}

if (!(Get-Command "dotnet" -ErrorAction SilentlyContinue)) {
    Write-Host "❌ 未找到 dotnet，请先安装 .NET SDK" -ForegroundColor Red
    exit 1
}

Write-Host "[1/2] 检查 TodoList.Maui 是否已在运行..." -ForegroundColor Yellow

$runningProcesses = Get-CimInstance Win32_Process -Filter "Name='TodoList.Maui.exe' OR Name='dotnet.exe'" | Where-Object {
    $_.CommandLine -like "*TodoList.Maui*"
}

if ($runningProcesses) {
    $pids = ($runningProcesses.ProcessId | Sort-Object) -join ", "
    Write-Host "⚠️  TodoList.Maui 可能已在运行中" -ForegroundColor Yellow
    Write-Host "   进程 ID: ($pids)" -ForegroundColor Gray

    if ($Force) {
        Write-Host ""
        Write-Host "🔄 强制关闭并重新启动..." -ForegroundColor Yellow
        try {
            $runningProcesses | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction Stop }
            Write-Host "✅ 旧进程已停止" -ForegroundColor Green
            Start-Sleep -Seconds 1
        } catch {
            Write-Host "❌ 停止进程失败: $_" -ForegroundColor Red
            exit 1
        }
    } else {
        Write-Host "   如果需要重新启动，请使用 -Force 参数" -ForegroundColor Gray
        Write-Host ""
        exit 0
    }
} else {
    Write-Host "✓ TodoList.Maui 未运行" -ForegroundColor Green
    Write-Host ""
}

Write-Host "[2/2] 启动 TodoList.Maui..." -ForegroundColor Yellow

$argumentList = @(
    "build",
    $mauiProjectFile,
    "-t:Run",
    "-c", $Configuration,
    "-f", $Framework
)

if ($SkipWebBuild) {
    $argumentList += "-p:SkipWebBuild=true"
}

Write-Host "📂 工作目录: $mauiProjectPath" -ForegroundColor Gray
Write-Host "🧩 Framework: $Framework" -ForegroundColor Gray
Write-Host "⚙️  Configuration: $Configuration" -ForegroundColor Gray
Write-Host "🚀 启动应用..." -ForegroundColor Green
Write-Host ""

try {
    $mauiProcess = Start-Process -FilePath "dotnet" -ArgumentList $argumentList -WorkingDirectory $mauiProjectPath -PassThru

    Write-Host "✅ TodoList.Maui 已启动" -ForegroundColor Green
    Write-Host "   进程 ID: ($($mauiProcess.Id))" -ForegroundColor Gray
    Write-Host ""
    Write-Host "📝 提示:" -ForegroundColor Yellow
    Write-Host "   - 使用 -Force 参数可以强制重启" -ForegroundColor Gray
    Write-Host "   - 使用 -SkipWebBuild 可跳过内置 Web 资源构建" -ForegroundColor Gray
    Write-Host "   - 使用 -Framework 可指定目标框架（如 net10.0-windows10.0.19041.0）" -ForegroundColor Gray
    Write-Host ""
} catch {
    Write-Host "❌ 启动失败: $_" -ForegroundColor Red
    exit 1
}

