using System;
using System.Collections;
using System.Collections.Generic;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report.ReportObjects;
using BeWo.Report;

namespace VkmHamm
{
    public partial class Mitarbeiterstunden : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<FLSReportRO>
    {
		public Mitarbeiterstunden()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(FLSReportRO pRO)
        {
			ConvertReportGroups(pRO);
			//foreach (var group in pRO.FLSReportGroups)
			//{
			//	foreach (var sr in group.ServiceRecords)
			//	{
			//		if (!sr.DistanceInMeter.HasValue && sr.ServiceRecordOid.HasValue)
			//		{
			//			var srBO = DAOFactory.GenericDAO.LoadByID<ServiceRecord>(sr.ServiceRecordOid.Value);
			//			sr.DistanceInMeter = srBO.DistanceInMeter;
			//		}
			//	}
			//}
            this.bindingSource1.DataSource = pRO;
        }

		private void ConvertReportGroups(FLSReportRO pRO)
		{
			if (pRO.FLSReportGroups != null)
			{
				foreach (var rg in pRO.FLSReportGroups)
				{
					if (rg.ServiceRecords == null || rg.ServiceRecords.Count == 0)
					{
						rg.ServiceRecords = new List<FLSReportRO.FLSReportServiceRecord>();

						if (rg.FLSReportDetails != null)
						{
							foreach (var rd in rg.FLSReportDetails)
							{
								if (rd.ServiceRecords != null)
								{
									foreach (var sd in rd.ServiceRecords)
									{
										rg.ServiceRecords.Add(sd);
									}
								}
							}
						}

						rg.ServiceRecords.Sort((s1, s2) => Comparer.Default.Compare(s1.Start, s2.Start));
					}
				}
			}
		}

    }
}
