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.IBPReports
{
	public class IBPReportCreator : 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)
			{
				String reportId = null;
				if (orgaOid > 0)
				{
					var organisation = DAOFactory.GenericDAO.LoadByID<Organisation>(orgaOid);
					if (organisation != null)
						reportId = organisation.Name;
				}

				
				var rootReport = CreateServicesOverviewReportForRo(lROs[0], reportId);
				rootReport.CreateDocument();
				
				for (int i = 1; i < lROs.Count; i++)
				{
					var lNext = CreateServicesOverviewReportForRo(lROs[i], reportId);
					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)
		{
			String reportId = null;
			if (orgaOid > 0)
			{
				var organisation = DAOFactory.GenericDAO.LoadByID<Organisation>(orgaOid);
				if (organisation != null)
					reportId = organisation.Name;
			}

			var ro = ServicesOverviewRO.Create(customerOid, month, year, false, orgaOid, empOid, startDay, endDay, serviceCategoryOid);

			return CreateServicesOverviewReportForRo(ro, reportId);

		}

		private XtraReport CreateServicesOverviewReportForRo(ServicesOverviewRO ro, String reportId)
		{
			IBeWoReport<ServicesOverviewRO> report = null;

			foreach (var s in ro.Services)
			{
				if (report == null && s.ServiceCategory.Contains("BeWo 67"))
					report = new BeWo.IBPReports.StundenzettelBeWo67();
			}
			if (report == null)
				report = new BeWo.IBPReports.Stundenzettel();
			report.SetReportDataSource(ro);

			return report as XtraReport;
		}
	}
}
