<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="FooEditor.HomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FooEditor"
    xmlns:common="using:FooEditor.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ad="using:Microsoft.Advertising.WinRT.UI"
    mc:Ignorable="d">
    <Page.Resources>
        <CollectionViewSource x:Key="groupedItemsViewSource" Source="{Binding Items}" ItemsPath="items" IsSourceGrouped="True"/>
        <DataTemplate x:Key="ButtonItemTemplate">
            <Button Command="{Binding Command}" CommandParameter="{Binding DocumentType}" Content="{Binding Title}">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Grid Width="120" Height="120">
                            <Rectangle Fill="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"/>
                            <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </DataTemplate>
        <DataTemplate x:Key="RecentFileItemTemplate">
            <Button Command="{Binding Command}" CommandParameter="{Binding File}">
                <Button.Template>
                    <ControlTemplate>
                        <Grid HorizontalAlignment="Left" Width="120" Height="120">
                            <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                                <TextBlock Margin="10,0,0,0" Text="{Binding File.Name}"
                                           Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}"
                                           Style="{StaticResource TitleTextStyle}"
                                           ToolTipService.Placement="Mouse">
                                    <ToolTipService.ToolTip>
                                        <TextBlock  Margin="10,0,0,0" Text="{Binding File.Path}"/>
                                    </ToolTipService.ToolTip>
                                </TextBlock>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </DataTemplate>
        <local:ItemGridViewTemplateSelector x:Key="ItemGridViewTemplateSelector" 
                                            ButtonItem="{StaticResource ButtonItemTemplate}"
                                            RecentFileItem="{StaticResource RecentFileItemTemplate}"/>
    </Page.Resources>
    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="False" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" x:Uid="HomeTitle" Grid.Column="1" Style="{StaticResource PageHeaderTextStyle}"/>
        </Grid>

        <GridView x:Name="gridView" Grid.Row="1" ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
                  ItemTemplateSelector="{StaticResource ItemGridViewTemplateSelector}" Margin="120,0,0,0">
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
            <GridView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,30,0"/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding title}" Style="{StaticResource GroupHeaderTextStyle}" Margin="0,0,0,10"/>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </GridView.GroupStyle>
        </GridView>
        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="NotSnapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>                    
                </VisualState>
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="gridView" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    20,0,0,0
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
