<UserControl x:Class="Plugin.DeviceDebug.Panels.CanChannelView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:ui="clr-namespace:HMI.UI;assembly=HMI.UI"
             xmlns:dp="clr-namespace:HMI.UI.DpUtil;assembly=HMI.UI"
             mc:Ignorable="d" 
             d:DesignHeight="700" d:DesignWidth="700">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/HMI.UI;component/Style/PanuonListView.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid dp:GridHelper.Space="10" Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- 标题 -->
        <Border Grid.Row="0" Background="#FF9800" CornerRadius="4" Padding="12,8">
            <TextBlock Text="CAN 通讯调试面板" FontSize="16" FontWeight="Bold" Foreground="White"/>
        </Border>

        <!-- 模拟器连接控制区 -->
        <Border Grid.Row="1" BorderBrush="#DDD" BorderThickness="1" CornerRadius="4" Padding="12" Background="#FAFAFA">
            <Grid dp:GridHelper.Space="8">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <!-- 第一行：总线名称和节点编号 -->
                <TextBlock Grid.Row="0" Grid.Column="0" Text="总线名称:" VerticalAlignment="Center" FontWeight="SemiBold"/>
                <TextBox Grid.Row="0" Grid.Column="1" x:Name="busNameInput" 
                         VerticalContentAlignment="Center" Padding="6,4" Text="CAN_BUS_1"/>

                <TextBlock Grid.Row="0" Grid.Column="2" Text="节点编号:" VerticalAlignment="Center" FontWeight="SemiBold" Margin="8,0,0,0"/>
                <TextBox Grid.Row="0" Grid.Column="3" x:Name="nodeIndexInput" 
                         VerticalContentAlignment="Center" Padding="6,4" Text="0"/>

                <Button Grid.Row="0" Grid.Column="5" x:Name="connectBtn" Content="连接" Width="80" Height="30"
                        Click="ConnectBtn_Click" 
                        Background="#4CAF50" Foreground="White" BorderThickness="0" Cursor="Hand" FontWeight="SemiBold"/>

                <!-- 第二行：状态显示和控制按钮 -->
                <TextBlock Grid.Row="1" Grid.Column="0" Text="状态:" VerticalAlignment="Center" FontWeight="SemiBold"/>
                <StackPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Orientation="Horizontal" VerticalAlignment="Center">
                    <Ellipse x:Name="statusIndicator" Width="12" Height="12" Fill="#9E9E9E" Margin="0,0,6,0"/>
                    <TextBlock x:Name="statusText" Text="未连接" VerticalAlignment="Center"/>
                </StackPanel>

                <TextBlock Grid.Row="1" Grid.Column="3" Text="统计:" VerticalAlignment="Center" FontWeight="SemiBold" Margin="8,0,0,0"/>
                <TextBlock Grid.Row="1" Grid.Column="4" x:Name="statsText" Text="TX: 0  RX: 0" 
                           VerticalAlignment="Center" Foreground="#666"/>

                <StackPanel Grid.Row="1" Grid.Column="5" Orientation="Horizontal">
                    <ToggleButton x:Name="autoResponseToggleBtn" Content="自动响应: 关" Width="100" Height="28" Margin="0,0,4,0"
                                  Click="AutoResponseToggleBtn_Click" IsEnabled="False" IsChecked="False"
                                  Background="#9E9E9E" Foreground="White" BorderThickness="0" Cursor="Hand" FontWeight="SemiBold"/>
                    <ToggleButton x:Name="maintenanceToggleBtn" Content="维护模式: 开" Width="100" Height="28" Margin="0,0,4,0"
                                  Click="MaintenanceToggleBtn_Click" IsEnabled="False" IsChecked="True"
                                  Background="#4CAF50" Foreground="White" BorderThickness="0" Cursor="Hand" FontWeight="SemiBold"/>
                    <Button x:Name="disconnectBtn" Content="断开" Width="60" Height="28" Margin="0,0,4,0"
                            Click="DisconnectBtn_Click" IsEnabled="False"
                            Background="#F44336" Foreground="White" BorderThickness="0" Cursor="Hand"/>
                    <Button x:Name="promiscuousBtn" Content="混杂模式" Width="80" Height="28"
                            Click="PromiscuousBtn_Click" IsEnabled="False"
                            Background="#2196F3" Foreground="White" BorderThickness="0" Cursor="Hand"/>
                </StackPanel>
            </Grid>
        </Border>

        <!-- 发送区 -->
        <Border Grid.Row="2" BorderBrush="#DDD" BorderThickness="1" CornerRadius="4" Padding="12">
            <Grid dp:GridHelper.Space="8">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <!-- 帧ID -->
                <TextBlock Grid.Row="0" Grid.Column="0" Text="帧 ID:" VerticalAlignment="Center" FontWeight="Bold"/>
                <TextBox Grid.Row="0" Grid.Column="1" x:Name="frameIdInput"
                         IsEnabled="{Binding IsMaintenanceMode, RelativeSource={RelativeSource AncestorType=UserControl}}"
                         VerticalContentAlignment="Center" Padding="6,4" Text="0x100"/>

                <!-- 数据 -->
                <TextBlock Grid.Row="1" Grid.Column="0" Text="数据(Hex):" VerticalAlignment="Center" FontWeight="Bold"/>
                <TextBox Grid.Row="1" Grid.Column="1" x:Name="dataInput"
                         IsEnabled="{Binding IsMaintenanceMode, RelativeSource={RelativeSource AncestorType=UserControl}}"
                         VerticalContentAlignment="Center" Padding="6,4" Text="00 00 00 00 00 00 00 00"/>

                <!-- 发送按钮 -->
                <Button Grid.Row="1" Grid.Column="2" x:Name="sendBtn" Content="发送" Width="80" Height="30"
                        IsEnabled="{Binding IsMaintenanceMode, RelativeSource={RelativeSource AncestorType=UserControl}}"
                        Click="SendBtn_Click" 
                        Background="#FF9800" Foreground="White" BorderThickness="0" Cursor="Hand" FontWeight="SemiBold"/>
            </Grid>
        </Border>

        <!-- 接收区 -->
        <Border Grid.Row="3" BorderBrush="#DDD" BorderThickness="1" CornerRadius="4">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <DockPanel Grid.Row="0" Margin="8,6">
                    <Button x:Name="clearBtn" Content="清空" Padding="12,4" DockPanel.Dock="Right"
                            Click="ClearBtn_Click"
                            Background="#9E9E9E" Foreground="White" BorderThickness="0" Cursor="Hand"/>
                    <TextBlock Text="接收记录" FontWeight="Bold" FontSize="14" VerticalAlignment="Center"/>
                </DockPanel>

                <ListView Grid.Row="1" x:Name="receiveList"
                          Style="{StaticResource DefaultListTable}"
                          VirtualizingPanel.IsVirtualizing="True"
                          VirtualizingPanel.VirtualizationMode="Standard">
                    <ListView.View>
                        <GridView AllowsColumnReorder="False">
                            <GridViewColumn Width="90" Header="时间">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Style="{StaticResource ListTableTextBlock}" 
                                                       Text="{Binding Timestamp, StringFormat=HH:mm:ss.fff}"/>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="80" Header="帧ID">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Style="{StaticResource ListTableTextBlock}" 
                                                       Text="{Binding FrameId}"/>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="50" Header="DLC">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Style="{StaticResource ListTableTextBlock}" 
                                                       Text="{Binding Dlc}"/>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="200" Header="数据">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Style="{StaticResource ListTableTextBlock}" 
                                                       Text="{Binding Data}" FontFamily="Consolas"/>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="50" Header="方向">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Text="{Binding Direction}">
                                                <TextBlock.Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource ListTableTextBlock}">
                                                        <Style.Triggers>
                                                            <DataTrigger Binding="{Binding Direction}" Value="TX">
                                                                <Setter Property="Foreground" Value="#2196F3"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding Direction}" Value="RX">
                                                                <Setter Property="Foreground" Value="#4CAF50"/>
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </TextBlock.Style>
                                            </TextBlock>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="100" Header="来源">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Style="{StaticResource ListTableTextBlock}" 
                                                       Text="{Binding Source}"/>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </Border>
    </Grid>
</UserControl>
