<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="Invaders.View.InvadersPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Invaders.View"
    xmlns:common="using:Invaders.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:viewmodel="using:Invaders.ViewModel" 
    SizeChanged="pageRoot_SizeChanged" 
    ManipulationMode="TranslateX" ManipulationCompleted="pageRoot_ManipulationCompleted" ManipulationDelta="pageRoot_ManipulationDelta" Tapped="pageRoot_Tapped"
    >

    <Page.Resources>
        <viewmodel:InvadersViewModel x:Name="viewModel"/>
        <viewmodel:BooleanVisibilityConverter x:Name="visibilityConverter"/>
        <x:String x:Key="AppName">Invaders</x:String>
    </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}" DataContext="{StaticResource viewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel HorizontalAlignment="Right" Margin="0,60,120,0">
            <TextBlock Style="{StaticResource GroupHeaderTextStyle}" Text="{Binding Score}" HorizontalAlignment="Right" Margin="0,0,15,0"/>
            <GridView ItemsSource="{Binding Lives}" HorizontalAlignment="Right" Width="Auto" Height="50" SelectionMode="None">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Image Source="ms-appx:///Assets/player.png" Stretch="Fill" Height="15" Width="20"/>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </StackPanel>

        <Border BorderBrush="Blue" BorderThickness="2" CornerRadius="10" Margin="5" Background="Black" 
                Grid.Row="1" x:Name="playArea" Loaded="playArea_Loaded">
            <ItemsControl 
                    ItemsSource="{Binding Path=Sprites}" 
                    Margin="5">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Background="Black" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Border>

        <StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"
                    Visibility="{Binding GameOver, Converter={StaticResource visibilityConverter}}" >
            
            <TextBlock Grid.Row="1" Text="Game Over" VerticalAlignment="Center" 
                       FontFamily="Arial Narrow" FontSize="150"
                       Foreground="Yellow" FontWeight="ExtraBold" FontStyle="Italic"
                       Margin="0,0,0,60" 
                       />
            
            <Button Content="Start a new game" Background="Black"
                    HorizontalAlignment="Center" FontSize="30" Click="StartButtonClick"/>
            
            <Button Content="Learn to build this app" Background="Black"
                    HorizontalAlignment="Center" FontSize="30" Click="LearnButton"/>
        </StackPanel>

        <TextBlock Grid.Row="1" Text="Paused" VerticalAlignment="Center" 
                   FontFamily="Arial Narrow" FontSize="60"
                   Foreground="Yellow" FontWeight="ExtraBold" FontStyle="Italic"
                   Margin="120,0,0,60" 
                   Visibility="{Binding Paused, Converter={StaticResource visibilityConverter}}" 
                   />


        <!-- 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" Grid.Column="1" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}"/>
        </Grid>

        <Popup x:Name="aboutPopup" Grid.RowSpan="2" VerticalAlignment="Stretch" HorizontalAlignment="Right"
               Width="400" IsOpen="False">
            <StackPanel Background="Blue" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="360" Margin="20">
                <StackPanel.Transitions>
                    <TransitionCollection>
                        <EntranceThemeTransition/>
                    </TransitionCollection>
                </StackPanel.Transitions>

                <StackPanel Orientation="Horizontal">
                    <Button Style="{StaticResource AppBarButtonStyle}" Click="CloseAboutPopup" Grid.RowSpan="2">&#xE0D5;</Button>
                    <TextBlock Text="About" Style="{StaticResource PageSubheaderTextStyle}" FontWeight="Bold"/>
                </StackPanel>

                <StackPanel Margin="30,0,0,0">
                    <TextBlock Style="{StaticResource SubheaderTextStyle}">
                        <Run FontWeight="ExtraBold" FontStyle="Italic">Head First C#</Run>
                        presents...
                    </TextBlock>

                    <TextBlock Grid.Row="1" Text="Invaders" HorizontalAlignment="Center" 
                   FontFamily="Arial Narrow" FontSize="50"
                   Foreground="Yellow" FontWeight="ExtraBold" FontStyle="Italic" Margin="10"
                   />

                    <TextBlock Style="{StaticResource TitleTextStyle}">
                        <Run>
                            Invaders pays homage to one of the most popular, revered, and replicated icons
                            in video game history. This is the final project in
                        </Run>
                        <Run FontStyle="Italic" FontWeight="Bold" Text=" Head First C# (3rd ed) "/>
                        <Run>
                            by Andrew Stellman and Jennifer Greene (O'Reilly, 2013).
                        </Run>
                    </TextBlock>
                    
                    
                    <TextBlock Margin="0,10,0,0" Style="{StaticResource SubtitleTextStyle}">Invaders is an open source app released under the Microsoft Public License (Ms-Pl). Download the source code from CodePlex:</TextBlock>
                    <HyperlinkButton Margin="0,10,0,10"
                        Content="https://invaders.codeplex.com/" NavigateUri="http://www.headfirstlabs.com/hfcsharp" Style="{StaticResource TextPrimaryButtonStyle}" FontSize="16"/>

                    <TextBlock Margin="0,10" Style="{StaticResource SubtitleTextStyle}">© 2013 Andrew Stellman and Jennifer Greene</TextBlock>
                </StackPanel>
            </StackPanel>
        </Popup>

        <Popup x:Name="learnMorePopup" Grid.RowSpan="2" VerticalAlignment="Stretch" HorizontalAlignment="Right"
               Width="400" IsOpen="False">
            <StackPanel Background="Blue" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="360" Margin="20">
                <StackPanel.Transitions>
                    <TransitionCollection>
                        <EntranceThemeTransition/>
                    </TransitionCollection>
                </StackPanel.Transitions>

                <StackPanel Orientation="Horizontal">
                    <Button Style="{StaticResource AppBarButtonStyle}" Click="CloseLearnMorePopup" Grid.RowSpan="2">&#xE0D5;</Button>
                    <TextBlock Text="About" Style="{StaticResource PageSubheaderTextStyle}" FontWeight="Bold"/>
                </StackPanel>
                
                <Image Source="ms-appx:///Assets/hfcsharp.gif" Width="300" RenderTransformOrigin="0.5,0.5">
                    <Image.RenderTransform>
                        <CompositeTransform Rotation="4"/>
                    </Image.RenderTransform>
                </Image>

                <StackPanel Margin="30,20,0,0">
                    <TextBlock Style="{StaticResource SubheaderTextStyle}">
                        <Run FontWeight="ExtraBold" FontStyle="Italic">Head First C#</Run>
                        presents...
                    </TextBlock>

                    <TextBlock Grid.Row="1" Text="Invaders" HorizontalAlignment="Center" 
                   FontFamily="Arial Narrow" FontSize="50"
                   Foreground="Yellow" FontWeight="ExtraBold" FontStyle="Italic" Margin="10"
                   />
                    
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource SubheaderTextStyle}" FontWeight="Bold">Learn to build this app!</TextBlock>
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource SubheaderTextStyle}" Margin="0,10,0,10">
                        <Run>Learing C# is fun, and it's easier than you think. Learn more here:</Run>
                    </TextBlock>

                    <HyperlinkButton Margin="0,10,0,10"
                        Content="http://www.headfirstlabs.com/hfcsharp" NavigateUri="http://www.headfirstlabs.com/hfcsharp" Style="{StaticResource TextPrimaryButtonStyle}" FontSize="16"/>
                </StackPanel>
            </StackPanel>
        </Popup>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <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>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <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>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
