<Window x:Class="Plugin.IntegrationTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="仪器仪表注册 &amp; 硬件调试 - 集成测试环境" 
        Height="800" Width="1300"
        WindowStartupLocation="CenterScreen"
        WindowState="Maximized"
        Background="#F5F5F5"
        Loaded="Window_Loaded" Closing="Window_Closing">
    <Grid Margin="8">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- 顶部状态栏 -->
        <Border Grid.Row="0" Background="#E3F2FD" CornerRadius="4" Padding="12,6" Margin="0,0,0,6">
            <DockPanel>
                <TextBlock Text="集成测试环境" FontSize="16" FontWeight="Bold" 
                           VerticalAlignment="Center" Foreground="#1565C0"/>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" DockPanel.Dock="Right">
                    <TextBlock x:Name="txtStatus" Text="就绪" FontSize="13" 
                               VerticalAlignment="Center" Foreground="#666" Margin="0,0,12,0"/>
                    <Button x:Name="btnRun" Content="▶ 运行自动化测试" Width="150" Height="32" 
                            FontSize="13" FontWeight="SemiBold"
                            Background="#4CAF50" Foreground="White" BorderThickness="0"
                            Cursor="Hand" Click="BtnRun_Click" Margin="0,0,6,0"/>
                    <Button x:Name="btnReset" Content="重置数据" Width="90" Height="32"
                            FontSize="13" Click="BtnReset_Click" Margin="0,0,6,0"
                            Background="#FF9800" Foreground="White" BorderThickness="0" Cursor="Hand"/>
                    <Button x:Name="btnClear" Content="清空日志" Width="80" Height="32" 
                            FontSize="13" Click="BtnClear_Click"/>
                </StackPanel>
            </DockPanel>
        </Border>

        <!-- 主 TabControl -->
        <TabControl x:Name="mainTabs" Grid.Row="1" Margin="0,4">
            <!-- Tab 1: 自动化测试 -->
            <TabItem Header="  自动化测试  " FontSize="13">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <ProgressBar x:Name="progressBar" Height="6" Minimum="0" Maximum="100" Value="0" Margin="4"/>
                    <TextBox x:Name="txtLog" Grid.Row="1"
                             IsReadOnly="True" VerticalScrollBarVisibility="Auto" 
                             HorizontalScrollBarVisibility="Auto"
                             FontFamily="Consolas" FontSize="12.5"
                             Background="#1E1E1E" Foreground="#D4D4D4"
                             BorderThickness="1" BorderBrush="#CCC"
                             Padding="8" TextWrapping="NoWrap"
                             AcceptsReturn="True" VerticalContentAlignment="Top"/>
                </Grid>
            </TabItem>

            <!-- Tab 2: 仪器仪表注册 -->
            <TabItem Header="  仪器仪表注册  " FontSize="13">
                <Border x:Name="deviceConfigHost" Background="White"/>
            </TabItem>

            <!-- Tab 3: 硬件调试 -->
            <TabItem Header="  硬件调试  " FontSize="13">
                <Border x:Name="deviceDebugHost" Background="White"/>
            </TabItem>

            <!-- Tab 4: CAN 模拟器 -->
            <TabItem Header="  CAN 模拟器  " FontSize="13">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    
                    <!-- 模拟器控制面板 -->
                    <Border Grid.Row="0" Background="#FFF3E0" CornerRadius="4" Padding="12,8" Margin="4">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            
                            <TextBlock Grid.Row="0" Grid.Column="0" Text="总线状态:" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,8,4"/>
                            <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
                                <Ellipse x:Name="canBusIndicator" Width="14" Height="14" Fill="#9E9E9E" Margin="0,0,8,0"/>
                                <TextBlock x:Name="canBusStatusText" Text="未启动" FontSize="13"/>
                            </StackPanel>
                            
                            <TextBlock Grid.Row="1" Grid.Column="0" Text="总线名称:" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,8,4"/>
                            <TextBox Grid.Row="1" Grid.Column="1" x:Name="canBusNameInput" Text="CAN_BUS_1" 
                                     VerticalContentAlignment="Center" Padding="6,4" Margin="0,0,80,4" Width="200" HorizontalAlignment="Left"/>
                            
                            <TextBlock Grid.Row="2" Grid.Column="0" Text="控制:" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,8,0"/>
                            <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
                                <Button x:Name="canStartBtn" Content="启动总线" Width="100" Height="30" Margin="0,0,8,0"
                                        Click="CanStartBtn_Click" Background="#4CAF50" Foreground="White" 
                                        BorderThickness="0" Cursor="Hand" FontWeight="SemiBold"/>
                                <Button x:Name="canStopBtn" Content="停止总线" Width="100" Height="30" Margin="0,0,8,0"
                                        Click="CanStopBtn_Click" Background="#F44336" Foreground="White" 
                                        BorderThickness="0" Cursor="Hand" FontWeight="SemiBold" IsEnabled="False"/>
                                <Button x:Name="canResetBtn" Content="重置所有" Width="100" Height="30"
                                        Click="CanResetBtn_Click" Background="#9E9E9E" Foreground="White" 
                                        BorderThickness="0" Cursor="Hand" FontWeight="SemiBold"/>
                            </StackPanel>
                        </Grid>
                    </Border>
                    
                    <!-- CAN 调试面板 -->
                    <Border x:Name="canSimulatorHost" Grid.Row="1" Background="White" Margin="4"/>
                </Grid>
            </TabItem>

            <!-- Tab 5: 全自动测试 -->
            <TabItem Header="  全自动测试  " FontSize="13">
                <Grid Margin="8">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <!-- 步骤信息区 -->
                    <Border Grid.Row="0" Background="#FFF3E0" CornerRadius="4" Padding="14,10" Margin="0,0,0,8">
                        <StackPanel>
                            <TextBlock Text="CAN 老化自动化步骤示例（方案 B + C）" FontSize="16" FontWeight="Bold" Foreground="#E65100" Margin="0,0,0,8"/>
                            <WrapPanel>
                                <TextBlock Text="发送设备:" FontWeight="SemiBold" VerticalAlignment="Center" Margin="0,0,6,6"/>
                                <ComboBox x:Name="autoTxDeviceCombo" Width="150" Height="28" Margin="0,0,16,6"/>

                                <TextBlock Text="目标设备:" FontWeight="SemiBold" VerticalAlignment="Center" Margin="0,0,6,6"/>
                                <ComboBox x:Name="autoRxDeviceCombo" Width="150" Height="28" Margin="0,0,16,6"/>

                                <TextBlock Text="循环次数:" FontWeight="SemiBold" VerticalAlignment="Center" Margin="0,0,6,6"/>
                                <TextBox x:Name="autoLoopCount" Width="60" Height="28" Text="5" VerticalContentAlignment="Center" Padding="4,0" Margin="0,0,16,6"/>

                                <TextBlock Text="帧间隔(ms):" FontWeight="SemiBold" VerticalAlignment="Center" Margin="0,0,6,6"/>
                                <TextBox x:Name="autoInterval" Width="70" Height="28" Text="300" VerticalContentAlignment="Center" Padding="4,0" Margin="0,0,16,6"/>
                            </WrapPanel>

                            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                                <Button x:Name="autoRunBtn" Content="▶ 运行自动化步骤" Width="180" Height="36" Margin="0,0,10,0"
                                        Click="AutoRunBtn_Click" Background="#FF9800" Foreground="White"
                                        BorderThickness="0" Cursor="Hand" FontWeight="SemiBold" FontSize="13"/>
                                <Button x:Name="perDeviceBtn" Content="▶ 逐设备一次收发(CAN-001~004)" Width="250" Height="36" Margin="0,0,10,0"
                                        Click="PerDeviceCanTest_Click" Background="#3F51B5" Foreground="White"
                                        BorderThickness="0" Cursor="Hand" FontWeight="SemiBold" FontSize="13"/>
                                <Button x:Name="autoStopBtn" Content="■ 中止" Width="90" Height="36" Margin="0,0,10,0"
                                        Click="AutoStopBtn_Click" Background="#F44336" Foreground="White"
                                        BorderThickness="0" Cursor="Hand" FontWeight="SemiBold" IsEnabled="False"/>
                                <ProgressBar x:Name="autoProgress" Width="300" Height="18" Margin="0,0,10,0"/>
                                <TextBlock x:Name="autoProgressText" Text="就绪" VerticalAlignment="Center" Foreground="#666"/>
                            </StackPanel>
                        </StackPanel>
                    </Border>

                    <!-- 代码预览 -->
                    <Expander Grid.Row="1" Header="查看等价代码（方案 B + C 使用示例）" IsExpanded="False" Margin="0,0,0,8">
                        <Border Background="#263238" Padding="12" CornerRadius="4" MaxHeight="280">
                            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                                <TextBlock FontFamily="Consolas" FontSize="12" Foreground="#ECEFF1">
