using System;
using System.Collections.Generic;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.Plugins;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using BS.Shared.Services;

namespace FizHof.Statistics
{
	public class FizHofServiceRecordStatisticFactory : ServiceRecordStatisticFactory
	{
		public override ServiceRecordStatisticsInfoDC GetServiceRecordStatisticInfo(long costBearer2SupportConceptOid, DateTime statisticsDate)
		{
			var stats = CreateSupportConceptStatisticsImpl(costBearer2SupportConceptOid, statisticsDate);

			var dc = new ServiceRecordStatisticsInfoDC();
			dc.Header = new string[4];
			dc.Header[0] = "Bewilligungszeitraum:";
			dc.Header[1] = "Geleistet diese Woche/Gesamt:";
			dc.Header[2] = "Bewilligt pro Woche/Gesamt:";
			dc.Header[3] = "Geleistet pro Woche(Ø)/Frei Gesamt:";
            
            dc.ForegroundColor = new string[4];
			dc.ForegroundColor[0] = "#FF2B508B";
			dc.ForegroundColor[1] = "#FF2B508B";
			dc.ForegroundColor[2] = "#FF2B508B";
			dc.ForegroundColor[3] = "#FF2B508B";
			
			dc.PeriodStatisticInfos = new List<ServiceRecordPeriodStatisticsInfoDC>();


            if (stats != null)
            {
                if (stats.PeriodStatistics.Count > 1 && !TimeSpanIsEqual(stats))
                    dc.PeriodStatisticInfos.Add(CreateTotalStatistics(stats, statisticsDate));

                foreach (var pdc in stats.PeriodStatistics)
                {
                    dc.PeriodStatisticInfos.Add(CreateServiceRecordPeriodStatisticsInfo(stats, pdc, statisticsDate));
                }
            }

            return dc;
        }

        public ServiceRecordPeriodStatisticsInfoDC CreateTotalStatistics(SupportConceptStatisticsDC stats, DateTime statisticsDate)
        {
            var dc = CreateDefaultServiceRecordPeriodStatisticsInfo(4);
            dc.LeftValues[0] = "Gesamt";

            dc.LeftValues[1] = String.Format("{0:0.00}", stats.MinutesProvidedThisWeek / 60);
            dc.RightValues[1] = String.Format("{0:0.00}", stats.MinutesProvidedTotalRounded / 60);

            dc.LeftValues[2] = String.Format("{0:0.00}", stats.MinutesApprovedPerWeek / 60);
            dc.RightValues[2] = String.Format("{0:0.00}", stats.MinutesApprovedTotal / 60);

            var minutes = stats.MinutesProvidedTotal;

            if (statisticsDate > stats.StatisticsEndDate)
                statisticsDate = stats.StatisticsEndDate;

            var days = Math.Ceiling(statisticsDate.Date.Subtract(stats.StatisticsStartDate).TotalDays) + 1;
            if (days <= 0)
            {
                days = 1;
            }
            var wochenDurchschnitt = ((minutes*7)/(decimal) days)/60;

            dc.LeftValues[3] = String.Format("{0:0.00}", wochenDurchschnitt);
            dc.RightValues[3] = String.Format("{0:0.00}", (stats.MinutesApprovedTotal - stats.MinutesProvidedTotalRounded) / 60);
            
            return dc;
        }

        public ServiceRecordPeriodStatisticsInfoDC CreateServiceRecordPeriodStatisticsInfo(SupportConceptStatisticsDC stats, SupportConceptPeriodStatisticsDC periodStats, DateTime statisticsDate)
        {
            var dc = CreateDefaultServiceRecordPeriodStatisticsInfo(4);

            if (periodStats.SupportConceptApprovalPeriod != null)
            {
                if (periodStats.SupportConceptApprovalPeriod.ServiceCategory != null)
                {
                    if (periodStats.SupportConceptApprovalPeriod.StartDate.HasValue && periodStats.SupportConceptApprovalPeriod.EndDate.HasValue &&
                        periodStats.SupportConceptApprovalPeriod.StartDate.Value.Date == stats.StatisticsStartDate.Date &&
                        periodStats.SupportConceptApprovalPeriod.EndDate.Value.Date == stats.StatisticsEndDate.Date)
                    {
                        dc.LeftValues[0] = periodStats.SupportConceptApprovalPeriod.ServiceCategory.Name;
                    }
                    else
                    {
                        dc.LeftValues[0] = String.Format("{0}: {1:dd.MM.yy} - {2:dd.MM.yy}",
                                                       periodStats.SupportConceptApprovalPeriod.ServiceCategory.Name,
                                                       periodStats.SupportConceptApprovalPeriod.StartDate,
                                                       periodStats.SupportConceptApprovalPeriod.EndDate);
                    }
                }
                else
                {
                    dc.LeftValues[0] = String.Format("{0:dd.MM.yy} - {1:dd.MM.yy}",
                                                   periodStats.SupportConceptApprovalPeriod.StartDate,
                                                   periodStats.SupportConceptApprovalPeriod.EndDate);
                }
            }

            dc.LeftValues[1] = String.Format("{0:0.00}", periodStats.MinutesProvidedThisWeek / 60);
            dc.RightValues[1] = String.Format("{0:0.00}", periodStats.MinutesProvidedTotalRounded / 60);

            dc.LeftValues[2] = String.Format("{0:0.00}", periodStats.MinutesApprovedPerWeek / 60);
            dc.RightValues[2] = String.Format("{0:0.00}", periodStats.MinutesApprovedTotal / 60);

            var minutes = periodStats.MinutesProvidedTotal;
            double days = 1;

            if (periodStats.SupportConceptApprovalPeriod != null)
            {

                if (statisticsDate > periodStats.SupportConceptApprovalPeriod.EndDate)
                    statisticsDate = periodStats.SupportConceptApprovalPeriod.EndDate.Value;

                var ts = (statisticsDate.Date - periodStats.SupportConceptApprovalPeriod.StartDate);
                if (ts.HasValue)
                    days = Math.Ceiling(ts.Value.TotalDays) + 1;

            }
            if (days <= 0)
            {
                days = 1;
            }
            var wochenDurchschnitt = ((minutes * 7) / (decimal)days) / 60;

            dc.LeftValues[3] = String.Format("{0:0.00}", wochenDurchschnitt);
            dc.RightValues[3] = String.Format("{0:0.00}", (periodStats.MinutesApprovedTotal - periodStats.MinutesProvidedTotalRounded) / 60);
            
            return dc;
        }

        public override SupportConceptStatisticsDC CreateSupportConceptStatistics(long costBearer2SupportConceptOid, DateTime statisticsDate)
        {
            return null;
        }
    }
}
