using BeWo.Data.Access;
using BeWo.Data.Entities;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
	public class SupportConceptCostBearerRelDC_CostBearer2SupportConcept : AbstractIDCEntityMapper<CostBearer2SupportConcept, SupportConceptCostBearerRelDC>
	{
		public override SupportConceptCostBearerRelDC MergeWithDC(CostBearer2SupportConcept pEntity, SupportConceptCostBearerRelDC pDataContract)
		{
			if (pEntity.CostBearer.Organisation != null)
			{
				pDataContract.CostBearer = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(pEntity.CostBearer.Organisation);
			}

			pDataContract.CostBearer2SupportConceptOid = pEntity.Oid;
			pDataContract.CostBearer2SupportConceptVersion = pEntity.Version;
			pDataContract.Notice = pEntity.Notice;
			pDataContract.Status = pEntity.Status;
			pDataContract.SupportConcept = MapperFactory.CompactSupportConceptDC_SupportConcept.MapToNewDC(pEntity.SupportConcept);

			// pDataContract.NoRelationExists = pEntity.ServiceRecords.Count == 0;
			pDataContract.RequestedStartDate = pEntity.RequestedStartDate;
			pDataContract.RequestedEndDate = pEntity.RequestedEndDate;
			pDataContract.ApprovedStartDate = pEntity.ApprovedStartDate;
			pDataContract.ApprovedEndDate = pEntity.ApprovedEndDate;
			pDataContract.CustomerReferenceNumber = pEntity.CustomerReferenceNumber;
            pDataContract.AuswahlBezeichnung = pEntity.AuswahlBezeichnung;

			pDataContract.MonthlyPayment = pEntity.MonthlyPayment;
			//pDataContract.ApprovedFLS = pEntity.ApprovedFLS;
			//pDataContract.ApprovedFLSTotal = pEntity.ApprovedFLSTotal;
			if (pEntity.CostBearerContactPerson != null)
				pDataContract.CostBearerContactPerson = MapperFactory.CompactPersonDC_Person.MapToNewDC(pEntity.CostBearerContactPerson);
			pDataContract.ApprovalPeriodList = MapperFactory.SupportConceptApprovalPeriodDC_SupportConceptApprovalPeriod.MapToNewDCs(pEntity.ApprovalPeriodList);
			if (pDataContract.ApprovalPeriodList != null)
			{
				foreach (SupportConceptApprovalPeriodDC item in pDataContract.ApprovalPeriodList)
				{
					if (!item.StartDate.HasValue)
					{
						item.StartDate = pEntity.ApprovedStartDate.HasValue ? pEntity.ApprovedStartDate : pEntity.RequestedStartDate;
					}
					if (!item.EndDate.HasValue)
					{
						item.EndDate = pEntity.ApprovedEndDate.HasValue ? pEntity.ApprovedEndDate : pEntity.RequestedEndDate;
					}
				}
			}
			return pDataContract;
		}

		public override CostBearer2SupportConcept MergeWithEntity(SupportConceptCostBearerRelDC pDataContract, CostBearer2SupportConcept pEntity)
		{
			ConcurrencyCheck(pDataContract.CostBearer2SupportConceptVersion, pEntity);

			pEntity.Notice = pDataContract.Notice;
			pEntity.Oid = pDataContract.CostBearer2SupportConceptOid;
			pEntity.Status = pDataContract.Status;
			pEntity.RequestedStartDate = pDataContract.RequestedStartDate;
			pEntity.RequestedEndDate = pDataContract.RequestedEndDate;
			pEntity.ApprovedStartDate = pDataContract.ApprovedStartDate;
			pEntity.ApprovedEndDate = pDataContract.ApprovedEndDate;
			pEntity.CustomerReferenceNumber = pDataContract.CustomerReferenceNumber;
            pEntity.AuswahlBezeichnung = pDataContract.AuswahlBezeichnung;
			if (!Utils.IsAnyNull(pDataContract.ApprovedStartDate, pDataContract.ApprovedEndDate))
			{
				pEntity.Status = CostBearer2SupportConceptStatus.Approved;
			}
			else if (!Utils.IsAnyNull(pDataContract.RequestedStartDate, pDataContract.RequestedEndDate))
			{
				pEntity.Status = CostBearer2SupportConceptStatus.Requested;
			}
			else
			{
				pEntity.Status = CostBearer2SupportConceptStatus.Created;
			}

			if (pEntity.CostBearer == null || (pDataContract.CostBearer.CostBearerOid.HasValue && pDataContract.CostBearer.CostBearerOid.Value != pEntity.CostBearer.Oid))
			{
				pEntity.CostBearer = DAOFactory.GenericDAO.LoadByID<CostBearer>(pDataContract.CostBearer.CostBearerOid.Value);
			}

			pEntity.CostBearerContactPerson = null;
			if (pDataContract.CostBearerContactPerson != null)
				pEntity.CostBearerContactPerson = DAOFactory.GenericDAO.LoadByID<Person>(pDataContract.CostBearerContactPerson.PersonOid);

			//pEntity.ApprovedFLS = pDataContract.ApprovedFLS;
			//pEntity.ApprovedFLSTotal = pDataContract.ApprovedFLSTotal;
			pEntity.MonthlyPayment = pDataContract.MonthlyPayment;

			MapperFactory.SupportConceptApprovalPeriodDC_SupportConceptApprovalPeriod.MergeWithEntitys(pDataContract.ApprovalPeriodList, pEntity.ApprovalPeriodList);

			return pEntity;
		}

		protected override bool AreDCAndEntityEqual(SupportConceptCostBearerRelDC pDC, CostBearer2SupportConcept pEntity)
		{
			if (pDC.CostBearer2SupportConceptOid == null)
			{
				return false;
			}

			return pDC.CostBearer2SupportConceptOid == pEntity.Oid;
		}
	}
}