using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using BeWo.Report;
using BeWo.Data.Entities;
using BeWo.Data.Access;
using BeWo.Report.ReportObjects;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.Plugins;
using BeWo.Service.ServiceImplementations;
using BS.Shared.Core;
using BS.Shared;
using BS.Shared.Services;


namespace BeWo.WegweiserBetreuungsdienstReports
{
	public class WegweiserAuslastungBerechnung : AuslastungBerechnung
	{
		
		
		public override void CreateEmployeeDetails(MitarbeiterauslastungRO ro)
		{
			foreach (var empDetail in ro.EmployeeDetailList)
			{
				var emp = empDetail.Employee;

				
				Contract contract = emp.LastContract;
				if (contract != null && contract.WeeklyFLS.HasValue)
				{
					empDetail.WeeklyTotalHoursByContract = contract.WeeklyTotalHours ?? 0;
					empDetail.FLSTotalHoursByContract = contract.WeeklyFLS ?? 0;
				}

				var span = new DateTimeSpan();
				span.StartDateTime = ro.ReportStartDate;
				span.EndDateTime = ro.ReportEndDate;

				//supportconcepts by costbearer2supportconceptoid
				Dictionary<long, MitarbeiterauslastungRO.SupportConceptDetail> supportConceptDict = new Dictionary<long, MitarbeiterauslastungRO.SupportConceptDetail>();
				if (emp.Employee2CustomerList != null)
				{
					foreach (Employee2Customer e2c in emp.Employee2CustomerList)
					{
						bool isMainAttendant = false;
						bool isAssistenzkraft = false;
						if (e2c.ValueList.Count > 0)
						{
							SystemEntryID? id = e2c.ValueList[0].Entry.SystemEntryID;
							if (id.HasValue && id.Value == SystemEntryID.EmployeeRoleMainAttendant)
								isMainAttendant = true;
							else if (e2c.ValueList[0].Entry.Value.ToLower().Contains("hauswirtschaft"))
							{
								isAssistenzkraft = true;
							}
						}
						//if (isMainAttendant && e2c.Customer != null)
						if (e2c.Customer != null && (isMainAttendant || isAssistenzkraft))
						{
							if (e2c.Customer.IsActive == ActivationTypeId.Active && e2c.Customer.SupportConcepts != null)
							{
								foreach (SupportConcept sc in e2c.Customer.SupportConcepts)
								{
									if (sc.IsActive == ActivationTypeId.Active && sc.CostBearer2SupportConceptList != null)
									{
										foreach (CostBearer2SupportConcept cb2sc in sc.CostBearer2SupportConceptList)
										{
											bool inTimeRange = true;

											if (cb2sc.StartDate.HasValue && cb2sc.EndDate.HasValue)
											{
												if (cb2sc.StartDate >= ro.ReportEndDate || cb2sc.EndDate < ro.ReportStartDate)
												{
													inTimeRange = false;
												}
											}
											
											if (inTimeRange && !supportConceptDict.ContainsKey(cb2sc.Oid.Value))
											{
												String org = cb2sc.CostBearer.Organisation.Name;

												if ((org == "LVR II" && isAssistenzkraft) || (org != "LVR II") && isMainAttendant)
												{
													//if (scStartDate == null || scEndDate == null || ())
													MitarbeiterauslastungRO.SupportConceptDetail scDC = new MitarbeiterauslastungRO.SupportConceptDetail();

													supportConceptDict.Add(cb2sc.Oid.Value, scDC);
													scDC.Customer = e2c.Customer;
													scDC.CostBearer2SupportConcept = cb2sc;

													DateTime end = ro.ReportEndDate;
													if (cb2sc.EndDate.HasValue && cb2sc.EndDate.Value < end)
													{
														end = cb2sc.EndDate.Value;
													}
													CalculateSoll(scDC, end);

													empDetail.SupportConceptDetailList.Add(scDC);	
												}
												
											}
										}
									}
								}
							}
						}
					}
				}


				empDetail.SupportConceptDetailList.Sort((a, b) => (a.Customer.Person.LastName + ", " + a.Customer.Person.FirstName).CompareTo(b.Customer.Person.LastName + ", " + b.Customer.Person.FirstName));

			}

		}


	}
}
