<UserControl
    x:Class="DMS.WPF.Views.VariableTableView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="http://schemas.inkore.net/lib/ui/wpf/modern"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:enums="clr-namespace:DMS.Core.Enums;assembly=DMS.Core"
    xmlns:ex="clr-namespace:DMS.Extensions"
    xmlns:helper="clr-namespace:DMS.WPF.Helper"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
    xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
    xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
    d:DataContext="{d:DesignInstance vm:VariableTableViewModel}"
    d:DesignHeight="600"
    d:DesignWidth="800"
    Loaded="VariableTableView_OnLoaded"
    mc:Ignorable="d">
    <UserControl.Resources>
        <ex:BindingProxy x:Key="proxy" Data="{Binding}" />
        <ex:EnumBindingSource x:Key="signalType" EnumType="{x:Type enums:SignalType}" />
        <valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

        <!--  标签字体的样式  -->
        <Style x:Key="VarTableLabelStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="{DynamicResource SecondaryTextBrush}" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
        <!--  值字体的样式  -->
        <Style x:Key="VarTableValueStyle" TargetType="TextBlock">
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="MinWidth" Value="100" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </UserControl.Resources>

    <DockPanel>

        <ikw:SimpleStackPanel DockPanel.Dock="Top" Margin="10" >
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                
                <!-- 返回设备详情按钮 -->
                <Button Grid.Column="0" 
                        Command="{Binding NavigateToDeviceDetailCommand}"
                        Style="{StaticResource ButtonDefault}"
                        ToolTip="返回设备详情"
                        Margin="0,0,10,0">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <ui:FontIcon
                                Icon="{x:Static ui:SegoeFluentIcons.ArrowLeft8}" />
                        </StackPanel>
                    </Button.Content>
                </Button>
                
                <!--  操作菜单  -->
                <controls:CommandBar Grid.Column="1"
                    x:Name="PrimaryCommandBar"
                    DefaultLabelPosition="Right"
                    IsOpen="False">
                <ui:AppBarButton Command="{Binding AddVariableCommand}" Label="添加变量">
                    <ui:AppBarButton.Icon>
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
                    </ui:AppBarButton.Icon>
                </ui:AppBarButton>

                <ui:AppBarButton Command="{Binding UpdateVariableCommand}" Label="编辑变量">
                    <ui:AppBarButton.Icon>
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
                    </ui:AppBarButton.Icon>
                </ui:AppBarButton>

                <ui:AppBarButton Command="{Binding DeleteVariableCommand}" Label="删除变量">
                    <ui:AppBarButton.Icon>
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Delete}" />
                    </ui:AppBarButton.Icon>
                </ui:AppBarButton>

                <ui:AppBarButton
                    Command="{Binding ImprotFromTiaVarTableCommand}"
                    Label="从TIA变量表导入"
                    Visibility="{Binding IsS7ProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
                    <ui:AppBarButton.Icon>
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Import}" />
                    </ui:AppBarButton.Icon>

                </ui:AppBarButton>

                <ui:AppBarButton
                    Command="{Binding ImportFromOpcUaServerCommand}"
                    Label="从OPC UA服务器导入"
                    Visibility="{Binding IsOpcUaProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
                    <ui:AppBarButton.Icon>
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Import}" />
                    </ui:AppBarButton.Icon>

                </ui:AppBarButton>


                <ui:AppBarButton x:Name="ShareButton" Label="Share">
                    <ui:AppBarButton.Icon>
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Share}" />
                    </ui:AppBarButton.Icon>
                </ui:AppBarButton>
                <ui:CommandBar.SecondaryCommands>
                    <ui:AppBarButton
                        x:Name="SettingsButton"
                        Icon="Setting"
                        Label="Settings" />
                </ui:CommandBar.SecondaryCommands>

               </controls:CommandBar>
           </Grid>
            <!--  变量表的名称描述等信息  -->
            <ikw:SimpleStackPanel
                Margin="5"
                Orientation="Horizontal"
                Spacing="10">
                <ui:ToggleSwitch
                    FontSize="16"
                    IsOn="{Binding CurrentVariableTable.IsActive}"
                    OffContent="停用"
                    OnContent="启用"
                    Toggled="OnIsActiveChanged" />
                <TextBlock Style="{StaticResource VarTableLabelStyle}" Text="变量表名称:" />
                <TextBlock Style="{StaticResource VarTableValueStyle}" Text="{Binding CurrentVariableTable.Name}" />
                <TextBlock Style="{StaticResource VarTableLabelStyle}" Text="变量表描述:" />
                <TextBlock Style="{StaticResource VarTableValueStyle}" Text="{Binding CurrentVariableTable.Description}" />
                <TextBlock Style="{StaticResource VarTableLabelStyle}" Text="所属设备:" />
                <TextBlock Style="{StaticResource VarTableValueStyle}" Text="{Binding CurrentVariableTable.Device.Name}" />
                <TextBlock Style="{StaticResource VarTableLabelStyle}" Text="协议:" />
                <TextBlock Style="{StaticResource VarTableValueStyle}" Text="{Binding CurrentVariableTable.Protocol}" />
                <TextBlock Style="{StaticResource VarTableLabelStyle}" Text="搜索:" />
                <TextBox
                    Width="200"
                    Margin="5,0,0,0"
                    HorizontalAlignment="Left"
                    ui:ControlHelper.PlaceholderText="搜索变量..."
                    Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" />
            </ikw:SimpleStackPanel>

            
        </ikw:SimpleStackPanel>
        <DataGrid
            x:Name="BasicGridView"
            Margin="10"
            AutoGenerateColumns="False"
            CanUserDeleteRows="False"
            CanUserSortColumns="False"
            IsReadOnly="True"
            ItemsSource="{Binding VariableItemListView}"
            SelectedItem="{Binding SelectedVariable}"
            SelectionMode="Extended"
            Style="{StaticResource DataGridBaseStyle}">
            <i:Interaction.Behaviors>
                <helper:SelectedItemsBehavior SelectedItems="{Binding SelectedVariables}" />
            </i:Interaction.Behaviors>

            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="{Binding AddVariableCommand}" Header="添加变量">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Command="{Binding UpdateVariableCommand}" Header="编辑变量">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Command="{Binding DeleteVariableCommand}" Header="删除变量">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Delete}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem
                        Command="{Binding ImprotFromTiaVarTableCommand}"
                        Header="从TIA变量表导入"
                        Visibility="{Binding IsS7ProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Import}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem
                        Command="{Binding ImportFromOpcUaServerCommand}"
                        Header="从OPC UA服务器导入"
                        Visibility="{Binding IsOpcUaProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Import}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem
                        Command="{Binding ChangePollingIntervalCommand}"
                        CommandParameter="{Binding PlacementTarget.SelectedItems, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                        Header="修改轮询间隔">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem
                        Command="{Binding ModifyConversionFormulaCommand}"
                        Header="修改数值转换公式">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem
                        Command="{Binding ModifyOpcUaUpdateTypeCommand}"
                        CommandParameter="{Binding PlacementTarget.SelectedItems, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                        Header="修改OPC UA更新方式"
                        Visibility="{Binding IsOpcUaProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Command="{Binding ChangeIsActiveCommand}" Header="修改启用状态">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem
                        Command="{Binding AddMqttServerToVariablesCommand}"
                        CommandParameter="{Binding PlacementTarget.SelectedItems, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                        Header="添加MQTT服务器">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    
                    <MenuItem
                        Command="{Binding ChangeHistorySettingsCommand}"
                        CommandParameter="{Binding PlacementTarget.SelectedItems, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                        Header="修改历史记录">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    
                    <MenuItem
                        Command="{Binding ChangeAlarmSettingsCommand}"
                        Header="修改报警设置">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
                        </MenuItem.Icon>
                    </MenuItem>
                    
                    <MenuItem
                        Command="{Binding ToHistoryCommand}"
                        Header="查看历史记录">
                        <MenuItem.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.History}" />
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <!-- <Setter Property="Background" Value="#fff"/> -->

                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsActive}" Value="False">
                            <Setter Property="Foreground" Value="{DynamicResource SecondaryTextBrush}" />
                        </DataTrigger>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{DynamicResource HoverBrush}" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="{DynamicResource PrimaryBrush}" />
                            <Setter Property="Foreground" Value="{DynamicResource TextIconBrush}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>


            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="名称" />
                <DataGridTextColumn Binding="{Binding Description}" Header="描述" />
                <DataGridTextColumn
                    Binding="{Binding S7Address}"
                    Header="S7地址"
                    IsReadOnly="True"
                    Visibility="{Binding Source={StaticResource proxy}, Path=Data.IsS7ProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}" />
                <DataGridTextColumn
                    Binding="{Binding OpcUaNodeId}"
                    Header="OPCUA节点ID"
                    IsReadOnly="True"
                    Visibility="{Binding Source={StaticResource proxy}, Path=Data.IsOpcUaProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}" />
                <DataGridTextColumn
                    Binding="{Binding DataType}"
                    Header="数据类型"
                    IsReadOnly="True" />
                <DataGridTemplateColumn Header="信号类型" SortMemberPath="SignalType">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding SignalType, Converter={StaticResource EnumDescriptionConverter}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource signalType}}" SelectedItem="{Binding SignalType}">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridCheckBoxColumn Binding="{Binding IsActive}" Header="启用" />
                
                <DataGridTemplateColumn Header="轮询间隔(毫秒)" SortMemberPath="PollingInterval">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding PollingInterval}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn
                    Binding="{Binding DataValue}"
                    Header="当前值"
                    IsReadOnly="True" />
                <DataGridTextColumn
                    Binding="{Binding DisplayValue}"
                    Header="显示值"
                    IsReadOnly="True" />

                <DataGridTextColumn
                    Binding="{Binding CreatedAt, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}"
                    Header="创建时间"
                    IsReadOnly="True" />
                <DataGridTextColumn
                    Binding="{Binding UpdatedAt, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss}'}"
                    Header="更新时间"
                    IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding ConversionFormula}" Header="转换规则" />
                
                <DataGridCheckBoxColumn Binding="{Binding IsHistoryEnabled}" Header="保存" />
                <DataGridTextColumn Binding="{Binding HistoryDeadband}" Header="保存范围" />
                <DataGridCheckBoxColumn Binding="{Binding IsAlarmEnabled}" Header="报警" />
                <DataGridTextColumn Binding="{Binding AlarmMinValue}" Header="报警最小值" />
                <DataGridTextColumn Binding="{Binding AlarmMaxValue}" Header="报警最大值" />
                </DataGrid.Columns>
        </DataGrid>
    </DockPanel>

</UserControl>