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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class CostRatePeriodDC_CostRatePeriod : AbstractIDCEntityMapper<CostRatePeriod, CostRatePeriodDC>
    {
        public override CostRatePeriodDC MergeWithDC(CostRatePeriod pEntity, CostRatePeriodDC pDataContract)
        {
            pDataContract.CostRatePeriodOid = pEntity.Oid;
            pDataContract.CostRatePeriodVersion = pEntity.Version;
            pDataContract.CostRateValue = pEntity.CostRateValue;
            pDataContract.CostRateType = pEntity.CostRateType;
            pDataContract.StartDate = pEntity.StartDate;
            pDataContract.EndDate = pEntity.EndDate;
            pDataContract.Notice = pEntity.Notice;
            pDataContract.UnitName = pEntity.UnitName;
            if (pEntity.ValueListEntry != null)
            {
                pDataContract.ValueListEntry = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.ValueListEntry);
            }

            return pDataContract;
        }

        public override CostRatePeriod MergeWithEntity(CostRatePeriodDC pDataContract, CostRatePeriod pEntity)
        {
            this.ConcurrencyCheck(pDataContract.CostRatePeriodVersion, pEntity);

            pEntity.Oid = pDataContract.CostRatePeriodOid;
            pEntity.CostRateValue = pDataContract.CostRateValue;
            pEntity.CostRateType = pDataContract.CostRateType;
            pEntity.StartDate = pDataContract.StartDate;
            pEntity.EndDate = pDataContract.EndDate;
            pEntity.Notice = pDataContract.Notice;
            pEntity.UnitName = pDataContract.UnitName;

            if (pDataContract.ValueListEntry == null)
            {
                pEntity.ValueListEntry = null;
            }
            else
            {
                pEntity.ValueListEntry = DAOFactory.GenericDAO.LoadByID<ValueListEntry>(pDataContract.ValueListEntry.ValueListEntryOid.Value);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(CostRatePeriodDC pDC, CostRatePeriod pEntity)
        {
            if (pDC.CostRatePeriodOid == null)
            {
                return false;
            }

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