using ExtendedSamples.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
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 User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace ExtendedSamples.Samples
{
    public sealed partial class C1BookDemo : UserControl
    {
        public C1BookDemo()
        {
            this.InitializeComponent();
            this.InitDataSource();
        }

        private void InitDataSource()
        {
            // load book descriptions from xml
            Assembly assembly = typeof(C1BookDemo).GetTypeInfo().Assembly;
            XDocument doc = XDocument.Load(new StreamReader(assembly.GetManifestResourceStream("ExtendedSamples.Resources.Amazon.xml")));
            
            var books = from reader in doc.Descendants("book")
                        select new AmazonBookDescription
                        {
                            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;
        }
    }
}
