<#
.SYNOPSIS
    阳泉婚礼宝典 - 桌面版卸载脚本
.DESCRIPTION
    删除桌面快捷方式、开始菜单目录、注册表卸载项
#>
#Requires -Version 2

[CmdletBinding()]
param([switch]$Silent)

$ErrorActionPreference = "Continue"

$APP_NAME       = "阳泉婚礼宝典"
$APP_ID         = "YangquanWedding"
$ScriptDir      = Split-Path -Parent $MyInvocation.MyCommand.Definition
$InstalledFlag  = Join-Path $ScriptDir ".installed"
$DesktopDir     = [System.Environment]::GetFolderPath("Desktop")
$StartMenuDir   = Join-Path ([System.Environment]::GetFolderPath("StartMenu")) "Programs\$APP_NAME"
$UninstallKey   = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$APP_ID"

function Write-Color($Text, $Color = "White") {
    if (-not $Silent) { Write-Host -ForegroundColor $Color $Text }
}

function Show-MsgBox {
    param([string]$Msg, [string]$Title = $APP_NAME, [string]$Icon = "Information")
    if ($Silent) { return }
    try {
        Add-Type -AssemblyName System.Windows.Forms
        [System.Windows.Forms.MessageBox]::Show($Msg, $Title, [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::$Icon) | Out-Null
    } catch { }
}

Write-Color "╔══════════════════════════════════════════╗" Cyan
Write-Color "║      $APP_NAME - 桌面版卸载程序         ║" Cyan
Write-Color "╚══════════════════════════════════════════╝" Cyan
Write-Color ""

$removedCount = 0

# 1) 桌面快捷方式
Write-Color "[1/3] 删除桌面快捷方式..."
$DesktopLnk = Join-Path $DesktopDir "$APP_NAME.lnk"
if (Test-Path $DesktopLnk) {
    Remove-Item -Path $DesktopLnk -Force
    Write-Color "  ✅ 桌面快捷方式已删除" Green
    $removedCount++
} else {
    Write-Color "  ℹ️  桌面快捷方式不存在" Gray
}

# 2) 开始菜单
Write-Color "[2/3] 删除开始菜单快捷方式..."
if (Test-Path $StartMenuDir) {
    Remove-Item -Path $StartMenuDir -Recurse -Force
    Write-Color "  ✅ 开始菜单目录已删除" Green
    $removedCount++
} else {
    Write-Color "  ℹ️  开始菜单目录不存在" Gray
}

# 3) 注册表
Write-Color "[3/3] 删除注册表卸载信息..."
if (Test-Path $UninstallKey) {
    Remove-Item -Path $UninstallKey -Recurse -Force
    Write-Color "  ✅ 注册表卸载信息已删除" Green
    $removedCount++
} else {
    Write-Color "  ℹ️  注册表项不存在" Gray
}

# 删除已安装标记
if (Test-Path $InstalledFlag) {
    Remove-Item $InstalledFlag -Force
}

Write-Color ""
if ($removedCount -gt 0) {
    Write-Color "╔══════════════════════════════════════════╗" Green
    Write-Color "║       ✅ 桌面版已卸载完成！              ║" Green
    Write-Color "╚══════════════════════════════════════════╝" Green
    Write-Color ""
    Write-Color "💡 便携版程序文件仍在: $ScriptDir" Gray
    Write-Color "   如需彻底删除，可手动删除该文件夹。" Gray
    Show-MsgBox "✅ $APP_NAME 桌面版已卸载完成。`n`n桌面快捷方式、开始菜单和注册表信息已清除。`n`n便携版程序文件仍在原目录，可手动删除或重新安装。" "卸载完成"
} else {
    Write-Color "ℹ️  检测到桌面版尚未安装，无需卸载。" Gray
    Show-MsgBox "ℹ️  检测到 $APP_NAME 桌面版尚未安装，无需卸载。" "提示"
}

exit 0
