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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class ServiceDescriptionDC_ServiceDescription : AbstractIDCEntityMapper<ServiceDescription, ServiceDescriptionDC>
    {
        public override ServiceDescriptionDC MergeWithDC(ServiceDescription pEntity, ServiceDescriptionDC pDataContract)
        {
            pDataContract.ServiceDescriptionOid = pEntity.Oid;
            pDataContract.ServiceDescriptionVersion = pEntity.Version;
            pDataContract.Name = pEntity.Name;
            pDataContract.Abbreviation = pEntity.Abbreviation;
            pDataContract.Category = MapperFactory.ServiceCategoryDC_ServiceCategory.MapToNewDC(pEntity.ServiceCategory);
            pDataContract.CategoryName = pEntity.ServiceCategory.Name;
            pDataContract.Position = pEntity.Position;
			pDataContract.IsDefault = pEntity.IsDefault;
            pDataContract.BillableAmount = pEntity.BillableAmount;
            pDataContract.AccountingInterval = pEntity.AccountingInterval;
            pDataContract.Unit = pEntity.Unit.HasValue ? pEntity.Unit.Value : ServiceUnit.DateTime;
            pDataContract.ScopeType = pEntity.ScopeType.HasValue ? pEntity.ScopeType.Value : ScopeTypeId.Global;
            pDataContract.IsSystemEntry = pEntity.SystemEntryID != null;
	        pDataContract.DoNotCheckOverlapping = pEntity.DoNotCheckOverlapping.HasValue && pEntity.DoNotCheckOverlapping.Value;
            pDataContract.CostRatePeriods = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(pEntity.CostRatePeriods);

            return pDataContract;
        }

        public override ServiceDescription MergeWithEntity(ServiceDescriptionDC pDataContract, ServiceDescription pEntity)
        {
            this.ConcurrencyCheck(pDataContract.ServiceDescriptionVersion, pEntity);
            pEntity.Oid = pDataContract.ServiceDescriptionOid;
            pEntity.Name = pDataContract.Name;
            pEntity.Abbreviation = pDataContract.Abbreviation;
            pEntity.Position = pDataContract.Position;
            pEntity.BillableAmount = pDataContract.BillableAmount;
            pEntity.AccountingInterval = pDataContract.AccountingInterval;
            pEntity.Unit = pDataContract.Unit;
            pEntity.ScopeType = pDataContract.ScopeType;
			pEntity.IsDefault = pDataContract.IsDefault;
	        pEntity.DoNotCheckOverlapping = pDataContract.DoNotCheckOverlapping;

            if (pEntity.ServiceCategory == null || pDataContract.Category.ServiceCategoryOid != pEntity.ServiceCategory.Oid)
            {
                if (pDataContract.Category.ServiceCategoryOid.HasValue)
                    pEntity.ServiceCategory = DAOFactory.GenericDAO.LoadByID<ServiceCategory>(pDataContract.Category.ServiceCategoryOid.Value);
                else
                    pEntity.ServiceCategory = MapperFactory.ServiceCategoryDC_ServiceCategory.MapToNewEntity(pDataContract.Category);
            }
            else
            {
                this.ConcurrencyCheck(pDataContract.Category.ServiceCategoryVersion, pEntity.ServiceCategory);
            }

            MapperFactory.CostRatePeriodDC_CostRatePeriod.MergeWithEntitys(pDataContract.CostRatePeriods, pEntity.CostRatePeriods);
            foreach (var costRatePeriod in pEntity.CostRatePeriods)
            {
                costRatePeriod.ObjectTid = TableID.ServiceDescription;
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(ServiceDescriptionDC pDC, ServiceDescription pEntity)
        {
            if (pDC.ServiceDescriptionOid == null)
            {
                return false;
            }

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