<Page
    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:viewModels="using:ModernKeePass.ViewModels"
    xmlns:local="using:ModernKeePass.Controls"
    xmlns:converters="using:ModernKeePass.Converters" 
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity" 
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:userControls="using:ModernKeePass.Views.UserControls"
    x:Class="ModernKeePass.Views.RecentDatabasesPage"
    mc:Ignorable="d">
    <Page.Resources>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <CollectionViewSource x:Name="RecentItemsSource" Source="{Binding RecentItems}" />
    </Page.Resources>
    <Page.DataContext>
        <viewModels:RecentVm/>
    </Page.DataContext>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <HyperlinkButton Grid.Row="0" HorizontalAlignment="Right">
            <StackPanel Orientation="Horizontal">
                <SymbolIcon Symbol="Cancel" />
                <TextBlock x:Uid="RecentClear" VerticalAlignment="Center" Margin="10,0,0,0" />
            </StackPanel>
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Click">
                    <core:CallMethodAction TargetObject="{Binding}" MethodName="ClearAll" />
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
        </HyperlinkButton>
        <ListView Grid.Row="1" 
            ItemsSource="{Binding Source={StaticResource RecentItemsSource}}"
            SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
            ItemContainerStyle="{StaticResource ListViewLeftIndicatorItemExpanded}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="10,0,10,0">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Text="{Binding Name}" Padding="5,0,0,0" />
                        <TextBlock Grid.Row="1" Text="{Binding Path}" Padding="5,0,0,0" FontSize="10" />
                        <userControls:CompositeKeyUserControl Grid.Row="2" x:Name="DatabaseUserControl" x:Uid="CompositeKeyOpenButton" HorizontalAlignment="Stretch" MinWidth="400" Margin="0,10,0,0" Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
                            <interactivity:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="ValidationChecking">
                                    <core:CallMethodAction TargetObject="{Binding}" MethodName="OpenDatabaseFile" />
                                </core:EventTriggerBehavior>
                                <core:EventTriggerBehavior EventName="ValidationChecked">
                                    <core:CallMethodAction TargetObject="{Binding}" MethodName="UpdateAccessTime" />
                                    <core:NavigateToPageAction TargetPage="ModernKeePass.Views.GroupDetailPage" />
                                </core:EventTriggerBehavior>
                            </interactivity:Interaction.Behaviors>
                        </userControls:CompositeKeyUserControl>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>