# 檢查 XDLMulti 所需 DLL 是否存在
# 在 xdlmulti 資料夾內執行此腳本

$appDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$system32 = "C:\Windows\System32"
$sysWow64 = "C:\Windows\SysWOW64"

$requiredFiles = @(
    # WebView2 相關
    "WebView2Loader.dll",
    "WebView2Bridge.dll",

    # MySQL/MariaDB 相關
    "libmariadb.dll",
    "libmysql.dll",

    # VC++ Runtime (x64)
    "msvcp140.dll",
    "vcruntime140.dll",
    "vcruntime140_1.dll",

    # WebView2 Runtime 核心 (安裝後會在 System32)
    "EmbeddedBrowserWebView.dll"
)

Write-Host "===============================" -ForegroundColor Cyan
Write-Host " XDLMulti DLL 檢查報告" -ForegroundColor Cyan
Write-Host "===============================" -ForegroundColor Cyan
Write-Host ""

$missing = @()
$found   = @()

foreach ($dll in $requiredFiles) {
    $paths = @(
        "$appDir\$dll",
        "$system32\$dll",
        "$sysWow64\$dll"
    )

    $ok = $false
    foreach ($p in $paths) {
        if (Test-Path $p) {
            Write-Host "[OK]  $dll" -ForegroundColor Green
            Write-Host "      -> $p" -ForegroundColor DarkGray
            $ok = $true
            $found += $dll
            break
        }
    }

    if (-not $ok) {
        Write-Host "[缺失] $dll" -ForegroundColor Red
        $missing += $dll
    }
}

Write-Host ""
Write-Host "===============================" -ForegroundColor Cyan

# 額外檢查 WebView2 Runtime 是否已安裝
Write-Host ""
Write-Host "[ WebView2 Runtime 安裝狀態 ]" -ForegroundColor Yellow
$wv2reg = Get-ItemProperty "HKLM:\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" -ErrorAction SilentlyContinue
if ($wv2reg) {
    Write-Host "[OK]  WebView2 Runtime 已安裝，版本：$($wv2reg.pv)" -ForegroundColor Green
} else {
    Write-Host "[缺失] WebView2 Runtime 未安裝！" -ForegroundColor Red
    $missing += "WebView2 Runtime"
}

# 檢查 VC++ Redist
Write-Host ""
Write-Host "[ VC++ Redistributable 狀態 ]" -ForegroundColor Yellow
$vcRedist = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64" -ErrorAction SilentlyContinue
if ($vcRedist) {
    Write-Host "[OK]  VC++ Redist x64 已安裝，版本：$($vcRedist.Version)" -ForegroundColor Green
} else {
    Write-Host "[缺失] VC++ Redistributable 2015-2022 x64 未安裝！" -ForegroundColor Red
    $missing += "VC++ Redistributable"
}

Write-Host ""
Write-Host "===============================" -ForegroundColor Cyan
if ($missing.Count -eq 0) {
    Write-Host " 全部檔案齊全！可以正常執行。" -ForegroundColor Green
} else {
    Write-Host " 缺少以下項目：" -ForegroundColor Red
    foreach ($m in $missing) {
        Write-Host "   - $m" -ForegroundColor Red
    }
    Write-Host ""
    Write-Host " 修復建議：" -ForegroundColor Yellow
    if ($missing -contains "WebView2 Runtime") {
        Write-Host "  1. 安裝 WebView2 Runtime:" -ForegroundColor White
        Write-Host "     https://developer.microsoft.com/en-us/microsoft-edge/webview2/" -ForegroundColor Cyan
    }
    if ($missing -contains "VC++ Redistributable") {
        Write-Host "  2. 安裝 VC++ Redist 2015-2022 x64:" -ForegroundColor White
        Write-Host "     https://aka.ms/vs/17/release/vc_redist.x64.exe" -ForegroundColor Cyan
    }
}
Write-Host "===============================" -ForegroundColor Cyan
Write-Host ""
Write-Host "按任意鍵結束..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
