# UI 诊断2: 模拟用户点击下拉框 (设置 SelectedItem → 触发 SelectionChanged → RefreshOptions)
# 检查选中值是否保持、_sel 是否记录、状态栏是否更新。
# 用法: powershell -STA -NoProfile -ExecutionPolicy Bypass -File tools\ui-diagnose2.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))
$flags = [Reflection.BindingFlags]'NonPublic,Instance'
$ls = $mwType.GetMethod('LoadSeries', $flags)
$boxesField = $mwType.GetField('_boxes', $flags)
$selField = $mwType.GetField('_sel', $flags)
$statusField = $mwType.GetField('_status', $flags)
$titleField = $mwType.GetField('_modelTitle', $flags)

$kc = $null
foreach ($sx in $pkg.Catalog.series) { if ($sx.code -eq 'KC') { $kc = $sx; break } }
$ls.Invoke($mw, @($kc)) | Out-Null
$boxes = $boxesField.GetValue($mw)
$sel = $selField.GetValue($mw)

Write-Output ("KC 参数框: " + (($boxes.Keys | ForEach-Object { $_ }) -join ' '))

# 模拟点击第一个参数框 (型号) 的第一项
$cbType = $boxes['type']
Write-Output ("点击前 type SelectedItem: " + $(if ($cbType.SelectedItem) { $cbType.SelectedItem.ToString() } else { 'null' }))
$firstItem = $cbType.Items[0]
$cbType.SelectedItem = $firstItem
Write-Output ("点击后 type SelectedItem: " + $(if ($cbType.SelectedItem) { $cbType.SelectedItem.ToString() } else { 'null' }))
Write-Output ("_sel 内容: " + (($sel.Keys | ForEach-Object { $_ + '=' + $sel[$_] }) -join ' '))

# 再点第二个框 (缸径) 的第一项
$cbBore = $boxes['bore']
$cbBore.SelectedItem = $cbBore.Items[0]
Write-Output ("bore SelectedItem: " + $(if ($cbBore.SelectedItem) { $cbBore.SelectedItem.ToString() } else { 'null' }))
Write-Output ("_sel 内容: " + (($sel.Keys | ForEach-Object { $_ + '=' + $sel[$_] }) -join ' '))

# 状态栏与标题
$status = $statusField.GetValue($mw)
$title = $titleField.GetValue($mw)
Write-Output ("状态栏: " + $status.Text)
Write-Output ("标题: " + $title.Text)
$pkg.Dispose()
