using System;
using System.Linq;
using BS.Shared;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Data.Entities;
using BeWo.Data.Access;
using System.Collections.Generic;
using BS.Shared.Core;
using BS.Shared.Services;
using BS.Shared.DataContracts;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceImplementations;

namespace BeWo.DiakonieKKKleve
{
	public partial class Finanzauswertung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<FinanceRO>
    {
        public Finanzauswertung()
        {
            InitializeComponent();
        }

		public void SetReportDataSource(FinanceRO pRO)
        {
			if (pRO.ReportStartDate.HasValue && pRO.ReportEndDate.HasValue)
			{
				DateTime start = pRO.ReportStartDate.Value;
				DateTime end = pRO.ReportEndDate.Value;

				if (start.Year == end.Year && start.Month == end.Month && start.Day == 1 &&
				    end.Day == DateTime.DaysInMonth(end.Year, end.Month))
				{
					lblTitel.Text = String.Format("Finanzauswertung {0:MMMM yyyy}", start);
					
				}
				else
				{
					lblTitel.Text = String.Format("Finanzauswertung {0:dd.MM.yyyy}-{1:dd.MM.yyyy}", start, end);
					
				}

				if (pRO.SupportConceptFinanceList != null)
				{
					var roFls = new FinanceRO
					{
						ReportStartDate = pRO.ReportStartDate,
						ReportEndDate = pRO.ReportEndDate,
						RateFactor = pRO.RateFactor,
						SupportConceptFinanceList = new List<FinanceRO.SupportConceptFinanceRO>()
					};
					var roAls = new FinanceRO
					{
						ReportStartDate = pRO.ReportStartDate,
						ReportEndDate = pRO.ReportEndDate,
						RateFactor = pRO.RateFactor,
						SupportConceptFinanceList = new List<FinanceRO.SupportConceptFinanceRO>()
					};
					var roVP = new FinanceRO
					{
						ReportStartDate = pRO.ReportStartDate,
						ReportEndDate = pRO.ReportEndDate,
						RateFactor = pRO.RateFactor,
						SupportConceptFinanceList = new List<FinanceRO.SupportConceptFinanceRO>()
					};

					Dictionary<String, FinanceRO.SupportConceptFinanceRO> cb2Sc2FinanceRo = new Dictionary<String, FinanceRO.SupportConceptFinanceRO>();
					foreach (var f in pRO.SupportConceptFinanceList)
					{
						String key = String.Format("{0}_{1}_{2}", f.CustomerName, f.SupportConceptTimeSpanString, f.CostBearerName);

						bool isVP = false;

						if (f.ServiceRecords != null)
						{
							foreach (var sr in f.ServiceRecords)
							{
								if (sr.ServiceDescription.CategoryName == "Verhinderungspflege" || sr.ServiceDescription.CategoryName.Contains("Betreuungsleistungen"))
								{
									FinanceRO.SupportConceptFinanceRO vpFinanceRo = null;
									if (cb2Sc2FinanceRo.ContainsKey(key))
									{
										vpFinanceRo = cb2Sc2FinanceRo[key];
									}
									else
									{
										vpFinanceRo = new FinanceRO.SupportConceptFinanceRO();
										vpFinanceRo.BillableHours = 0;
										vpFinanceRo.CostBearerName = f.CostBearerName;
										vpFinanceRo.CustomerName = f.CustomerName;
										vpFinanceRo.SupportConceptTimeSpanString = f.SupportConceptTimeSpanString;
										cb2Sc2FinanceRo.Add(key, vpFinanceRo);

										roVP.SupportConceptFinanceList.Add(vpFinanceRo);
										isVP = true;
									}

									decimal hours = sr.RoundedDuration / 60m;
									vpFinanceRo.BillableHours += hours;

									f.BillableHours -= hours;

									if (sr.Start >= start && sr.Start < end.AddDays(1))
									{
										vpFinanceRo.BillableHoursInReportTimeSpan += hours;
										f.BillableHoursInReportTimeSpan -= hours;
										if (f.RateFactor.HasValue)
											f.BillableHoursInReportTimeSpanWithFactor -= hours*f.RateFactor.Value;
										else
											f.BillableHoursInReportTimeSpanWithFactor -= hours;
									}

								}
							}
						}
						if (!isVP)
						{
							int test = 0;
						}
						if (f.RateFactor.HasValue && f.RateFactor > 1)
							{
								roFls.SupportConceptFinanceList.Add(f);
							}
							else
							{
								roAls.SupportConceptFinanceList.Add(f);
							}
						//}
					}

					var flsReport = subReportFls.ReportSource as FinanzauswertungFls;
					flsReport.SetReportDataSource(roFls);

					var alsReport = subReportAls.ReportSource as FinanzauswertungAls;
					alsReport.SetReportDataSource(roAls);

					var vpReport = subReportPflege.ReportSource as FinanzauswertungVP;
					vpReport.SetReportDataSource(roVP);
				}
			}
			
            this.bindingSource1.DataSource = pRO;
        }

    }

}
