using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace OData
{
    using OData.NorthwindServiceReference1;
    using System.Data.Services.Client;
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public partial class MainPage : Page
    {
        static Uri _svcRoot = new Uri("http://services.odata.org/Northwind/Northwind.svc/");
        NorthwindEntities _context = new NorthwindEntities(_svcRoot);
        // Instantiate the context.
        //DataServiceContext oDataContext = new DataServiceContext(new Uri("http://services.odata.org/Northwind/Northwind.svc/"));
        //DataServiceCollection<Category> objectListFromService = new DataServiceCollection<Category>(oDataContext);

        public MainPage()
        {
            this.InitializeComponent();

             //bind top grid to categories
            var categories = new DataServiceCollection<Category>();
            _flexCategories.ItemsSource = categories;

            // when the user selects a category, load corresponding products
            _flexCategories.SelectionChanged += _flexCategories_SelectionChanged;

            // show progress ring
            categoryLoader.IsActive = true;

             //load categories
            categories.LoadCompleted += (s, ee) =>
            {
                if (categories.Continuation != null)
                {
                    categories.LoadNextPartialSetAsync();
                }
                else
                {
                    categoryLoader.IsActive = false;
                    categoryLoader.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
            };
            categories.LoadAsync(_context.Categories);
           
        }

        void _flexCategories_SelectionChanged(object sender, C1.Xaml.FlexGrid.CellRangeEventArgs e)
        {
            // get selected category
            var category = _flexCategories.SelectedItem as Category;
            if (category != null)
            {
                // build query to select products in the selected category
                var query = from p in _context.Products
                            where p.CategoryID == category.CategoryID
                            orderby p.ProductName
                            select p;

                // assign new products collection to grid
                var products = new DataServiceCollection<Product>();
                _flexProducts.ItemsSource = products;

                // show progress ring
                productLoader.IsActive = true;
                productLoader.Visibility = Windows.UI.Xaml.Visibility.Visible;

                // load products in the selected category
                products.LoadAsync(query);
                products.LoadCompleted += (s, ee) =>
                {
                    if (products.Continuation != null)
                    {
                        products.LoadNextPartialSetAsync();
                    }
                    else
                    {
                        productLoader.IsActive = false;
                        productLoader.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    }
                };
            }
        }

        
        /// <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)
        {
        }
    }
}
