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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class ServiceCategoryDC_ServiceCategory : AbstractIDCEntityMapper<ServiceCategory, ServiceCategoryDC>
    {
        public override ServiceCategoryDC MergeWithDC(ServiceCategory pEntity, ServiceCategoryDC pDataContract)
        {
            pDataContract.ServiceCategoryOid = pEntity.Oid;
            pDataContract.ServiceCategoryVersion = pEntity.Version;
            pDataContract.Percentage = pEntity.Percentage;
            pDataContract.Name = pEntity.Name;
            pDataContract.IsBillable = pEntity.IsBillable;
            pDataContract.OhneHilfeplan = pEntity.OhneHilfeplan;
            pDataContract.Abbreviation = pEntity.Abbreviation;
            pDataContract.Position = pEntity.Position;
	        pDataContract.IsDefault = pEntity.IsDefault;
            pDataContract.BillableAmount = pEntity.BillableAmount;
            pDataContract.AccountingInterval = pEntity.AccountingInterval;
            pDataContract.ScopeType = pEntity.ScopeType.HasValue ? pEntity.ScopeType.Value : ScopeTypeId.Global;
            pDataContract.IsSystemEntry = pEntity.SystemEntryID != null;

            pDataContract.CostRatePeriods = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(pEntity.CostRatePeriods);

            return pDataContract;
        }

        public override ServiceCategory MergeWithEntity(ServiceCategoryDC pDataContract, ServiceCategory pEntity)
        {
            ConcurrencyCheck(pDataContract.ServiceCategoryVersion, pEntity);

            pEntity.Oid = pDataContract.ServiceCategoryOid;
            pEntity.Name = pDataContract.Name;
            pEntity.IsBillable = pDataContract.IsBillable;
            pEntity.OhneHilfeplan = pDataContract.OhneHilfeplan;
            pEntity.Percentage = pDataContract.Percentage;
            pEntity.Abbreviation = pDataContract.Abbreviation;
            pEntity.Position = pDataContract.Position;
            pEntity.BillableAmount = pDataContract.BillableAmount;
            pEntity.AccountingInterval = pDataContract.AccountingInterval;
            pEntity.ScopeType = pDataContract.ScopeType;
			pEntity.IsDefault = pDataContract.IsDefault;

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

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(ServiceCategoryDC pDC, ServiceCategory pEntity)
        {
            if (pDC.ServiceCategoryOid == null)
            {
                return false;
            }

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