<Common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="WintellogMvvm.GroupDetailPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    IsTabStop="false"
    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:Common="using:WintellogMvvm.Common"
    xmlns:DataModel="using:WintellogMvvm.DataModel" xmlns:Data="using:PortableWintellog.Data" mc:Ignorable="d">

    <Page.Resources>

        <!-- Collection of items displayed by this page -->
        <CollectionViewSource
            x:Name="itemsViewSource"
            Source="{Binding Items}"
            d:Source="{Binding GroupList[0].Items, Source={d:DesignInstance Type=Data:BlogDataSource, IsDesignTimeCreatable=True}}"/>
    </Page.Resources>
    
    <Page.BottomAppBar>
        <AppBar x:Name="AppBar" Margin="10,0,10,0">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <StackPanel x:Name="LeftBar" Orientation="Horizontal" HorizontalAlignment="Left">
                    <Button x:Name="Pin" HorizontalAlignment="Left" Style="{StaticResource PinAppBarButtonStyle}" Click="Pin_Click_1"/>
                </StackPanel>
                <StackPanel x:Name="RightBar" Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1">
                    <Button x:Name="Home" HorizontalAlignment="Right" Style="{StaticResource HomeAppBarButtonStyle}" Click="Home_Click_1"/>
                </StackPanel>
            </Grid>
        </AppBar>
    </Page.BottomAppBar>

    <!--
        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}"
        DataContext="{Binding Group}"
        d:DataContext="{Binding GroupList[0], Source={d:DesignInstance Type=Data:BlogDataSource, IsDesignTimeCreatable=True}}">

        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Text="{Binding Title}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1"/>
        </Grid>

        <!-- Horizontal scrolling grid used in most view states -->
        <ScrollViewer
            x:Name="gridScrollViewer"
            AutomationProperties.AutomationId="DetailsScrollViewer"
            Grid.Row="1"
            Padding="0,-14,0,50"
            Style="{StaticResource HorizontalScrollViewerStyle}">

            <StackPanel x:Name="gridLayoutPanel" Margin="120,0,120,0" Orientation="Horizontal">
                <StackPanel Width="480" Margin="0,4,14,0">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Total Items: " Style="{StaticResource SubheaderTextStyle}" MaxHeight="60"/>
                        <TextBlock Text="{Binding ItemCount}" Style="{StaticResource SubheaderTextStyle}" MaxHeight="60"
                                   Margin="10 0 0 0"/>
                        <TextBlock Text="New Items: " Style="{StaticResource SubheaderTextStyle}" FontWeight="Bold" MaxHeight="60"
                                   Margin="10 0 0 0"/>
                        <TextBlock Text="{Binding NewItemCount}" Style="{StaticResource SubheaderTextStyle}" 
                                   FontWeight="Bold"
                                   MaxHeight="60"
                                   Margin="10 0 0 0"/>

                    </StackPanel>
                    <Image Height="400" Margin="0,12,18,20" Stretch="UniformToFill">
                        <Image.Source>
                            <BitmapImage UriSource="{Binding Items[0].DefaultImageUri}"/>
                        </Image.Source>
                    </Image>
                    <!--<TextBlock Text="{Binding Description}" Margin="0,0,18,0" Style="{StaticResource BodyTextStyle}"/>-->
                </StackPanel>
                <GridView
                    AutomationProperties.AutomationId="ItemGridView"
                    AutomationProperties.Name="Items In Group"
                    ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
                    ItemTemplate="{StaticResource Blog500x130ItemTemplate}"
                    SelectionMode="None"
                    IsItemClickEnabled="True"
                    ItemClick="ItemView_ItemClick">

                    <GridView.ItemContainerStyle>
                        <Style TargetType="GridViewItem">
                            <Setter Property="Margin" Value="52,0,0,10"/>
                        </Style>
                    </GridView.ItemContainerStyle>
                </GridView>
            </StackPanel>
        </ScrollViewer>

        <!-- Vertical scrolling list only used when snapped -->
        <ScrollViewer
            x:Name="snappedScrollViewer"
            AutomationProperties.AutomationId="SnappedDetailsScrollViewer"
            Grid.Row="1"
            Visibility="Collapsed"
            Style="{StaticResource VerticalScrollViewerStyle}">

            <StackPanel>
                <TextBlock Text="{Binding Title}" Margin="20,0,18,20" Style="{StaticResource TitleTextStyle}" MaxHeight="60"/>                
                <StackPanel Margin="20,20,18,30" Orientation="Horizontal">
                    <TextBlock Style="{StaticResource BodyTextStyle}" Text="Total Items: "/>
                    <TextBlock Style="{StaticResource BodyTextStyle}" Text="{Binding ItemCount}"/>
                </StackPanel>
                <ListView
                    AutomationProperties.AutomationId="ItemListView"
                    AutomationProperties.Name="Items In Group"
                    Margin="10,0,0,60"
                    ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
                    ItemTemplate="{StaticResource Blog80ItemTemplate}"
                    SelectionMode="None"
                    IsItemClickEnabled="True"
                    ItemClick="ItemView_ItemClick"/>
            </StackPanel>
        </ScrollViewer>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup>
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="gridLayoutPanel" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="100,0,90,0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    The back button and title have different styles when snapped, and the list representation is substituted
                    for the grid displayed in all other view states
                -->
                <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="gridScrollViewer" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="snappedScrollViewer" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</Common:LayoutAwarePage>