<Run Foreground="#80CBC4">// 业务代码只看到类型化服务（方案 C）和作域（方案 B）</Run>
<Run>&#10;</Run><Run Foreground="#FFCC80">using </Run><Run Foreground="#C5E1A5">var </Run><Run>lease = </Run><Run Foreground="#FFCC80">await </Run><Run>auto.AcquireAsync(</Run><Run Foreground="#C5E1A5">"CAN-001"</Run><Run>, </Run><Run Foreground="#C5E1A5">"CAN_Aging"</Run><Run>, ct);</Run>
<Run>&#10;</Run><Run Foreground="#C5E1A5">await </Run><Run>canBus.SetAutoResponseAsync(lease, enabled:</Run><Run Foreground="#F48FB1">true</Run><Run>);</Run>
<Run>&#10;</Run><Run Foreground="#89DDFF">for </Run><Run>(</Run><Run Foreground="#C5E1A5">int </Run><Run>i = 0; i &lt; cycles; i++)</Run>
<Run>&#10;</Run><Run>{</Run>
<Run>&#10;</Run><Run>    </Run><Run Foreground="#C5E1A5">await </Run><Run>canBus.SendFrameAsync(lease, 0x100 + i, data);</Run>
<Run>&#10;</Run><Run>    </Run><Run Foreground="#C5E1A5">var </Run><Run>echo = </Run><Run Foreground="#C5E1A5">await </Run><Run>canBus.ReceiveFrameAsync(lease, ct);</Run>
<Run>&#10;</Run><Run>    Verify(echo != </Run><Run Foreground="#FF5370">null</Run><Run>);  </Run><Run Foreground="#546E7A">// 断言</Run>
<Run>&#10;</Run><Run>}</Run>
                                </TextBlock>
                            </ScrollViewer>
                        </Border>
                    </Expander>

                    <!-- 日志区 -->
                    <Border Grid.Row="2" BorderBrush="#DDD" BorderThickness="1" CornerRadius="4">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <DockPanel Grid.Row="0" Margin="10,6">
                                <TextBlock Text="自动化执行日志" FontWeight="Bold" FontSize="14"/>
                                <Button Content="清空" Padding="10,3" DockPanel.Dock="Right" Click="AutoClearLog_Click"
                                        Background="#9E9E9E" Foreground="White" BorderThickness="0" Cursor="Hand"/>
                            </DockPanel>
                            <TextBox Grid.Row="1" x:Name="autoLog"
                                     IsReadOnly="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
                                     FontFamily="Consolas" FontSize="12"
                                     Background="#1E1E1E" Foreground="#D4D4D4"
                                     BorderThickness="0" Padding="10" TextWrapping="NoWrap" AcceptsReturn="True"/>
                        </Grid>
                    </Border>
                </Grid>
            </TabItem>
        </TabControl>

        <!-- 底部结果栏 -->
        <Border Grid.Row="2" Padding="12,6" Background="#FAFAFA" 
                BorderBrush="#DDD" BorderThickness="1" CornerRadius="4">
            <DockPanel>
                <TextBlock x:Name="txtSummary" FontSize="13" FontWeight="SemiBold" 
                           VerticalAlignment="Center" Foreground="#333" Text="就绪 - 请切换到对应 Tab 进行手动测试"/>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" DockPanel.Dock="Right">
                    <TextBlock x:Name="txtPassed" FontSize="13" Foreground="#4CAF50" 
                               Margin="0,0,16,0" VerticalAlignment="Center"/>
                    <TextBlock x:Name="txtFailed" FontSize="13" Foreground="#F44336" 
                               VerticalAlignment="Center"/>
                </StackPanel>
            </DockPanel>
        </Border>
    </Grid>
</Window>
