﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotKana.Model;
using System.ComponentModel;

namespace DotKana.ViewModels
{
    public class StatisticsViewModel : INotifyPropertyChanged
    {
        #region Private Members

        private static readonly int _commonMistakesCount = 10;
        private Statistics _statistics;

        #endregion

        #region Properties

        private const string _commonMistakesPropertyName = "CommonMistakes";
        public List<Statistic> CommonMistakes
        {
            get { return _statistics.UserStatistics.Values.ToList().OrderByDescending(o => o.Fails).ToList().Take(_commonMistakesCount).ToList(); }
        }

        private const string _allStatisticsPropertyName = "AllStatistics";
        public List<Statistic> AllStatistics
        {
            get { return _statistics.UserStatistics.Values.ToList(); }
        }

        #endregion

        #region Constructor / Events handler

        public StatisticsViewModel()
        {
            _statistics = Statistics.Instance;
            _statistics.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Statistics_PropertyChanged);
        }

        private void Statistics_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            RaisePropertyChanged(_commonMistakesPropertyName);
            RaisePropertyChanged(_allStatisticsPropertyName);
        }

        #endregion        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
