<ui:ContentDialog
    x:Class="DMS.WPF.Views.Dialogs.VariableDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:en="clr-namespace:DMS.Core.Enums;assembly=DMS.Core"
    xmlns:enums="clr-namespace:DMS.Core.Enums;assembly=DMS.Core"
    xmlns:ex="clr-namespace:DMS.Extensions"
    xmlns:hc="https://handyorg.github.io/handycontrol"
    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:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
    Title="{Binding Title}"
    d:DataContext="{d:DesignInstance vmd:VariableDialogViewModel}"
    CloseButtonCommand="{Binding CancleButtonCommand}"
    CloseButtonText="取消"
    PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
    PrimaryButtonText="{Binding PrimaryButText}"
    mc:Ignorable="d">

    <ui:ContentDialog.Resources>
        <valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
        <valueConverts:EnumToStringConverter x:Key="EnumToStringConverter" />
        <ex:EnumBindingSource x:Key="ProtocolType" EnumType="{x:Type en:ProtocolType}" />
        <ex:EnumBindingSource x:Key="SignalType" EnumType="{x:Type enums:SignalType}" />
        <ex:EnumBindingSource x:Key="CSharpDataType" EnumType="{x:Type enums:DataType}" />
    </ui:ContentDialog.Resources>

    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <StackPanel Margin="16">
            <!--  错误信息显示区域  -->
            <Border
                Margin="0,0,0,15"
                Padding="10"
                Background="#FFF0F0"
                BorderBrush="#FF8080"
                BorderThickness="1"
                CornerRadius="4"
                Visibility="{Binding HasError, Converter={StaticResource BooleanToVisibilityConverter}}">
                <StackPanel Orientation="Horizontal">
                    <ui:FontIcon
                        Margin="0,0,10,0"
                        VerticalAlignment="Center"
                        Foreground="#C50500"
                        Glyph="&#xE783;" />
                    <TextBlock
                        VerticalAlignment="Center"
                        Foreground="#C50500"
                        Text="{Binding ErrorMessage}"
                        TextWrapping="Wrap" />
                </StackPanel>
            </Border>

            <!--  基本信息  -->
            <GroupBox Margin="0,0,0,15" Header="基本信息">
                <Grid Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="20" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <hc:TextBox
                        Grid.Row="0"
                        Grid.Column="0"
                        MinWidth="150"
                        hc:InfoElement.Title="变量名称:"
                        Text="{Binding Variable.Name, UpdateSourceTrigger=PropertyChanged}" />

                    <!--  S7 Address Field - Visible when Protocol is S7  -->
                    <hc:TextBox
                        Grid.Row="0"
                        Grid.Column="2"
                        MinWidth="150"
                        hc:InfoElement.Title="S7地址:"
                        IsEnabled="{Binding IsAddModel}"
                        Text="{Binding Variable.S7Address, UpdateSourceTrigger=PropertyChanged}">
                        <hc:TextBox.Style>
                            <Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Variable.Protocol, Converter={StaticResource EnumToStringConverter}}" Value="S7">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </hc:TextBox.Style>
                    </hc:TextBox>

                    <!--  OPC UA Node ID Field - Visible when Protocol is OpcUa  -->
                    <hc:TextBox
                        Grid.Row="0"
                        Grid.Column="2"
                        MinWidth="150"
                        hc:InfoElement.Title="OpcUa节点ID:"
                        IsEnabled="{Binding IsAddModel}"
                        Text="{Binding Variable.OpcUaNodeId, UpdateSourceTrigger=PropertyChanged}">
                        <hc:TextBox.Style>
                            <Style BasedOn="{StaticResource {x:Type hc:TextBox}}" TargetType="hc:TextBox">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Variable.Protocol, Converter={StaticResource EnumToStringConverter}}" Value="OpcUa">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </hc:TextBox.Style>
                    </hc:TextBox>

                    <hc:ComboBox
                        Grid.Row="1"
                        Grid.Column="0"
                        Margin="0,15,0,0"
                        hc:InfoElement.Title="协议类型:"
                        IsEnabled="False"
                        ItemsSource="{Binding Source={StaticResource ProtocolType}}"
                        SelectedItem="{Binding Variable.Protocol, UpdateSourceTrigger=PropertyChanged}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </hc:ComboBox>

                    <hc:ComboBox
                        Grid.Row="1"
                        Grid.Column="2"
                        Margin="0,15,0,0"
                        hc:InfoElement.Title="信号类型:"
                        ItemsSource="{Binding Source={StaticResource SignalType}}"
                        SelectedItem="{Binding Variable.SignalType, UpdateSourceTrigger=PropertyChanged}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </hc:ComboBox>

                    <hc:TextBox
                        Grid.Row="2"
                        Grid.Column="0"
                        Margin="0,15,0,0"
                        hc:InfoElement.Title="轮询间隔(毫秒):"
                        Text="{Binding  PollingInterval}"
                        >
                       
                    </hc:TextBox>

                    <hc:ComboBox
                        Grid.Row="2"
                        Grid.Column="2"
                        Margin="0,15,0,0"
                        hc:InfoElement.Title="数据类型:"
                        IsEnabled="{Binding IsAddModel}"
                        ItemsSource="{Binding Source={StaticResource CSharpDataType}}"
                        SelectedItem="{Binding Variable.CSharpDataType, UpdateSourceTrigger=PropertyChanged}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </hc:ComboBox>

                    <hc:TextBox
                        Grid.Row="3"
                        Grid.Column="0"
                        Grid.ColumnSpan="3"
                        Height="60"
                        Margin="0,15,0,0"
                        hc:InfoElement.Title="变量描述:"
                        Text="{Binding Variable.Description, UpdateSourceTrigger=PropertyChanged}"
                        TextWrapping="Wrap"
                        VerticalScrollBarVisibility="Auto" />
                </Grid>
            </GroupBox>

            <!--  保存设置  -->
            <GroupBox Margin="0,0,0,15" Header="保存设置">
                <Grid Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="20" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <hc:TextBox
                        Grid.Row="0"
                        Grid.Column="0"
                        hc:InfoElement.Title="保存范围:"
                        Text="{Binding Variable.HistoryDeadband, UpdateSourceTrigger=PropertyChanged}" />

                    <StackPanel
                        Grid.Row="0"
                        Grid.Column="2"
                        VerticalAlignment="Bottom"
                        Orientation="Horizontal">
                        <CheckBox
                            Margin="0,0,20,0"
                            Content="是否激活"
                            IsChecked="{Binding Variable.IsActive, UpdateSourceTrigger=PropertyChanged}" />
                        <CheckBox Content="是否保存" IsChecked="{Binding Variable.IsHistoryEnabled, UpdateSourceTrigger=PropertyChanged}" />
                    </StackPanel>
                </Grid>
            </GroupBox>

            <!--  报警设置  -->
            <GroupBox Header="报警设置">
                <Grid Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="20" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <hc:TextBox
                        Grid.Row="0"
                        Grid.Column="0"
                        hc:InfoElement.Title="报警最小值:"
                        Text="{Binding Variable.AlarmMinValue, UpdateSourceTrigger=PropertyChanged}" />

                    <hc:TextBox
                        Grid.Row="0"
                        Grid.Column="2"
                        hc:InfoElement.Title="报警最大值:"
                        Text="{Binding Variable.AlarmMaxValue, UpdateSourceTrigger=PropertyChanged}" />

                    <CheckBox
                        Grid.Row="1"
                        Grid.Column="0"
                        Grid.ColumnSpan="3"
                        Margin="0,15,0,0"
                        Content="是否报警"
                        IsChecked="{Binding Variable.IsAlarmEnabled, UpdateSourceTrigger=PropertyChanged}" />
                </Grid>
            </GroupBox>
        </StackPanel>
    </ScrollViewer>
</ui:ContentDialog>