<ContentControl x:Class="Microsoft.VisualStudio.TestTools.TestCaseMigrator.DataMappingView"
                                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                 xmlns:TestCaseMigrator="clr-namespace:Microsoft.VisualStudio.TestTools.TestCaseMigrator"
                                 x:ClassModifier="internal">
    <ContentControl.Resources>
        <TestCaseMigrator:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <TestCaseMigrator:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />

        <Style x:Key="VerticalSeparator"
               TargetType="{x:Type Path}">
            <Setter Property="Data"
                Value="M 0,0 L 0,24" />
            <Setter Property="Stretch"
                Value="None" />
            <Setter Property="Stroke"
                Value="Gray" />
            <Setter Property="VerticalAlignment"
                Value="Center" />
            <Setter Property="Margin"
                Value="0,0,2,0" />
            <Setter Property="SnapsToDevicePixels"
                Value="True" />
        </Style>

        <Style x:Key="DataMappingListBoxItem"
           TargetType="{x:Type ListBoxItem}">
            <Setter Property="OverridesDefaultStyle"
                Value="True" />
            <Setter Property="Background"
                Value="Transparent" />
            <Setter Property="HorizontalContentAlignment"
                Value="Stretch" />
            <Setter Property="VerticalContentAlignment"
                Value="Center" />
            <Setter Property="KeyboardNavigation.DirectionalNavigation"
                Value="None" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border Name="Bd"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                SnapsToDevicePixels="true"
                                CornerRadius="2"
                                Focusable="False">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          Focusable="False"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                 Value="true">
                                <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource ItemBackgroundBrush}" />
                            </Trigger>
                            <Trigger Property="IsEnabled"
                                 Value="false">
                                <Setter Property="Foreground"
                                    Value="Gray" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ContentControl.Resources>
    
    <Grid>
        <ListBox ItemsSource="{Binding DataMappingRows}"
                 SelectionMode="Extended"        
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 ItemContainerStyle="{StaticResource DataMappingListBoxItem}">
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type TestCaseMigrator:DataMappingRow}">
                    <Border HorizontalAlignment="Stretch"
                            BorderBrush="Gray"
                            BorderThickness="0.5"
                            SnapsToDevicePixels="true"
                            Focusable="False">
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding DataSourceField}"
                                       TextWrapping="Wrap"
                                       VerticalAlignment="Center"
                                       Grid.Column="0"
                                       Visibility="{Binding IsEditable, Converter={StaticResource InverseBooleanToVisibilityConverter}}">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Foreground" Value="Black" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}"
                                                         Value="True">
                                                <Setter Property="Foreground" Value="White" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <Grid Grid.Column="0"
                                  Visibility="{Binding IsEditable, Converter={StaticResource BooleanToVisibilityConverter}}">
                                <TextBlock Text="{Binding DataSourceField}"
                                           TextWrapping="Wrap"
                                           VerticalAlignment="Center"
                                            Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource InverseBooleanToVisibilityConverter}}" />
                                            
                                <ComboBox SelectedItem="{Binding DataSourceField}"
                                          VerticalAlignment="Top"
                                          Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource BooleanToVisibilityConverter}}"
                                          ItemsSource="{Binding DataSourceFields}" />
                            </Grid>
                            
                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="1" />
                            
                            <TextBlock Text="{Binding DataSourceValue}"
                                       TextWrapping="Wrap"
                                       Visibility="{Binding IsEditable, Converter={StaticResource InverseBooleanToVisibilityConverter}}"
                                       Grid.Column="2" >
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Foreground" Value="Black" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}"
                                                         Value="True">
                                                <Setter Property="Foreground" Value="White" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

                            <TextBox Text="{Binding DataSourceValue}"
                                       VerticalAlignment="Center"
                                       Visibility="{Binding IsEditable, Converter={StaticResource BooleanToVisibilityConverter}}"
                                       Grid.Column="2"
                                       ToolTip="{Binding DataSourceValue}" />

                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="3" />

                            <Grid Grid.Column="4">
                                <TextBlock Text="{Binding NewValue}"
                                           TextWrapping="Wrap"
                                          Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource InverseBooleanToVisibilityConverter}}"
                                            VerticalAlignment="Center" />
                                <ComboBox x:Name="WIFieldComboBox"
                                          SelectedItem="{Binding NewValue}"
                                          VerticalAlignment="Center"
                                          Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource BooleanToVisibilityConverter}}"
                                          ItemsSource="{Binding AllowedNewValues}" />
                            </Grid>
                        </Grid>
                    </Border>
                </DataTemplate>

                <DataTemplate DataType="{x:Type TestCaseMigrator:BlankDataMappingRow}">
                    <Border>
                        <Button Content="Add"
                                Margin="0,5,0,0"
                                MinWidth="40"
                                HorizontalAlignment="Left"
                                Click="AddDataMappingRowButton_Click" />
                    </Border>
                </DataTemplate>
                
                <DataTemplate DataType="{x:Type TestCaseMigrator:HeaderDataMappingRow}">
                    <Border HorizontalAlignment="Stretch"
                            BorderBrush="Gray"
                            BorderThickness="0.5"
                            SnapsToDevicePixels="true"
                            Padding="0,3,0,3"
                            Focusable="False">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding DataSourceField}"
                                       FontWeight="Bold"
                                       Grid.Column="0"
                                       Padding="3"/>
                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="1" />

                            <TextBlock Text="{Binding DataSourceValue}"
                                       FontWeight="Bold"
                                       Grid.Column="2"
                                       Padding="3"/>
                            
                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="3" />

                            <TextBlock Text="{Binding NewValue}"
                                       FontWeight="Bold"
                                       Grid.Column="4"
                                       Padding="3"/>
                        </Grid>
                    </Border>
                </DataTemplate>                
            </ListBox.Resources>
        </ListBox>
    </Grid>
</ContentControl>
