<UserControl x:Class="MonitorModule.Views.MonitorView"
             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:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:b="clr-namespace:UIShare.Behaviors;assembly=UIShare"
             xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
             xmlns:oxy="http://oxyplot.org/wpf"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d"
             d:DesignHeight="700"
             d:DesignWidth="1200">
    <UserControl.Resources>
        <converters:LessThanConverter x:Key="LessThanConverter"/>
    </UserControl.Resources>

    <Border Background="#F5F7FA">
        <i:Interaction.Behaviors>
            <b:MouseDoubleClickBehavior
                Command="{Binding DataContext.RefreshCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
        </i:Interaction.Behaviors>

        <Grid x:Name="RootGrid" Margin="8">
            <Grid.RowDefinitions>
                <RowDefinition>
                    <RowDefinition.Style>
                        <Style TargetType="RowDefinition">
                            <Setter Property="Height" Value="Auto"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ActualWidth, ElementName=RootGrid, Converter={StaticResource LessThanConverter}, ConverterParameter=600}" Value="True">
                                    <Setter Property="Height" Value="0"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RowDefinition.Style>
                </RowDefinition>
                <RowDefinition>
                    <RowDefinition.Style>
                        <Style TargetType="RowDefinition">
                            <Setter Property="Height" Value="Auto"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ActualWidth, ElementName=RootGrid, Converter={StaticResource LessThanConverter}, ConverterParameter=600}" Value="True">
                                    <Setter Property="Height" Value="0"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RowDefinition.Style>
                </RowDefinition>
                <RowDefinition Height="*"/>
                <RowDefinition>
                    <RowDefinition.Style>
                        <Style TargetType="RowDefinition">
                            <Setter Property="Height" Value="Auto"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ActualWidth, ElementName=RootGrid, Converter={StaticResource LessThanConverter}, ConverterParameter=600}" Value="True">
                                    <Setter Property="Height" Value="0"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RowDefinition.Style>
                </RowDefinition>
            </Grid.RowDefinitions>

            <!-- ====== Row0：标题 ====== -->
            <TextBlock Grid.Row="0"
                       Text="{Binding TestStatus, StringFormat=监控界面 - {0}}"
                       FontSize="20" FontWeight="Bold"
                       Margin="4,0,0,8"/>

            <!-- ====== Row1：工具栏 ====== -->
            <Border Grid.Row="1"
                    Background="White"
                    BorderBrush="#DDD" BorderThickness="1"
                    CornerRadius="4" Padding="8" Margin="0,0,0,6">
                <StackPanel Orientation="Horizontal">
                    <Button Content="+ 添加监测项"
                            Command="{Binding AddChannelCommand}"
                            Padding="12,4"/>
                    <Button Content="− 删除监测项"
                            Command="{Binding DeleteChannelCommand}"
                            Padding="12,4" Margin="6,0,0,0"/>
                    <Button Content="↺ 复原视图"
                            Command="{Binding ResetViewCommand}"
                            Padding="12,4" Margin="20,0,0,0"
                            ToolTip="按数据范围重置坐标轴缩放/平移"/>
                    <Button Content="⟳ 刷新"
                            Command="{Binding RefreshDataCommand}"
                            Padding="12,4" Margin="6,0,0,0"
                            ToolTip="重新绘制图表"/>
                    <Button Content=" 保存"
           Command="{Binding SaveCommand}"
           Padding="12,4" Margin="6,0,0,0"
           ToolTip="保存到配置文件"/>
                </StackPanel>
            </Border>

            <!-- ====== Row2：主体 ====== -->
            <Grid Grid.Row="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition>
                        <ColumnDefinition.Style>
                            <Style TargetType="ColumnDefinition">
                                <Setter Property="Width" Value="280"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ActualWidth, ElementName=RootGrid, Converter={StaticResource LessThanConverter}, ConverterParameter=600}" Value="True">
                                        <Setter Property="Width" Value="0"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ColumnDefinition.Style>
                    </ColumnDefinition>

                    <ColumnDefinition>
                        <ColumnDefinition.Style>
                            <Style TargetType="ColumnDefinition">
                                <Setter Property="Width" Value="6"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ActualWidth, ElementName=RootGrid, Converter={StaticResource LessThanConverter}, ConverterParameter=600}" Value="True">
                                        <Setter Property="Width" Value="0"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ColumnDefinition.Style>
                    </ColumnDefinition>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!-- 左侧：可用方法列表 + 已添加监测通道列表 -->
                <Border Grid.Column="0"
                        Background="White"
                        BorderBrush="#DDD" BorderThickness="1"
                        CornerRadius="4">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <!-- 可用方法列表 -->
                        <Border Grid.Row="0" Background="#ECEFF4" Padding="8,4">
                            <TextBlock Text="可用监测项（选择后点击添加）" FontWeight="Bold"/>
                        </Border>
                        <ListBox Grid.Row="1"
                                 ItemsSource="{Binding AvailableMethods}"
                                 SelectedItem="{Binding SelectedAvailableMethod}"
                                 BorderThickness="0">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding DisplayName}" Margin="2"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

                        <!-- 分隔 -->
                        <Border Grid.Row="2" Background="#ECEFF4" Padding="8,4">
                            <TextBlock Text="已添加监测通道" FontWeight="Bold"/>
                        </Border>

                        <!-- 监测通道列表（带显示/隐藏CheckBox） -->
                        <ListBox Grid.Row="3"
                                 ItemsSource="{Binding Channels}"
                                 SelectedItem="{Binding SelectedChannel}"
                                 BorderThickness="0">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Margin="2">
                                        <DockPanel>
                                            <!-- 显示/隐藏复选框 -->
                                            <CheckBox IsChecked="{Binding IsDisplayed}"
                                                      VerticalAlignment="Center"
                                                      ToolTip="勾选=在图表上显示，取消=仅记录不显示"/>
                                            <!-- 颜色色块 -->
                                            <Border Width="10" Height="10"
                                                    CornerRadius="2"
                                                    VerticalAlignment="Center"
                                                    Margin="4,0,6,0">
                                                <Border.Background>
                                                    <SolidColorBrush Color="SteelBlue"/>
                                                </Border.Background>
                                            </Border>
                                            <TextBlock Text="{Binding DisplayName}"
                                                       VerticalAlignment="Center"
                                                       TextTrimming="CharacterEllipsis"/>
                                        </DockPanel>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

                        <!-- 状态 -->
                        <TextBlock Grid.Row="4"
                                   Text="{Binding StatusMessage}"
                                   FontSize="11" Foreground="#666"
                                   Margin="4,2" TextWrapping="Wrap"/>
                    </Grid>
                </Border>

                <GridSplitter Grid.Column="1"
                              HorizontalAlignment="Stretch"
                              Background="Transparent"/>

                <!-- 右侧：OxyPlot 图表 -->
                <Border Grid.Column="2"
                        Background="White"
                        BorderBrush="#DDD" BorderThickness="1"
                        CornerRadius="4">
                    <oxy:PlotView Model="{Binding Plot}"
                                  Background="Transparent"/>
                </Border>
            </Grid>

            <!-- ====== Row3：状态栏 ====== -->
            <Border Grid.Row="3"
                    Background="#ECEFF4"
                    Padding="8,4" Margin="0,6,0,0"
                    CornerRadius="2">
                <TextBlock Text="{Binding StatusMessage}"
                           Foreground="#444"
                           FontSize="12"/>
            </Border>
        </Grid>
    </Border>
</UserControl>
