using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using Telerik.Windows.Controls;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MyViewModel();
        }
    }

    public class Club : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string name;
        private DateTime established;
        private int stadiumCapacity;

        public string Name
        {
            get { return this.name; }
            set
            {
                if (value != this.name)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }

        public DateTime Established
        {
            get { return this.established; }
            set
            {
                if (value != this.established)
                {
                    this.established = value;
                    this.OnPropertyChanged("Established");
                }
            }
        }

        public int StadiumCapacity
        {
            get { return this.stadiumCapacity; }
            set
            {
                if (value != this.stadiumCapacity)
                {
                    this.stadiumCapacity = value;
                    this.OnPropertyChanged("StadiumCapacity");
                }
            }
        }

        public Club(string name, DateTime established, int stadiumCapacity)
        {
            this.name = name;
            this.established = established;
            this.stadiumCapacity = stadiumCapacity;
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, args);
            }
        }

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

    public class MyViewModel : ViewModelBase
    {
        private ObservableCollection<Club> clubs;
        private DelegateCommand pageIndexChangedCommand;
        private int itemCount;
        private int pageSize;
        private List<Club> pagedClubs;

        public MyViewModel()
        {
            this.PageIndexChangedCommand = new DelegateCommand(OnPageIndexChanged);
            this.ItemCount = this.Clubs.Count;
            this.PageSize = 5;

            this.PagedClubs = this.Clubs.Take(this.PageSize).ToList();
        }

        public ObservableCollection<Club> Clubs
        {
            get
            {
                if (this.clubs == null)
                {
                    this.clubs = this.CreateClubs();
                }

                return this.clubs;
            }
        }

        public List<Club> PagedClubs
        {
            get
            {
                return this.pagedClubs;
            }
            set
            {
                if (this.pagedClubs != value)
                {
                    this.pagedClubs = value;
                    this.OnPropertyChanged("PagedClubs");
                }
            }
        }

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

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

        public DelegateCommand PageIndexChangedCommand
        {
            get
            {
                return this.pageIndexChangedCommand;
            }
            set
            {
                if (this.pageIndexChangedCommand != value)
                {
                    this.pageIndexChangedCommand = value;
                }
            }
        }

        private void OnPageIndexChanged(object obj)
        {
            var e = obj as PageIndexChangedEventArgs;
            this.PagedClubs = this.Clubs.Skip(e.NewPageIndex * this.PageSize).Take(this.PageSize).ToList();
        }

        private ObservableCollection<Club> CreateClubs()
        {
            ObservableCollection<Club> clubs = new ObservableCollection<Club>();

            for (int i = 0; i < 500; i++)
            {
                clubs.Add(new Club("Club " + i, DateTime.Now.AddDays(i), i));
            }

            return clubs;
        }
    }
}
