using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace RGV_WPF_AggregateSelectedRows
{
    public class MyViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ObservableCollection<Club> clubs;
        private ObservableCollection<Player> players;
        private object selectedItem;

        private double sum;

        public double Sum
        {
            get { return sum; }
            set
            {
                sum = value;
                this.OnPropertyChanged("Sum");
            }
        }

        private double average;

        public double Average
        {
            get { return average; }
            set
            {
                average = value;
                this.OnPropertyChanged("Average");
            }
        }

        private ObservableCollection<object> selectedItems;

        public ObservableCollection<object> SelectedItems
        {
            get { return selectedItems; }
            set
            {
                selectedItems = value;
                selectedItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(selectedItems_CollectionChanged);
            }
        }

        void selectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //Update aggregate results.

            if (SelectedItems.Count != 0)
            {
                Sum = SelectedItems.OfType<Player>().Select(c => c.Number).Sum();
                Average = SelectedItems.OfType<Player>().Select(c => c.Number).Average();
            }
            else
            {
                Sum = 0;
                Average = 0;
            }
        }


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

                return this.clubs;
            }
        }

        public ObservableCollection<Player> Players
        {
            get
            {
                if (this.players == null)
                {
                    this.players = Player.GetPlayers();
                }

                return this.players;
            }
        }

        public object SelectedItem
        {
            get { return this.selectedItem; }
            set
            {
                if (value != this.selectedItem)
                {
                    this.selectedItem = value;
                    this.OnPropertyChanged("SelectedItem");
                }
            }
        }

        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));
        }
    }
}
