using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class SupportConceptApprovalPeriodDC_SupportConceptApprovalPeriod : AbstractIDCEntityMapper<SupportConceptApprovalPeriod, SupportConceptApprovalPeriodDC>
    {
        public override SupportConceptApprovalPeriodDC MergeWithDC(SupportConceptApprovalPeriod pEntity, SupportConceptApprovalPeriodDC pDataContract)
        {
            pDataContract.ApprovedBEInterval                  = pEntity.ApprovedBEInterval;
            pDataContract.IsApprovedBEShifting                = pEntity.IsApprovedBEShifting;
            pDataContract.IsApprovedFixedAmountShifting       = pEntity.IsApprovedFixedAmountShifting;
            pDataContract.MonthlyPayment                      = pEntity.MonthlyPayment;
            pDataContract.SupportConceptApprovalPeriodOid     = pEntity.Oid;
            pDataContract.SupportConceptApprovalPeriodVersion = pEntity.Version;
            pDataContract.StartDate                           = pEntity.StartDate;
            pDataContract.EndDate                             = pEntity.EndDate;
            pDataContract.ApprovedBETotal                     = pEntity.ApprovedBETotal;
            pDataContract.ApprovedBEPerInterval               = pEntity.ApprovedBEPerInterval;
            pDataContract.ContactCountInterval                = pEntity.ContactCountInterval;
            pDataContract.ContactCountPerInterval             = pEntity.ContactCountPerInterval;
            pDataContract.ApprovedFixedAmount                 = pEntity.ApprovedFixedAmount;
            pDataContract.ApprovedFixedAmountInterval         = pEntity.ApprovedFixedAmountInterval;

            pDataContract.SupportConceptApprovalPeriodEmployeeRelations = MapperFactory.SupportConceptApprovalPeriodEmployeeRelDC_SupportConceptApprovalPeriod2Employee.MapToNewDCs(pEntity.SupportConceptApprovalPeriod2Employee);

            if (!pDataContract.ApprovedBEInterval.HasValue)
            {
                pDataContract.ApprovedBEInterval = SupportConceptApprovalInterval.Weekly;
            }

			if (!pDataContract.ContactCountInterval.HasValue)
			{
			    pDataContract.ContactCountInterval = SupportConceptApprovalInterval.Weekly;
			}

            if (!pDataContract.ApprovedFixedAmountInterval.HasValue)
            {
                pDataContract.ApprovedFixedAmountInterval = SupportConceptApprovalInterval.Weekly;
            }

            if (pEntity.ServiceCategory != null)
            {
                pDataContract.ServiceCategory = MapperFactory.ServiceCategoryDC_ServiceCategory.MapToNewDC(pEntity.ServiceCategory);
            }

            return pDataContract;
        }

        public override SupportConceptApprovalPeriod MergeWithEntity(SupportConceptApprovalPeriodDC pDataContract, SupportConceptApprovalPeriod pEntity)
        {
            ConcurrencyCheck(pDataContract.SupportConceptApprovalPeriodVersion, pEntity);

            pEntity.ApprovedBEInterval            = pDataContract.ApprovedBEInterval;
            pEntity.ApprovedBETotal               = pDataContract.ApprovedBETotal;
			pEntity.ContactCountInterval          = pDataContract.ContactCountInterval;
			pEntity.ContactCountPerInterval       = pDataContract.ContactCountPerInterval;
            pEntity.ApprovedBEPerInterval         = pDataContract.ApprovedBEPerInterval;
            pEntity.ApprovedFixedAmount           = pDataContract.ApprovedFixedAmount;
            pEntity.ApprovedFixedAmountInterval   = pDataContract.ApprovedFixedAmountInterval;
            pEntity.EndDate                       = pDataContract.EndDate;
            pEntity.IsApprovedBEShifting          = pDataContract.IsApprovedBEShifting;
            pEntity.IsApprovedFixedAmountShifting = pDataContract.IsApprovedFixedAmountShifting;
            pEntity.MonthlyPayment                = pDataContract.MonthlyPayment;
            pEntity.Oid                           = pDataContract.SupportConceptApprovalPeriodOid;
            pEntity.Version                       = pDataContract.SupportConceptApprovalPeriodVersion;
            pEntity.StartDate                     = pDataContract.StartDate;

            if (pDataContract.ServiceCategory == null)
            {
                pEntity.ServiceCategory = null;
            }
            else if (pEntity.ServiceCategory == null || pDataContract.ServiceCategory.ServiceCategoryOid != pEntity.ServiceCategory.Oid)
            {
                pEntity.ServiceCategory = DAOFactory.GenericDAO.LoadByID<ServiceCategory>(pDataContract.ServiceCategory.ServiceCategoryOid.Value);
            }

            MapperFactory.SupportConceptApprovalPeriodEmployeeRelDC_SupportConceptApprovalPeriod2Employee.MergeWithEntitys(pDataContract.SupportConceptApprovalPeriodEmployeeRelations, pEntity.SupportConceptApprovalPeriod2Employee);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(SupportConceptApprovalPeriodDC pDC, SupportConceptApprovalPeriod pEntity)
        {
            if (pDC.SupportConceptApprovalPeriodOid == null)
            {
                return false;
            }

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