<UserControl x:Class="TestingModule.Views.SingleStepEdit"
             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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:prism="http://prismlibrary.com/"
             d:DesignHeight="450"
             d:DesignWidth="800"
             mc:Ignorable="d">
    <UserControl.Resources>
        <converters:ParameterCategoryToVisibilityConverter x:Key="ParameterCategoryToVisibilityConverter" />
        <converters:ParameterTypeToBoolConverter x:Key="ParameterTypeToBoolConverter" />
        <converters:ParameterCategoryToStringConverter x:Key="ParameterCategoryToStringConverter" />
        <converters:IsEnumTypeConverter x:Key="IsEnumTypeConverter" />
        <converters:EnumValuesConverter x:Key="EnumValuesConverter" />
        <converters:EnumValueConverter x:Key="EnumValueConverter" />
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <converters:FilteredParametersConverter x:Key="FilteredParametersConverter" />

    </UserControl.Resources>
    <Grid>
        <GroupBox Header="单步编辑">
            <Grid>

                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="50" />
                </Grid.RowDefinitions>


                <!-- 名称 -->
                <StackPanel Grid.Row="0"
                            Orientation="Horizontal"
                            Margin="7">
                    <Label Width="60"
                           Content="名称" />
                    <TextBox MinWidth="120"
                             materialDesign:HintAssist.Hint=""
                             Text="{Binding SelectedStep.Name}" />
                </StackPanel>

                <!-- 合格条件 -->
                <StackPanel Grid.Row="1"
                            Orientation="Horizontal"
                            Margin="7">
                    <Label Width="60"
                           Content="合格条件" />
                    <TextBox MinWidth="120"
                             materialDesign:HintAssist.Hint=""
                             Text="{Binding SelectedStep.OKExpression}" />
                </StackPanel>

                <!-- 跳转 -->
                <StackPanel Grid.Row="2"
                            Orientation="Horizontal"
                            Margin="7">
                    <Label Width="60"
                           Content="跳转"
                           ToolTip="格式：OK跳转序号/NG跳转序号（默认为0/0)" />
                    <TextBox MinWidth="120"
                             Text="{Binding SelectedStep.GotoSettingString}"
                             materialDesign:HintAssist.Hint="格式：OK跳转序号/NG跳转序号（默认为0/0)" />
                </StackPanel>

                <!-- 备注 -->
                <StackPanel Grid.Row="3"
                            Orientation="Horizontal"
                            Margin="7">
                    <Label Width="60"
                           Content="备注" />
                    <TextBox MinWidth="120"
                             materialDesign:HintAssist.Hint=""
                             Text="{Binding SelectedStep.Description}" />
                </StackPanel>


                <!--  参数编辑区  -->
                <Grid Grid.Row="4">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Height="30"
                                Orientation="Horizontal">
                        <Label Margin="7,0"
                               VerticalAlignment="Bottom"
                               Content="参数" />
                    </StackPanel>
                    <ScrollViewer Grid.Row="1"
                                  Margin="40,0,0,0"
                                  HorizontalScrollBarVisibility="Auto"
                                  VerticalScrollBarVisibility="Auto">
                        <StackPanel>
                            <ItemsControl ItemsSource="{Binding SelectedStep.Method.Parameters}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="50" />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>
                                            <StackPanel Grid.Column="0"
                                                        Margin="7,7,0,7"
                                                        Orientation="Horizontal">
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text="{Binding Name}" />
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text="(" />
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text="{Binding Type.Name}" />
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text=")" />
                                            </StackPanel>
                                            <StackPanel Grid.Column="1"
                                                        Margin="7"
                                                        Orientation="Horizontal">
                                                <TextBlock Margin="5,0"
                                                           VerticalAlignment="Bottom"
                                                           Text="{Binding Category, Converter={StaticResource ParameterCategoryToStringConverter}}" />
                                            </StackPanel>
                                            <StackPanel Grid.Column="2"
                                                        Margin="7"
                                                        Orientation="Horizontal">
                                                <!--  输入参数编辑  -->
                                                <StackPanel Height="30"
                                                            IsEnabled="{Binding Type, Converter={StaticResource ParameterTypeToBoolConverter}}"
                                                            Orientation="Horizontal"
                                                            Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}}">
                                                    <CheckBox VerticalAlignment="Bottom"  Margin="0 15 0 0"
                                                              IsChecked="{Binding IsUseVar}" />
                                                    <Grid>
                                                        <!--  常量输入区域  -->
                                                        <Grid Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverse}">
                                                            <!--  非枚举类型  -->
                                                            <TextBox Height="22"
                                                                     MinWidth="120" 
                                                                     VerticalAlignment="Bottom"
                                                                     Padding="0"
                                                                     Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"
                                                                     materialDesign:HintAssist.Hint=""
                                                                     Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}, ConverterParameter=Collapse}" />

                                                            <!--  枚举类型  -->
                                                            <ComboBox Height="22"
                                                                      MinWidth="120"
                                                                      VerticalAlignment="Bottom"
                                                                      materialDesign:HintAssist.Hint=""
                                                                      ItemsSource="{Binding Type, Converter={StaticResource EnumValuesConverter}}"
                                                                      Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}}">
                                                                <ComboBox.SelectedItem>
                                                                    <MultiBinding Converter="{StaticResource EnumValueConverter}">
                                                                        <Binding Path="Type" />
                                                                        <Binding Path="Value" />
                                                                    </MultiBinding>
                                                                </ComboBox.SelectedItem>
                                                            </ComboBox>
                                                        </Grid>

                                                        <!--  变量选择框  -->
                                                        <ComboBox Height="22"
                                                                  MinWidth="120"
                                                                  materialDesign:HintAssist.Hint=""
                                                                  VerticalAlignment="Bottom"
                                                                  SelectedValue="{Binding VariableName}"
                                                                  SelectedValuePath="Name"
                                                                  Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}}">
                                                            <ComboBox.ItemsSource>
                                                                <MultiBinding Converter="{StaticResource FilteredParametersConverter}">
                                                                    <Binding Path="Type" />
                                                                    <Binding Path="DataContext.Program.Parameters"
                                                                             RelativeSource="{RelativeSource AncestorType=UserControl}" />
                                                                </MultiBinding>
                                                            </ComboBox.ItemsSource>
                                                            <ComboBox.ItemTemplate>
                                                                <DataTemplate>
                                                                    <StackPanel Orientation="Horizontal">
                                                                        <TextBlock Text="{Binding Name}" />
                                                                        <TextBlock Text="(" />
                                                                        <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                        <TextBlock Text=")" />
                                                                    </StackPanel>
                                                                </DataTemplate>
                                                            </ComboBox.ItemTemplate>
                                                        </ComboBox>

                                                    </Grid>
                                                </StackPanel>

                                                <!--  输出参数编辑  -->
                                                <StackPanel Height="30"
                                                            Orientation="Horizontal"
                                                            Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}">
                                                    <CheckBox VerticalAlignment="Bottom"
                                                              IsChecked="{Binding IsUseVar}"
                                                              Margin="0 15 0 0"
                                                              />
                                                    <ComboBox Width="120"
                                                              Margin="0,0,0,0"
                                                              materialDesign:HintAssist.Hint=""
                                                              VerticalAlignment="Bottom"
                                                              SelectedValue="{Binding VariableName}"
                                                              SelectedValuePath="Name"
                                                              Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}">
                                                        <ComboBox.ItemsSource>
                                                            <MultiBinding Converter="{StaticResource FilteredParametersConverter}">
                                                                <Binding Path="Type" />
                                                                <Binding Path="DataContext.Program.Parameters"
                                                                         RelativeSource="{RelativeSource AncestorType=UserControl}" />
                                                            </MultiBinding>
                                                        </ComboBox.ItemsSource>
                                                        <ComboBox.ItemTemplate>
                                                            <DataTemplate>
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock Text="{Binding Name}" />
                                                                    <TextBlock Text="(" />
                                                                    <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                    <TextBlock Text=")" />
                                                                </StackPanel>
                                                            </DataTemplate>
                                                        </ComboBox.ItemTemplate>
                                                    </ComboBox>
                                                </StackPanel>

                                            </StackPanel>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                            <ItemsControl ItemsSource="{Binding SelectedStep.SubProgram.Parameters}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Item}">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="50" />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>
                                            <StackPanel Grid.Column="0"
                                                        Margin="7,7,0,7"
                                                        Orientation="Horizontal">
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text="{Binding Name}" />
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text="(" />
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text="{Binding Type.Name}" />
                                                <TextBlock VerticalAlignment="Bottom"
                                                           Text=")" />
                                            </StackPanel>
                                            <StackPanel Grid.Column="1"
                                                        Margin="7"
                                                        Orientation="Horizontal">
                                                <TextBlock Margin="5,0"
                                                           VerticalAlignment="Bottom"
                                                           Text="{Binding Category, Converter={StaticResource ParameterCategoryToStringConverter}}" />
                                            </StackPanel>
                                            <StackPanel Grid.Column="2"
                                                        Margin="7"
                                                        Orientation="Horizontal">
                                                <!--  输入参数编辑  -->
                                                <StackPanel Height="30"
                                                            Orientation="Horizontal"
                                                            Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}}">
                                                    <CheckBox VerticalAlignment="Bottom"
                                                              IsChecked="{Binding IsUseVar}" />
                                                    <Grid>
                                                        <!--  常量输入区域  -->
                                                        <Grid Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverse}">
                                                            <!--  非枚举类型  -->
                                                            <TextBox Height="22"
                                                                     MinWidth="120"
                                                                     materialDesign:HintAssist.Hint=""
                                                                     VerticalAlignment="Bottom"
                                                                     Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"
                                                                     Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}, ConverterParameter=Collapse}" />

                                                            <!--  枚举类型  -->
                                                            <ComboBox Height="22"
                                                                      MinWidth="120"
                                                                      materialDesign:HintAssist.Hint=""
                                                                      VerticalAlignment="Bottom"
                                                                      ItemsSource="{Binding Type, Converter={StaticResource EnumValuesConverter}}"
                                                                      SelectedItem="{Binding Value}"
                                                                      Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}}" />
                                                        </Grid>

                                                        <!--  变量选择框  -->
                                                        <ComboBox Height="22"
                                                                  MinWidth="120"
                                                                  materialDesign:HintAssist.Hint=""
                                                                  VerticalAlignment="Bottom"
                                                                  ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Program.Parameters}"
                                                                  SelectedValue="{Binding VariableName}"
                                                                  SelectedValuePath="Name"
                                                                  Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}}">
                                                            <ComboBox.ItemTemplate>
                                                                <DataTemplate>
                                                                    <StackPanel Orientation="Horizontal">
                                                                        <TextBlock Text="{Binding Name}" />
                                                                        <TextBlock Text="(" />
                                                                        <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                        <TextBlock Text=")" />
                                                                    </StackPanel>
                                                                </DataTemplate>
                                                            </ComboBox.ItemTemplate>
                                                        </ComboBox>
                                                    </Grid>
                                                </StackPanel>

                                                <!--  输出参数编辑  -->
                                                <CheckBox VerticalAlignment="Bottom"
                                                          IsChecked="{Binding IsUseVar}"
                                                          Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}" />
                                                <ComboBox Width="120"
                                                          Margin="0,0,0,0"
                                                          materialDesign:HintAssist.Hint=""
                                                          VerticalAlignment="Bottom"
                                                          ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Program.Parameters}"
                                                          SelectedValue="{Binding VariableName}"
                                                          SelectedValuePath="Name"
                                                          Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}">
                                                    <ComboBox.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal">
                                                                <TextBlock Text="{Binding Name}" />
                                                                <TextBlock Text="(" />
                                                                <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                <TextBlock Text=")" />
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </ComboBox.ItemTemplate>
                                                </ComboBox>

                                            </StackPanel>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </ScrollViewer>
                </Grid>

                <!-- 按钮 -->
                <StackPanel Grid.Row="5"
                            Orientation="Horizontal"
                            FlowDirection="RightToLeft"
                            Margin="5,0">

                    <Button Width="90"
                            Content="取消"
                            Command="{Binding CancelEditCommand}" />

                    <Button Width="90"
                            Margin="20,0"
                            Content="保存"
                            Command="{Binding SaveStepCommand}" />

                </StackPanel>
            </Grid>
        </GroupBox>
    </Grid>
</UserControl>
