using System;
using System.Linq;
using System.Windows;
using Telerik.Windows.Data;
using System.Collections;
using System.ComponentModel;
using System.Collections.Specialized;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new MyDataContext();
        }
    }

    public class MyDataContext
    {
        MyPagedCollectionView _View;
        public MyPagedCollectionView View
        {
            get
            {
                if (_View == null)
                {
                    _View = new MyPagedCollectionView() { PageSize = 10 };
                    _View.SetCount(25000000);
                }

                return _View;
            }
        }
    }

    public class MyPagedCollectionView : IPagedCollectionView, IEnumerable, INotifyPropertyChanged, INotifyCollectionChanged
    {
        public void SetCount(int count)
        {
            this._Count = count;
        }

        #region IPagedCollectionView Members

        public bool CanChangePage
        {
            get { return true; }
        }

        bool _IsPageChanging;
        public bool IsPageChanging
        {
            get { return _IsPageChanging; }
        }

        int _Count;
        public int ItemCount
        {
            get { return _Count; }
        }

        public bool MoveToFirstPage()
        {
            return MoveToPage(0);
        }

        public bool MoveToLastPage()
        {
            var page = PageCount - 1;
            return MoveToPage(page);
        }

        public bool MoveToNextPage()
        {
            var page = _PageIndex + 1;
            return MoveToPage(page);
        }

        public bool MoveToPage(int pageIndex)
        {
            if ((pageIndex <= (PageCount - 1)) && pageIndex >= 0)
            {
                _PageIndex = pageIndex;

                OnPropertyChanged("PageIndex");

                if (CollectionChanged != null)
                {
                    CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                }

                return true;
            }

            return false;
        }

        public bool MoveToPreviousPage()
        {
            var page = _PageIndex - 1;
            return MoveToPage(page);
        }

        public event EventHandler<EventArgs> PageChanged;

        public event EventHandler<PageChangingEventArgs> PageChanging;

        int _PageIndex;
        public int PageIndex
        {
            get { return _PageIndex; }
        }

        int _PageSize;
        public int PageSize
        {
            get
            {
                return _PageSize;
            }
            set
            {
                _PageSize = value;
            }
        }

        public int TotalItemCount
        {
            get { return _Count; }
        }

        #endregion


        public int PageCount
        {
            get { return TotalItemCount / PageSize; }
        }


        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return (from i in Enumerable.Range(PageIndex * PageSize, PageSize) select i).GetEnumerator();
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyCollectionChanged Members

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        #endregion
    }
}
