<ui:ContentDialog x:Class="DMS.WPF.Views.Dialogs.TriggerDialog"
                  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="http://schemas.inkore.net/lib/ui/wpf/modern"
                  xmlns:enums="clr-namespace:DMS.Core.Models.Triggers;assembly=DMS.Core"
                  xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
                  xmlns:hc="https://handyorg.github.io/handycontrol"
                  xmlns:ex="clr-namespace:DMS.Extensions"
                  xmlns:converters="clr-namespace:DMS.WPF.Converters"
                  xmlns:items="clr-namespace:DMS.WPF.ViewModels.Items"
                  Title="{Binding Title}"
                  mc:Ignorable="d"
                  d:DesignHeight="700"
                  d:DesignWidth="600"
                  d:DataContext="{d:DesignInstance vmd:TriggerDialogViewModel}"
                  MinWidth="500"
                  MinHeight="600"
                  PrimaryButtonText="{Binding PrimaryButText}"
                  CloseButtonText="取消"
                  PrimaryButtonCommand="{Binding SaveCommand}"
                  CloseButtonCommand="{Binding CancelCommand}"
                  DefaultButton="Primary">

    <ui:ContentDialog.Resources>
        <converters:EnumToVisibilityConverter x:Key="LocalEnumToVisibilityConverter" />
        <converters:IntToVisibilityConverter x:Key="IntToVisibilityConverter" />
        <converters:EmptyCollectionToVisibilityConverter x:Key="EmptyCollectionToVisibilityConverter" />
        <ex:EnumBindingSource x:Key="ConditionTypeEnum"
                              EnumType="{x:Type enums:ConditionType}" />
        <ex:EnumBindingSource x:Key="ActionTypeEnum"
                              EnumType="{x:Type enums:ActionType}" />
    </ui:ContentDialog.Resources>

    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <StackPanel Margin="10">
            <!-- Basic Info Section -->
            <GroupBox Header="基本信息"
                      Padding="5">
                <StackPanel>
                    <DockPanel Margin="0,0,0,5">
                        <Label Content="描述:"
                               Width="100"
                               VerticalAlignment="Center" />
                        <TextBox Text="{Binding Trigger.Description, UpdateSourceTrigger=PropertyChanged}"
                                 Width="300"
                                 HorizontalAlignment="Left" />
                    </DockPanel>

                    <CheckBox Content="激活"
                              IsChecked="{Binding Trigger.IsActive}"
                              Margin="0,0,0,10" />
                              
                    <!-- Selected Variables Section -->
                    <Label Content="已选择的变量:" 
                           FontWeight="Bold"
                           Margin="0,0,0,5"/>
                    <Border BorderBrush="LightGray" BorderThickness="1" 
                            Padding="5" 
                            Margin="0,0,0,10"
                            Background="WhiteSmoke">
                        <WrapPanel x:Name="SelectedVariablesPanel">
                            <ItemsControl ItemsSource="{Binding SelectedVariables}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate DataType="{x:Type items:VariableItemViewModel}">
                                        <Border Background="LightBlue" 
                                                CornerRadius="3" 
                                                Margin="2"
                                                Padding="5,2">
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Name}" 
                                                           VerticalAlignment="Center" 
                                                           Margin="0,0,5,0"/>
                                                <Button Content="×" 
                                                        FontSize="12" 
                                                        FontWeight="Bold"
                                                        Background="Transparent" 
                                                        BorderThickness="0"
                                                        Padding="2"
                                                        Click="RemoveVariableButton_Click"
                                                        Tag="{Binding}"/>
                                            </StackPanel>
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                            <TextBlock Text="暂无选择的变量" 
                                       FontStyle="Italic" 
                                       Foreground="Gray"
                                       Visibility="{Binding SelectedVariables, Converter={StaticResource EmptyCollectionToVisibilityConverter}}"/>
                        </WrapPanel>
                    </Border>
                    
                    <!-- Variable Search Section -->
                    <Label Content="搜索变量:" 
                           FontWeight="Bold"
                           Margin="0,0,0,5"/>
                    <TextBox x:Name="SearchTextBox"
                             Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
                             Margin="0,0,0,5"
                             ui:ControlHelper.PlaceholderText="输入变量名称进行搜索"/>
                             
                    <Border BorderBrush="LightGray" 
                            BorderThickness="1" 
                            Height="150"
                            Margin="0,0,0,10">
                        <ListBox x:Name="VariableListBox"
                                 ItemsSource="{Binding FilteredVariables}"
                                 SelectionMode="Single"
                                 MouseDoubleClick="VariableListBox_MouseDoubleClick">
                            <ListBox.ItemTemplate>
                                <DataTemplate DataType="{x:Type items:VariableItemViewModel}">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                                        <TextBlock Text=" - " />
                                        <TextBlock Text="{Binding Description}" FontStyle="Italic" Foreground="Gray"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Border>
                </StackPanel>
            </GroupBox>

            <!-- Condition Section -->
            <GroupBox Header="触发条件"
                      Padding="5"
                      Margin="0,10,0,0">
                <StackPanel>
                    <DockPanel Margin="0,0,0,5">
                        <Label Content="条件类型:"
                               Width="100"
                               VerticalAlignment="Center" />
                        <ComboBox ItemsSource="{Binding Source={StaticResource ConditionTypeEnum}}"
                                  SelectedItem="{Binding Trigger.Condition}"
                                  Width="200"
                                  HorizontalAlignment="Left" />
                    </DockPanel>

                    <!-- Conditional Fields based on Condition Type -->
                    <StackPanel
                        Visibility="{Binding Trigger.Condition, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=GreaterThan}">
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="阈值:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.Threshold, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                    </StackPanel>

                    <StackPanel
                        Visibility="{Binding Trigger.Condition, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=LessThan}">
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="阈值:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.Threshold, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                    </StackPanel>

                    <StackPanel
                        Visibility="{Binding Trigger.Condition, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=EqualTo}">
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="阈值:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.Threshold, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                    </StackPanel>

                    <StackPanel
                        Visibility="{Binding Trigger.Condition, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=NotEqualTo}">
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="阈值:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.Threshold, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                    </StackPanel>

                    <StackPanel
                        Visibility="{Binding Trigger.Condition, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=InRange}">
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="下限:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.LowerBound, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="上限:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.UpperBound, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                    </StackPanel>

                    <StackPanel
                        Visibility="{Binding Trigger.Condition, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=OutOfRange}">
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="下限:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.LowerBound, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="上限:"
                                   Width="100"
                                   VerticalAlignment="Center" />
                            <TextBox Text="{Binding Trigger.UpperBound, UpdateSourceTrigger=PropertyChanged}"
                                     Width="100"
                                     HorizontalAlignment="Left" />
                        </DockPanel>
                    </StackPanel>
                </StackPanel>
            </GroupBox>

            <!-- Action Section -->
            <GroupBox Header="触发动作"
                      Padding="5"
                      Margin="0,10,0,0">
                <StackPanel>
                    <DockPanel Margin="0,0,0,5">
                        <Label Content="动作类型:"
                               Width="100"
                               VerticalAlignment="Center" />
                        <ComboBox ItemsSource="{Binding Source={StaticResource ActionTypeEnum}}"
                                  SelectedItem="{Binding Trigger.Action}"
                                  Width="200"
                                  HorizontalAlignment="Left" />
                    </DockPanel>

                    <!-- Conditional Fields based on Action Type -->
                    <StackPanel
                        Visibility="{Binding Trigger.Action, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=SendEmail}">
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="收件人 (分号分隔):"
                                   Width="150"
                                   VerticalAlignment="Top" />
                            <TextBox
                                Text="{Binding EmailRecipients, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                AcceptsReturn="True"
                                TextWrapping="Wrap"
                                Width="350"
                                Height="60"
                                HorizontalAlignment="Left" />
                        </DockPanel>
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="邮件主题模板:"
                                   Width="150"
                                   VerticalAlignment="Center" />
                            <TextBox
                                Text="{Binding EmailSubjectTemplate, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                Width="350"
                                HorizontalAlignment="Left" />
                        </DockPanel>
                        <DockPanel Margin="0,0,0,5">
                            <Label Content="邮件内容模板:"
                                   Width="150"
                                   VerticalAlignment="Top" />
                            <TextBox
                                Text="{Binding EmailBodyTemplate, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                AcceptsReturn="True"
                                TextWrapping="Wrap"
                                Width="350"
                                Height="100"
                                HorizontalAlignment="Left" />
                        </DockPanel>
                    </StackPanel>

                    <!-- Add placeholders for other action types if needed -->
                    <StackPanel
                        Visibility="{Binding Trigger.Action, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=ActivateAlarm}">
                        <TextBlock Text="配置激活报警动作..."
                                   Margin="5" />
                    </StackPanel>

                    <StackPanel
                        Visibility="{Binding Trigger.Action, Converter={StaticResource LocalEnumToVisibilityConverter}, ConverterParameter=WriteToLog}">
                        <TextBlock Text="配置写入日志动作..."
                                   Margin="5" />
                    </StackPanel>
                </StackPanel>
            </GroupBox>

            <!-- Suppression Section -->
            <GroupBox Header="抑制设置"
                      Padding="5"
                      Margin="0,10,0,0">
                <StackPanel>
                    <DockPanel Margin="0,0,0,5">
                        <Label Content="抑制持续时间 (秒):"
                               Width="150"
                               VerticalAlignment="Center" />
                        <TextBox
                            Text="{Binding Trigger.SuppressionDuration, Converter={StaticResource NullableTimeSpanToSecondsConverter}, UpdateSourceTrigger=PropertyChanged}"
                            Width="100"
                            HorizontalAlignment="Left" />
                    </DockPanel>
                </StackPanel>
            </GroupBox>
        </StackPanel>
    </ScrollViewer>
</ui:ContentDialog>