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.BetreuWoReports
{
	public class BetreuwoReportCreator : 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, serviceCategoryOid);

			if (lROs.Count > 0)
			{
				XtraReport rootReport = null;
				var reports = CreateServicesOverviewReportsForRo(lROs[0], serviceCategoryOid);
				rootReport = reports[0];
				rootReport.CreateDocument();

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

				for (int i = 1; i < lROs.Count; i++)
				{
					var lNextReports = CreateServicesOverviewReportsForRo(lROs[i], serviceCategoryOid);
					for (int j = 0; j < lNextReports.Count; j++)
					{
						var lNext = lNextReports[j];
						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);

			var reports = CreateServicesOverviewReportsForRo(ro, serviceCategoryOid);

			
			if (reports.Count == 1)
			{
				return reports[0];
			}
			if (reports.Count > 1)
			{
				var rootReport = reports[0];
				rootReport.CreateDocument();

				for (int i = 1; i < reports.Count; i++)
				{
					var lNext = reports[i];
					lNext.CreateDocument();
					rootReport.Pages.AddRange(lNext.Pages);
				}
				return rootReport;
			}
			return new XtraReport();
		}

		private IList<XtraReport> CreateServicesOverviewReportsForRo(ServicesOverviewRO ro, long? serviceCategoryOid)
		{
			IList<XtraReport> reports = new List<XtraReport>();

			bool containsFLS = false;
			bool containsALS = false;
			bool containsOther = false;

			if (ro.Services != null)
			{
				foreach (var sr in ro.Services)
				{
					if (sr.ServiceCategory == "ALS Assistenz")
					{
						containsALS = true;
					}
					else if (sr.ServiceCategory == "LVR Fachleistung")
					{
						if (sr.ServiceDescription.StartsWith("Bereitschaft"))
						{
							containsOther = true;
						}
						else
						{
							containsFLS = true;
						}
					}
					else if (sr.ServiceCategory == "Hausmeister 0,3")
					{
						containsFLS = true;
					}
					else
					{
						containsOther = true;
					}
				}
			}

			if (containsFLS)
			{
				IBeWoReport<ServicesOverviewRO> flsReport = new BetreuWoReports.StundenzettelFLS();
				flsReport.SetReportDataSource(ro);
				reports.Add(flsReport as XtraReport);
			}
			if (containsALS)
			{
				IBeWoReport<ServicesOverviewRO> alsReport = new BetreuWoReports.StundenzettelALS();
				alsReport.SetReportDataSource(ro);
				reports.Add(alsReport as XtraReport);
			}
			if (reports.Count == 0)
			{
				containsOther = true;
			}
			if (containsOther)
			{
				IBeWoReport<ServicesOverviewRO> report = new BetreuWoReports.Stundenzettel();
				report.SetReportDataSource(ro);
				reports.Add(report as XtraReport);
			}
			
			return reports;
		}

	}
}
