using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using DevExpress.XtraReports.UI;

namespace BeWo.LebenshilfeDuisburgReports
{
	public class LHDuisburgReportCreator : DefaultReportCreator
	{
		public override XtraReport CreateServiceOverviewAllCustomersReport(int month, int year, long orgaOid, long empOid, long? serviceCategoryOid, int startDay, int endDay)
		{
			List<ServicesOverviewRO> lROs = ServicesOverviewRO.Create(month, year, orgaOid, empOid, startDay, endDay);

			if (lROs.Count > 0)
			{

				var rootReport = CreateServicesOverviewReportForRo(lROs[0], serviceCategoryOid);
				rootReport.CreateDocument();

				for (int i = 1; i < lROs.Count; i++)
				{
					var lNext = CreateServicesOverviewReportForRo(lROs[i], serviceCategoryOid);
					lNext.CreateDocument();
					rootReport.Pages.AddRange(lNext.Pages);
				}

				return rootReport;
			}
			return new XtraReport();
		}

		public override XtraReport CreateServiceOverviewSingleCustomerReport(long customerOid, int month, int year, long orgaOid, long empOid, long? serviceCategoryOid, int startDay, int endDay)
		{
			var ro = ServicesOverviewRO.Create(customerOid, month, year, false, orgaOid, empOid, startDay, endDay, serviceCategoryOid);

			return CreateServicesOverviewReportForRo(ro, serviceCategoryOid);

		}

		private XtraReport CreateServicesOverviewReportForRo(ServicesOverviewRO ro, long? serviceCategoryOid)
		{
			IBeWoReport<ServicesOverviewRO> report = null;
			bool isBeWo = false;
			bool isIff = false;
			bool isFf = false;
			bool isTbt = false;

			if (ro.Services != null)
			{
				foreach (var sr in ro.Services)
				{
					if (sr.ServiceCategory == "Direkte Betreuungsleistung")
					{
						isBeWo = true;
					}
					else if (sr.ServiceCategory == "Assistenzleistungen")
					{
						isBeWo = true;
					}
					else if (sr.ServiceCategory.StartsWith("Leistungsmodul"))
					{
						isBeWo = true;
					}
					else if (sr.ServiceCategory.ToLower().StartsWith("iff") || sr.ServiceCategory.ToLower().Contains("terminausfall iff"))
					{
						isIff = true;
					}
					else if (sr.ServiceCategory.ToLower().StartsWith("hpff") || sr.ServiceCategory.ToLower().Contains("terminausfall ff"))
					{
						isFf = true;
					}
					else if (sr.ServiceCategory.ToLower().StartsWith("tbt"))
					{
						isTbt = true;
					}
				}
			}
			if (isIff)
			{
				report = new BeWo.LebenshilfeDuisburgReports.QuittierungsbelegIFF();
			}
			else if (isFf)
			{
				report = new BeWo.LebenshilfeDuisburgReports.QuittierungsbelegFF();
			}
			else if (isTbt)
			{
				report = new BeWo.LebenshilfeDuisburgReports.StundenzettelTBT();
			}
			else if (isBeWo)
			{
				report = new BeWo.LebenshilfeDuisburgReports.Stundenzettel();
			}

			if (report == null)
				report = new BeWo.LebenshilfeDuisburgReports.QuittierungsbelegHPFH();
			report.SetReportDataSource(ro);

			return report as XtraReport;
		}
	}
}
