<#
.SYNOPSIS
    PowerToggle 卸载脚本
.DESCRIPTION
    停止 PowerToggle,删除开机自启快捷方式,删除安装目录,
    并可选择删除创建的电源方案。
.NOTES
    运行方式: 右键 -> 使用 PowerShell 运行
    或: powershell -ExecutionPolicy Bypass -File uninstall.ps1
#>

$ErrorActionPreference = "Stop"
$InstallDir = "$env:LOCALAPPDATA\PowerToggle"
$StartupDir = [Environment]::GetFolderPath("Startup")
$ShortcutPath = "$StartupDir\PowerToggle.lnk"
$ConfigFile = "$InstallDir\PowerToggle.ini"

function Write-Step($msg) { Write-Host "[*] $msg" -ForegroundColor Cyan }
function Write-Ok($msg)   { Write-Host "[+] $msg" -ForegroundColor Green }
function Write-Warn($msg)  { Write-Host "[!] $msg" -ForegroundColor Yellow }

Write-Host ""
Write-Host "  ================================" -ForegroundColor Magenta
Write-Host "    PowerToggle 卸载程序 v1.0" -ForegroundColor Magenta
Write-Host "  ================================" -ForegroundColor Magenta
Write-Host ""

# ============================================================
# 步骤 1: 停止进程
# ============================================================
Write-Step "正在停止 PowerToggle..."

$proc = Get-Process PowerToggle -ErrorAction SilentlyContinue
if ($proc) {
    $proc | Stop-Process -Force
    Start-Sleep -Milliseconds 500
    Write-Ok "PowerToggle 已停止"
} else {
    Write-Ok "PowerToggle 未运行"
}

# ============================================================
# 步骤 2: 读取配置文件中的方案 GUID(用于可选清理)
# ============================================================
$highGuid = $null
$saverGuid = $null

if (Test-Path $ConfigFile) {
    foreach ($line in Get-Content $ConfigFile) {
        if ($line -match "HighPerfScheme=(.+)") { $highGuid = $matches[1].Trim() }
        if ($line -match "PowerSaverScheme=(.+)") { $saverGuid = $matches[1].Trim() }
    }
}

# ============================================================
# 步骤 3: 删除开机自启快捷方式
# ============================================================
Write-Step "正在删除开机自启快捷方式..."

if (Test-Path $ShortcutPath) {
    Remove-Item $ShortcutPath -Force
    Write-Ok "已删除快捷方式"
} else {
    Write-Ok "未找到快捷方式"
}

# ============================================================
# 步骤 4: 删除安装目录
# ============================================================
Write-Step "正在删除安装目录..."

if (Test-Path $InstallDir) {
    Remove-Item $InstallDir -Recurse -Force
    Write-Ok "已删除安装目录: $InstallDir"
} else {
    Write-Ok "未找到安装目录"
}

# ============================================================
# 步骤 5: 可选删除电源方案
# ============================================================
if ($highGuid -or $saverGuid) {
    Write-Host ""
    $choice = Read-Host "是否删除 PowerToggle 创建的电源方案?(y/N)"

    if ($choice -eq "y" -or $choice -eq "Y") {
        # 先切换到平衡方案(避免系统没有活动方案)
        Write-Step "正在切换到平衡电源方案..."
        powercfg /setactive 381b4222-f694-41f0-9685-ff5bb260df2e 2>$null
        Start-Sleep -Milliseconds 500

        if ($highGuid) {
            Write-Step "正在删除高性能方案 ($highGuid)..."
            powercfg /delete $highGuid 2>$null
            Write-Ok "已删除高性能方案"
        }

        if ($saverGuid) {
            Write-Step "正在删除节能方案 ($saverGuid)..."
            powercfg /delete $saverGuid 2>$null
            Write-Ok "已删除节能方案"
        }
    } else {
        Write-Ok "已保留电源方案(之后可通过 powercfg /delete 手动删除)"
    }
}

# ============================================================
# 完成
# ============================================================
Write-Host ""
Write-Ok "卸载完成!"
Write-Host ""
Read-Host "按回车键关闭"