# UI 层诊断 (STA): 反射创建主窗口, 对每个系列执行 LoadSeries,
# 检查下拉框数据源是否为空 / 是否抛异常。
# 用法: powershell -STA -NoProfile -ExecutionPolicy Bypass -File tools\ui-diagnose.ps1
param(
  [string]$Exe = (Join-Path $PSScriptRoot '..\bin\OnebotCatalog.exe'),
  [string]$Opc = (Join-Path $PSScriptRoot '..\sample\OnebotCatalog_2026.08.opc')
)
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase

$asm = [Reflection.Assembly]::LoadFrom($Exe)
$pkgType = $asm.GetType('Ounibo.Catalog.Core.OpcPackage')
$pkg = $pkgType.GetMethod('Open').Invoke($null, @($Opc))

$mwType = $asm.GetType('Ounibo.Catalog.App.MainWindow')
$mw = [Activator]::CreateInstance($mwType, @($pkg, $false))
$ls = $mwType.GetMethod('LoadSeries', [Reflection.BindingFlags]'NonPublic,Instance')
$boxesField = $mwType.GetField('_boxes', [Reflection.BindingFlags]'NonPublic,Instance')

foreach ($s in $pkg.Catalog.series) {
  try {
    $ls.Invoke($mw, @($s)) | Out-Null
    $boxes = $boxesField.GetValue($mw)
    $parts = @()
    foreach ($k in $boxes.Keys) {
      $cb = $boxes[$k]
      $count = 0
      if ($cb.ItemsSource -ne $null) { $count = $cb.Items.Count }
      $parts += ($k + '=' + $count)
    }
    Write-Output ("系列 " + $s.code + " 下拉数据源: " + ($parts -join ' '))
  } catch {
    Write-Output ("系列 " + $s.code + " 异常: " + $_.Exception.InnerException.Message)
  }
}
$pkg.Dispose()
