using System.Linq;
using System.Collections;
using Telerik.Windows.Data;
using SilverlightApplication1.ServiceReference1;
using System.ComponentModel;

namespace SilverlightApplication1
{
    public class MyDataContext
    {
        NorthwindServiceSoapClient _Client;
        public NorthwindServiceSoapClient Client
        {
            get
            {
                if (_Client == null)
                {
                    _Client = new NorthwindServiceSoapClient();
                }

                return _Client;
            }
        }

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

                    _Data = new MyQueryableCollectionView(collection);
                    _Data.PageSize = 10;
                    _Data.MoveToFirstPage();

                    Client.GetCustomersCompleted += (s, args) =>
                    {
                        collection.SuspendNotifications();
                        collection.Clear();
                        collection.AddRange(args.Result.Data);
                        collection.ResumeNotifications();

                        _Data.SetIsLoading(false);

                        _Data.Refresh();

                        _Data.SetTotalItemsCount(args.Result.Count);

                        _Data.CollectionChanged += CollectionChanged;
                    };

                    Refresh();
                }

                return _Data;
            }
        }

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

            _Data.SetIsLoading(true);

            Refresh();
        }

        void Refresh()
        {
            Client.GetCustomersAsync(_Data.PageIndex * _Data.PageSize, _Data.PageSize,
                _Data.SortDescriptors.ToDynamicLinq(),
                _Data.FilterDescriptors.ToDynamicLinq(),
                _Data.GroupDescriptors.ToDynamicLinq());
        }
    }

    public class MyQueryableCollectionView : QueryableCollectionView
    {
		private int itemCount;

        public MyQueryableCollectionView(IEnumerable source)
            : base(source)
        { 


        }

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

        internal void SetTotalItemsCount(int count)
        {
            this.itemCount = count;

			this.OnPropertyChanged("ItemCount");
			this.OnPropertyChanged("TotalItemCount");
		}

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

		public override int ItemCount
		{
			get
			{
				return this.itemCount;
			}
		}

		public override int TotalItemCount
		{
			get
			{
				return this.itemCount;
			}
			protected set
			{
				// ...
			}
		}

		private void OnPropertyChanged(string propertyName)
		{
			base.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
		}
    }
}
