using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BS.Shared.Core;
using BS.Shared.Services;
using BeWo.Report.ReportObjects;
using BeWo.Report;

namespace BeWo.ABCBetreuungenReports
{
	public partial class Stundenzettel : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<ServicesOverviewRO>
	{
		public Stundenzettel()
		{
			InitializeComponent();
		}

		public void SetReportDataSource(ServicesOverviewRO pRO)
		{
			pRO.CalculateFLSSollIst();
			var test = pRO.FreeMinutesThisMonth;
			CalculateFLSSollIstThisMonth(pRO);
			test = pRO.FreeMinutesThisMonth;
			this.bindingSource1.DataSource = pRO;
		}



		public void CalculateFLSSollIstThisMonth(ServicesOverviewRO ro)
		{
			if (ro.SupportConceptCostBearer != null && ro.SupportConceptCostBearer.CostBearer2SupportConceptOid != null)
			{
				if (ro.SupportConceptCostBearer.StartDate != null)
				{
					CostBearer2SupportConcept c2s =
						DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(
							ro.SupportConceptCostBearer.CostBearer2SupportConceptOid.Value);

					var calc = Calculations.GetInstance(c2s.CostBearer.ID);
					DateTime startHilfeplan = ro.SupportConceptCostBearer.StartDate.Value;
					DateTime startAuswerteMonat = ro.StartDate;

					decimal flmSollLetztenMonat = 0;
					
					TimeSpan ts;
					if (startAuswerteMonat > startHilfeplan)
						ts = ro.EndDate.Subtract(startAuswerteMonat);
					else
						ts = ro.EndDate.Subtract(startHilfeplan);
					
					int daysPassed = ts.Days + 1;

					int abcenceDays = 0;

					
					ro.ApprovedFLSPerMonth = ro.ApprovedFLSPerWeek * 4.3m * 60m;
					daysPassed -= abcenceDays;
					if ((startHilfeplan.Day > 1 && startHilfeplan > startAuswerteMonat) || abcenceDays > 0) //Bin im erstenMonat && Hilfeplan fängt mitten im Monat an
					{
						ro.ApprovedFLSPerMonth = ro.ApprovedFLSPerWeek * (daysPassed / 7m) * 60m;

					}

					if (ro.ApprovedFLSPerMonth < 0)
						ro.ApprovedFLSPerMonth = 0;

					DateTime startIterator = startHilfeplan;
					while (startIterator < ro.EndDate)
					{
						decimal flsSollPerWeekThisMonth = calc.GetApprovedHoursPerWeek(ro.SupportConceptCostBearer, startIterator, false) ?? 0;

						decimal flmSollThisMonth = flsSollPerWeekThisMonth * 4.3m * 60m;

						DateTime endeDesMonats = startIterator.AddMonths(1);
						endeDesMonats = new DateTime(endeDesMonats.Year, endeDesMonats.Month, 1);
						daysPassed = endeDesMonats.Subtract(startIterator).Days;

						DateTime nextstart = startIterator.AddMonths(1);
						nextstart = new DateTime(nextstart.Year, nextstart.Month, 1);
						
						abcenceDays = 0;
						
						if (startIterator.Day > 1 || abcenceDays > 0) //Bin im erstenMonat && Hilfeplan fängt mitten im Monat an
						{
							flmSollThisMonth = flsSollPerWeekThisMonth * (daysPassed / 7m) * 60m;
						}

						if (startIterator < ro.StartDate)
							flmSollLetztenMonat += flmSollThisMonth;
						
						startIterator = nextstart;
					}



					// Berechne FLM Ist bis Ende des Monats vom Quittierungsbeleg
					IList<ServiceRecord> allServiceRecords = c2s.ServiceRecords;
					decimal minutenIst = 0;

					DateTime end = ro.EndDate.Date.AddDays(1);

					Dictionary<long, bool> groupBookingDict = new Dictionary<long, bool>();
					foreach (var item in allServiceRecords)
					{
						if (!item.GroupOid.HasValue || !groupBookingDict.ContainsKey(item.GroupOid.Value))
						{
							if (item.Start != null)
							{
								if (item.Start >= ro.SupportConceptCostBearer.StartDate.Value && item.Start.Value < end)
								{
									if (item.ServiceDescription != null &&
										item.ServiceDescription.ServiceCategory != null &&
										item.ServiceDescription.ServiceCategory.IsBillable)
									{
										minutenIst += (item.RoundedDuration *
													   item.ServiceDescription.ServiceCategory.Percentage) / 100;

										if (item.GroupOid.HasValue)
											groupBookingDict.Add(item.GroupOid.Value, true);
									}
								}
							}
						}
					}
					minutenIst *= ro.RateFactor;

					decimal minutenBisLetztenMonat = minutenIst - ((decimal)ro.TotalFLMBillable * ro.RateFactor);

					ro.FreeMinutesThisMonth = flmSollLetztenMonat - minutenBisLetztenMonat;
				}
			}

		}

	}
}
