<UserControl x:Class="DatabaseEditor.Views.AuthorsPageView"
             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 AuthorsPageViewModel, 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:AuthorsFilterView Grid.Row="0" Grid.Column="0" />

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

            <ListView x:Name="ListView"
                      Grid.Row="0"
                      Style="{DynamicResource ListViewStyle}"
                      SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
                      ItemsSource="{Binding ItemsCollection}">
                <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 Surname}" Header="Surname" />
                    </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:AuthorDetailView Grid.Column="1" Grid.Row="1" />

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

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