using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TreeViewDNDWithRia.Web;
using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;
using System.ComponentModel;
using Telerik.Windows.Controls;
using System.Collections.Specialized;
using System.Linq;

namespace TreeViewDNDWithRia.ViewModels
{
    public class CategoryViewModel : ViewModelBase
    {
        public CategoryViewModel(Category category)
        {
            this.categoryModel = category;
        }

        private DomainServiceNorthwind context;
        private Category categoryModel;
        public Category CategoryModel
        {
            get 
            {
                return categoryModel;
            }
        }
        public string Name
        {
            get 
            {
                return categoryModel.CategoryName;
            }
            set 
            {
                if (categoryModel.CategoryName != value)
                {
                    categoryModel.CategoryName = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        private ObservableCollection<ProductViewModel> products;
        public ObservableCollection<ProductViewModel> Products
        {
            get 
            {
                if (products == null)
                {
                    context = new DomainServiceNorthwind();
                    context.Load<Product>(context.GetProductsQuery(), lo =>
                    {
                        products = new ObservableCollection<ProductViewModel>(context.Products.
                            Where(x => x.CategoryID == categoryModel.CategoryID).
                            Select(y => new ProductViewModel(y)));     
               
                        OnPropertyChanged("Products");
                        products.CollectionChanged += new NotifyCollectionChangedEventHandler(Products_CollectionChanged);
                    }, null);
                }
                return products;
            }
        }

        void Products_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {       
            if (e.Action == NotifyCollectionChangedAction.Add )
            {
                ProductViewModel product = e.NewItems[0] as ProductViewModel;
                var oldCatId = product.ProductModel.CategoryID;
                if (oldCatId != null && oldCatId != this.categoryModel.CategoryID)
                {
                    Product productFFromBase = context.Products.
                        Where(x => x.ProductID == product.ProductModel.ProductID).FirstOrDefault();
                    productFFromBase.CategoryID = this.categoryModel.CategoryID;
                    context.SubmitChanges();
                }
            }
        }
    }
}
