<UserControl x:Class="DatabaseEditor.Views.AuthorsFilterView"
             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:local="clr-namespace:DatabaseEditor.Views"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             mc:Ignorable="d"
             DataContext="{Binding AuthorsFilterViewModel, Source={StaticResource ViewModelLocator}}"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <!-- Search box -->
            <TextBox Name="SearchBox" Text="{Binding SearchBoxText, Mode=OneWayToSource}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="TextChanged">
                        <i:InvokeCommandAction Command="{Binding SearchboxChangedCommand}"
                                               CommandParameter="{Binding ElementName=SearchBox, Path=Text}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBox>
            <ComboBox Grid.Column="1" SelectedIndex="0" ItemsSource="{Binding SearchableProperties}"
                      DisplayMemberPath="Key"
                      SelectedItem="{Binding SearchBoxProperty, Mode=OneWayToSource}" />
        </Grid>

        <Expander Grid.Row="1" Header="Extended search">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <!-- New rule -->
                    <ComboBox x:Name="LogicComboBox" Grid.Column="0"
                              SelectedIndex="0"
                              ItemsSource="{Binding LogicOperators}"
                              SelectedItem="{Binding SelectedLogicOperator, Mode=OneWayToSource}" />
                    <ComboBox x:Name="PropertyComboBox" Grid.Column="1" Grid.Row="0"
                              SelectedIndex="0"
                              ItemsSource="{Binding SearchableProperties}"
                              SelectedItem="{Binding SelectedProperty, Mode=OneWayToSource}"
                              DisplayMemberPath="Key" />
                    <ComboBox x:Name="OperatorComboBox" Grid.Column="2" Grid.Row="0"
                              SelectedIndex="0"
                              ItemsSource="{Binding AppliableOperators}"
                              SelectedItem="{Binding SelectedOperator, Mode=OneWayToSource}" />
                    <TextBox x:Name="ValueTextBox" Grid.Column="3" Grid.Row="0"
                             Text="{Binding FilterValue, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />
                    <Button Grid.Column="4" Grid.Row="0" Content="Add" Command="{Binding AddConditionCommand}" />
                </Grid>

                <!-- Applied rules -->
                <TextBlock Grid.Row="1" Grid.Column="0" Text="Applied rules:" />
                <ItemsControl Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left"
                              ItemsSource="{Binding Conditions, Mode=OneWay}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel VerticalAlignment="Center">
                                <TextBlock LineStackingStrategy="BlockLineHeight">
                                    <Run Text="{Binding LogicOperator, Mode=OneWay}" />
                                    <Run Text="{Binding PropertyName, Mode=OneWay}" />
                                    <Run Text="{Binding OperatorText, Mode=OneWay}" />
                                    "<Run Text="{Binding PropertyValue, Mode=OneWay}" />"
                                    <Button Content="Remove"
                                            Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}, Path=DataContext.RemoveConditionCommand}"
                                            CommandParameter="{Binding}" />
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>
        </Expander>
        <Button Grid.Row="2" HorizontalAlignment="Right" Content="Search" Command="{Binding ApplyFilterCommand}"
                IsDefault="True" />
    </Grid>
</UserControl>