using System.Linq;
using System.Collections;
using Telerik.Windows.Data;
using WpfBrowserApplication1.ServiceReference1;
using Telerik.Windows.Controls;
//using WpfBrowserApplication1.NorthwindService;

namespace SilverlightApplication1
{
    public class MyDataContext : ViewModelBase
    {
		public MyDataContext()
		{
			this.PageSize = 10;
			this.PageIndex = 0;
		}

		private int itemCount;

		public int ItemCount
		{
			get
			{
				return itemCount;
			}
			set
			{
				itemCount = value;
				this.OnPropertyChanged("ItemCount");
			}
		}

		private int pageSize;

		public int PageSize
		{
			get
			{
				return pageSize;
			}
			set
			{
				pageSize = value;
				this.OnPropertyChanged("PageSize");
			}
		}

		private int pageIndex;

		public int PageIndex
		{
			get
			{
				return pageIndex;
			}
			set
			{
				pageIndex = value;
				this.OnPropertyChanged("PageIndex");

				if (this.collection != null)
				{
					this.Refresh();
				}
			}
		}

		Service1Client _Client;
		public Service1Client Client
		{
			get
			{
				if (_Client == null)
				{
					_Client = new Service1Client();
				}

				return _Client;
			}
		}



		RadObservableCollection<Customer> collection;
		MyQueryableCollectionView _Data;
		public MyQueryableCollectionView Data
		{
			get
			{
				if (_Data == null)
				{
					this.collection = new RadObservableCollection<Customer>();

					_Data = new MyQueryableCollectionView(collection);
					
					_Data.PageSize = this.PageSize;
					_Data.MoveToFirstPage();
					_Data.SortDescriptors.Add(new SortDescriptor() { Member = "CompanyName" });
					
					Refresh();
				}

				return _Data;
			}
		}

		void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			_Data.CollectionChanged -= CollectionChanged;
			_Data.SetIsLoading(true);

			Refresh();
		}

		void Refresh()
		{
			_Data.CollectionChanged -= CollectionChanged;

			var d = Client.GetCustomers(this.PageIndex * this.PageSize, this.PageSize,
				_Data.SortDescriptors.ToDynamicLinq(),
				_Data.FilterDescriptors.ToDynamicLinq(),
				_Data.GroupDescriptors.ToDynamicLinq());

			if (collection != null)
			{
				collection.SuspendNotifications();
				collection.Clear();
				collection.AddRange(d.Data);
				collection.ResumeNotifications();

				_Data.SetIsLoading(false);
				_Data.SetTotalItemsCount(d.Count);
				this.ItemCount = d.Count;
				_Data.CollectionChanged += CollectionChanged;
			}

		}
    }

    public class MyQueryableCollectionView : QueryableCollectionView
    {
        public MyQueryableCollectionView(IEnumerable source)
            : base(source)
        { }

        protected override IQueryable CreateView()
        {
            return IsGrouped ? QueryableSourceCollection.GroupBy(GroupDescriptors) : QueryableSourceCollection;
        }

        internal void SetTotalItemsCount(int count)
        {
            TotalItemCount = count;
        }

        internal void SetIsLoading(bool value)
        {
            IsLoading = value;
        }
    }
}
