using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using C1.Silverlight.Extended;

namespace C1Extended_Demo
{
    public partial class SinglePage : UserControl
    {
        #region Object Model

        public bool ShowInnerShadows 
        { 
            get 
            { 
                return book.ShowInnerShadows;
            }
            set
            {
                book.ShowInnerShadows = value;
            }
        }

        public bool ShowOuterShadows 
        { 
            get 
            { 
                return book.ShowOuterShadows;
            }
            set
            {
                book.ShowOuterShadows = value;
            }
        }

        public Orientation Orientation
        {
            get
            {
                return book.Orientation;
            }
            set
            {
                book.Orientation = value;
            }
        }

        public bool IsFirstPageOnTheRight 
        { 
            get 
            { 
                return book.IsFirstPageOnTheRight;
            }
            set
            {
                book.IsFirstPageOnTheRight = value;
            }
        }

        public PageFoldVisibility ShowPageFold 
        { 
            get 
            { 
                return book.ShowPageFold;
            }
            set
            {
                book.ShowPageFold = value;
            }
        }

        public PageFoldAction PageFoldAction 
        { 
            get 
            { 
                return book.PageFoldAction;
            }
            set
            {
                book.PageFoldAction = value;
            }
        }

        public double FoldSize 
        { 
            get 
            { 
                return book.FoldSize;
            }
            set
            {
                book.FoldSize = value;
            }
        }

        public int CurrentPage
        { 
            get 
            { 
                return book.CurrentPage;
            }
            set
            {
                book.CurrentPage = value;
            }
        }

        public event EventHandler CurrentPageChanged;

        #endregion

        public SinglePage()
        {
            InitializeComponent();

            // propagate events
            book.CurrentPageChanged += (s, a) =>
            {
                if (CurrentPageChanged != null) CurrentPageChanged(s, a);
            };

            // load book descriptions from xml
            XDocument doc = XDocument.Load(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("C1Extended_Demo.Resources.book.Amazon.xml")));
            var books = from reader in doc.Descendants("book")
                        select new Book
                        {
                            Title = reader.Attribute("title").Value,
                            CoverUri = reader.Attribute("coverUri").Value,
                            Id = reader.Attribute("id").Value,
                            Price = reader.Attribute("price").Value,
                            StockAmount = int.Parse(reader.Attribute("stockAmount").Value)
                        };

            // set the book's item source
            book.ItemsSource = books;
        }

        private void Backward_Click(object sender, RoutedEventArgs e)
        {
            this.book.TurnPage(false);
        }

        private void Forward_Click(object sender, RoutedEventArgs e)
        {
            this.book.TurnPage(true);
        }

        private void Vertical_Checked(object sender, RoutedEventArgs e)
        {
            this.book.Orientation = Orientation.Vertical;
            this.book.Width = this.book.RenderSize.Height;
            this.book.Height = this.book.RenderSize.Width;
        }

        private void Vertical_Unchecked(object sender, RoutedEventArgs e)
        {
            this.book.Orientation = Orientation.Horizontal;
            this.book.Width = this.book.RenderSize.Height;
            this.book.Height = this.book.RenderSize.Width;
        }
    }
}
