using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data.Services.Client;
using C1.Silverlight.FlexGrid;
using MultiColumnCombo.NorthWindServiceReference;

namespace MultiColumnCombo
{
    /// <summary>
    /// Provides NorthWind data loaded from the standard NorthWind OData provider.
    /// </summary>
    public class ViewModel
    {
        // ** fields
        static NorthwindEntities _ctx;
        CollectionViewSource _categories, _products, _suppliers;

        // ** ctor
        public ViewModel()
        {
            if (_ctx == null)
            {
                var url = new Uri("http://services.odata.org/Northwind/Northwind.svc/", UriKind.Absolute);
                _ctx = new NorthwindEntities(url);
            }
            LoadData();
        }

        // ** object model
        public ICollectionView Categories
        {
            get { return _categories.View; }
        }
        public ICollectionView Products
        {
            get { return _products.View; }
        }
        public ICollectionView Suppliers
        {
            get { return _suppliers.View; }
        }

        // ** implementation
        void LoadData()
        {
            // load product categories
            var categories = new DataServiceCollection<Category>();
            categories.LoadCompleted += (s, ee) =>
                {
                    if (categories.Continuation != null)
                        categories.LoadNextPartialSetAsync();
                };
            categories.LoadAsync(_ctx.Categories);
            _categories = new CollectionViewSource();
            _categories.Source = categories;

            // load products
            var products = new DataServiceCollection<Product>();
            products.LoadCompleted += (s, ee) =>
            {
                if (products.Continuation != null)
                    products.LoadNextPartialSetAsync();
            };
            products.LoadAsync(_ctx.Products);
            _products = new CollectionViewSource();
            _products.Source = products;

            // load suppliers
            var suppliers = new DataServiceCollection<Supplier>();
            suppliers.LoadCompleted += (s, ee) =>
            {
                if (suppliers.Continuation != null)
                    suppliers.LoadNextPartialSetAsync();
            };
            suppliers.LoadAsync(_ctx.Suppliers);
            _suppliers = new CollectionViewSource();
            _suppliers.Source = suppliers;
        }
    }
    /// <summary>
    /// ColumnConverter for product category column.
    /// </summary>
    public class CategoryConverter : ColumnValueConverter
    {
        public CategoryConverter()
        {
            var vm = new ViewModel();
            base.SetSource(vm.Categories, "CategoryID", "CategoryName");
        }
    }
    /// <summary>
    /// ColumnConverter for supplier column.
    /// </summary>
    public class SupplierConverter : ColumnValueConverter
    {
        public SupplierConverter()
        {
            var vm = new ViewModel();
            base.SetSource(vm.Suppliers, "SupplierID", "CompanyName");
        }
    }
}
