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 SynpraxReports
{
	public class SynpraxReportCreator : 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)
            {
                lROs.Sort((a, b) =>
                {
                    if (a.MainAttendant == null)
                        a.MainAttendant = "";
                    if (b.MainAttendant == null)
                        b.MainAttendant = "";
                    var c = a.MainAttendant.CompareTo(b.MainAttendant);

                    if (c == 0)
                    {
                        c = a.CustomerLastName.CompareTo(b.CustomerLastName);
                    }
                    return c;
                });
                String reportId = null;
                if (orgaOid > 0)
                {
                    var organisation = DAOFactory.GenericDAO.LoadByID<Organisation>(orgaOid);
                    if (organisation != null)
                        reportId = organisation.Name;
                }
                var lServiceOverviewAllCustomersReport = this.FindReportImp<ServicesOverviewRO>(reportId);
                lServiceOverviewAllCustomersReport.SetReportDataSource(lROs[0]);
                (lServiceOverviewAllCustomersReport as XtraReport).CreateDocument();
                var Report = lServiceOverviewAllCustomersReport as XtraReport;

                for (int i = 1; i < lROs.Count; i++)
                {
                    var lNext = FindReportImp<ServicesOverviewRO>(reportId);
                    lNext.SetReportDataSource(lROs[i]);
                    (lNext as XtraReport).CreateDocument();
                    Report.Pages.AddRange((lNext as XtraReport).Pages);
                }

                return lServiceOverviewAllCustomersReport as XtraReport;
            }
            return new XtraReport();
        }
    }
}
