<UserControl x:Class="DatabaseEditor.Views.BooksPageView"
             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:views="clr-namespace:DatabaseEditor.Views"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             mc:Ignorable="d"
             DataContext="{Binding BooksPageViewModel, Source={StaticResource ViewModelLocator}}"
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <Style x:Key="ListViewStyle" TargetType="ListView">
            <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True" />
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <views:BooksFilterView />

        <Grid Grid.Row="1" Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="{Binding DatabaseChanged, Converter={StaticResource BoolToGridLength}}" />
            </Grid.RowDefinitions>

            <ListView x:Name="ListView"
                      Grid.Column="0"
                      ItemsSource="{Binding ItemsCollection}"
                      SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
                      Style="{DynamicResource ListViewStyle}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction
                            Command="{Binding ElementName=ListView, Path=DataContext.ItemSelectedCommand}"
                            CommandParameter="{Binding ElementName=ListView, Path=SelectedItem}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ListView.View>
                    <GridView x:Name="GridView">
                        <GridView.ColumnHeaderContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Ascending"
                                          Command="{Binding SortTableAscCommand}"
                                          CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Content}" />
                                <MenuItem Header="Descending"
                                          Command="{Binding SortTableDescCommand}"
                                          CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Content}" />
                            </ContextMenu>
                        </GridView.ColumnHeaderContextMenu>
                        <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
                        <GridViewColumn DisplayMemberBinding="{Binding Pages}" Header="Pages" />
                        <GridViewColumn DisplayMemberBinding="{Binding Year}" Header="Year" />
                        <GridViewColumn DisplayMemberBinding="{Binding ISBN}" Header="ISBN" />
                    </GridView>
                </ListView.View>
            </ListView>

            <Grid Grid.Row="1" Background="Gold">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock HorizontalAlignment="Center"
                           Text="Database has changed." VerticalAlignment="Center" Margin="0 0 10 0" />
                <Button Grid.Column="1" HorizontalAlignment="Right"
                        Content="Reload" Command="{Binding ReloadTableCommand}" />
            </Grid>
        </Grid>
        <views:BookDetailView Grid.Column="1"
                              Grid.Row="1" />

        <Button Grid.Row="2" Grid.Column="0"
                Content="Add new book" Command="{Binding AddNewCommand}" />

        <views:EditBookView Grid.Row="0" Grid.RowSpan="3"
                            Grid.Column="0" Grid.ColumnSpan="2"
                            Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=views:BooksPageView}, Path=DataContext.EditDialogVisible, Converter={StaticResource BooltoVis}}" />

    </Grid>
</UserControl>