using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace C1Magazine
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        // keep page index between view states
        private int pageIndex = 0;

        public MainPage()
        {
            this.InitializeComponent();

            Window.Current.SizeChanged += Current_SizeChanged;

            Current_SizeChanged(null, null);
        }

        

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;
            if (btn != null)
            {
                //c1Book1.CurrentPage = int.Parse(btn.Tag.ToString());

                int pageNumber = int.Parse(btn.Tag.ToString());

                if (bookGrid.Visibility == Windows.UI.Xaml.Visibility.Visible)
                {
                    while (c1Book1.CurrentPage < pageNumber)
                    {
                        c1Book1.TurnPage(true);
                    }
                }
                if (listGrid.Visibility == Windows.UI.Xaml.Visibility.Visible)
                {
                    c1ListBox1.BringIndexIntoView(pageNumber);
                }
            }
        }

        private void btnTOC_Click(object sender, RoutedEventArgs e)
        {
            if (bookGrid.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                while (c1Book1.CurrentPage > 2)
                {
                    c1Book1.TurnPage(false);
                }

                if (c1Book1.CurrentPage < 1)
                {
                    c1Book1.TurnPage(true);
                }
            }
            else if (listGrid.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                c1ListBox1.BringIndexIntoView(2);
            }
        }

        private void btnPreviousPage_Click(object sender, RoutedEventArgs e)
        {
            c1Book1.TurnPage(false);
        }

        private void btnNextPage_Click(object sender, RoutedEventArgs e)
        {
            c1Book1.TurnPage(true);
        }

        private void c1Book1_CurrentPageChanged(object sender, C1.Xaml.PropertyChangedEventArgs<int> e)
        {
            btnPreviousPage.IsEnabled = c1Book1.CurrentPage > 0 ? true : false;
            btnNextPage.IsEnabled = c1Book1.CurrentPage < c1Book1.Items.Count - 1 ? true : false;
        }

        void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            switch (ApplicationView.Value)
            {
                case ApplicationViewState.Filled:
                    VisualStateManager.GoToState(this, "Filled", false);
                    break;

                case ApplicationViewState.FullScreenLandscape:
                    VisualStateManager.GoToState(this, "FullScreenLandscape", false);
                    break;

                case ApplicationViewState.Snapped:
                    VisualStateManager.GoToState(this, "Snapped", false);
                    break;

                case ApplicationViewState.FullScreenPortrait:
                    VisualStateManager.GoToState(this, "FullScreenPortrait", false);
                    break;

                default:
                    return;
            }
        }

    }
}
