using System;
using System.Linq;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Data.Entities;
using BeWo.Data.Access;
using System.Collections.Generic;
using BS.Shared;
using BS.Shared.Core;
using DevExpress.XtraPrinting;

namespace BeWo.Spektrum
{
	[IDSpecificClass(Identifier = "UeberschneidungenInZeiterfassung")]
	public partial class UeberschneidungenInZeiterfassung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<QueryRO>
	{

		public UeberschneidungenInZeiterfassung()
		{
			InitializeComponent();
		}

		public void SetReportDataSource(QueryRO pRO)
		{

			this.bindingSource1.DataSource = CreateDataSource(pRO);
		}

		private QueryRO CreateDataSource(QueryRO ro)
		{
			QueryRO newQueryRo = new QueryRO();


			var srArray = DAOFactory.SearchDAO.GetBillableServiceRecords().ToArray();

			List<OverlappingPair> overlappingList = new List<OverlappingPair>();

			Dictionary<String, bool> groupOids = new Dictionary<String, bool>();


			for (int i = 0; i < srArray.Length - 1; i++)
			{
				for (int j = i + 1; j < srArray.Length; j++)
				{

					ServiceRecord sr1 = srArray[i];
					ServiceRecord sr2 = srArray[j];

					if (sr1.CustomerOid.HasValue && sr2.CustomerOid.HasValue && sr1.EmployeeOid.HasValue && sr2.EmployeeOid.HasValue && (!sr1.GroupOid.HasValue || !sr2.GroupOid.HasValue || sr1.GroupOid != sr2.GroupOid))
					{

						
						bool overlaps = TimeSpanOverlapsWithOtherTimeSpan(sr1, sr2);

						if (overlaps)
						{
							if (sr1.CustomerOid == sr2.CustomerOid || sr1.EmployeeOid == sr2.EmployeeOid)
							{
								if (sr1.SupportConcept.IsActive == ActivationTypeId.Active ||
								    sr2.SupportConcept.IsActive == ActivationTypeId.Active)
								{


									bool cont = true;

									if (sr1.GroupOid.HasValue && sr2.GroupOid.HasValue)
									{
										String key = String.Format("{0}_{1}", sr1.GroupOid.Value, sr2.GroupOid.Value);
										String key2 = String.Format("{0}_{1}", sr2.GroupOid.Value, sr1.GroupOid.Value);

										if (groupOids.ContainsKey(key) || groupOids.ContainsKey(key2))
											cont = false;
										else
											groupOids.Add(key, true);
									}

									if (cont)
										overlappingList.Add(new OverlappingPair {ServiceRecord1 = sr1, ServiceRecord2 = sr2});
								}
							}

						}
					}
				}
			}

			foreach (var overlappingPair in overlappingList)
			{
				QueryRO.Row row = new QueryRO.Row();

				String name1 = "";
				String name2 = "";

				Customer c1 = overlappingPair.ServiceRecord1.Customer;
				name1 = String.Format("{0}, {1}", c1.Person.LastName, c1.Person.FirstName);

				Customer c2 = overlappingPair.ServiceRecord2.Customer;
				name2 = String.Format("{0}, {1}", c2.Person.LastName, c2.Person.FirstName);

				if (overlappingPair.ServiceRecord1.GroupOid.HasValue)
					name1 += " (Gruppe)";


				if (overlappingPair.ServiceRecord2.GroupOid.HasValue)
					name2 += " (Gruppe)";


				Employee e1 = overlappingPair.ServiceRecord1.Employee;
				Employee e2 = overlappingPair.ServiceRecord2.Employee;

				CostBearer2SupportConcept c2s1 = overlappingPair.ServiceRecord1.CostBearer2SupportConcept;
				CostBearer2SupportConcept c2s2 = overlappingPair.ServiceRecord2.CostBearer2SupportConcept;


				row.Field1 = String.Format("{0}\n{1:dd.MM.yy} - {2:dd.MM.yy}", name1, c2s1.StartDate, c2s1.EndDate);
				//row.Field2 = String.Format("{0:dd.MM.yy} - {1:dd.MM.yy}", c2s1.StartDate, c2s1.EndDate);
				row.Field2 = String.Format("{0:dd.MM.yy HH:mm} - {1:HH:mm}\n{2} {3}", overlappingPair.ServiceRecord1.Start, overlappingPair.ServiceRecord1.End,
					e1.Person.FirstName, e1.Person.LastName);

				row.Field6 = String.Format("{0}\n{1:dd.MM.yy} - {2:dd.MM.yy}", name2, c2s2.StartDate, c2s2.EndDate);
				//row.Field7 = String.Format("{0:dd.MM.yy} - {1:dd.MM.yy}", c2s2.StartDate, c2s2.EndDate);
				row.Field7 = String.Format("{0:dd.MM.yy HH:mm} - {1:HH:mm}\n{2} {3}", overlappingPair.ServiceRecord2.Start, overlappingPair.ServiceRecord2.End,
					e2.Person.FirstName, e2.Person.LastName);

				row.Field10 = String.Format("{0:yyyyMMddHHmm}", overlappingPair.ServiceRecord1.Start);

				newQueryRo.Rows.Add(row);
			}
			newQueryRo.Rows.Sort((a, b) => a.Field10.ToString().CompareTo(b.Field10));

			if (newQueryRo.Rows.Count == 0)
			{
				lblGefunden.Visible = true;
				xrTable2.Visible = false;
			}
			else
			{
				lblGefunden.Visible = false;
			}
			return newQueryRo;
		}

		private bool TimeSpanOverlapsWithOtherTimeSpan(ServiceRecord sr1, ServiceRecord sr2)
		{

			if (sr1.Start.HasValue && sr2.Start.HasValue && sr1.End.HasValue && sr2.End.HasValue)
			{
				if (sr1.Start.Value.Hour == 0 && sr1.Start.Value.Minute == 0 && sr1.Start.Value.Second == 1)
					return false;
				if (sr2.Start.Value.Hour == 0 && sr2.Start.Value.Minute == 0 && sr2.Start.Value.Second == 1)
					return false;
				
				return !(sr1.End <= sr2.Start || sr1.Start >= sr2.End);
			}
			return false;
		}


	}

	class OverlappingPair
	{
		public ServiceRecord ServiceRecord1;
		public ServiceRecord ServiceRecord2;
	}

}
