using Client.DataStructures;
using Client.ISServiceRef;
using Client.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using LiveCharts;
using LiveCharts.Wpf;
using MoreLinq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using WpfAsyncPack.Command;

namespace Client.ViewModel
{
    public class TimeSheetStatisticsViewModel : ViewModelBase
    {

        public TimeSheetMonthlyData TimeSheetMonthlyData { get; set; }

        public double TotalHours { get; set; }
        public double LunchBreakHours { get; set; }
        public double VacationHours { get; set; }
        public double MedicalLeaveHours { get; set; }
        public double WorkHours { get; set; }

        public SeriesCollection Series { get; set; }

        public TimeSheetStatisticsViewModel(TimeSheetMonthlyData timeSheetMonthlyData)
        {
            TimeSheetMonthlyData = timeSheetMonthlyData;

            InitStatisticsChart();
        }


        private void InitStatisticsChart()
        {
            Series = new SeriesCollection();

            var projects = TimeSheetMonthlyData.TimeSheetRecords.DistinctBy(x => x.ProjectId).Select(x => x.Project);

            foreach(var project in projects)
            {
                var projectTimeSheets = TimeSheetMonthlyData.TimeSheetRecords.Where(x => x.ProjectId == project.Id);

                double hoursSpentOnProject = 0;
                foreach(var projectTimeSheet in projectTimeSheets)
                {
                    hoursSpentOnProject += (projectTimeSheet.To - projectTimeSheet.From).TotalHours;
                }

                var series = new PieSeries()
                {
                    Title = project.Name,
                    Values = new ChartValues<double>(new double[] { hoursSpentOnProject }),
                    DataLabels = true,
                    LabelPoint = _labelFunc,
                    Stroke = new SolidColorBrush(Color.FromArgb(255, 214, 223, 225))
                };

                Series.Add(series);
            }


        }


        private Func<ChartPoint, string> _labelFunc = (chartPoint) => { return string.Format("{0}", Math.Round(chartPoint.Y,1)); };

    }
}
