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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class AbsenceTimeDC_AbsenceTime : AbstractIDCEntityMapper<AbsenceTime, AbsenceTimeDC>
    {
        public override AbsenceTimeDC MergeWithDC(AbsenceTime pEntity, AbsenceTimeDC pDataContract)
        {
            pDataContract.AbsenceTimeOid = pEntity.Oid;
            pDataContract.AbsenceTimeVersion = pEntity.Version;
            pDataContract.End = pEntity.End;
            pDataContract.Start = pEntity.Start;
            pDataContract.Notice = pEntity.Notice;

            pDataContract.Reason = MapperFactory.AbsenceReasonDC_AbsenceReason.MapToNewDC(pEntity.AbsenceReason);

            pDataContract.EmployeeOid = pEntity.EmployeeOid;
            pDataContract.CustomerOid = pEntity.CustomerOid;

            return pDataContract;
        }

        public override AbsenceTime MergeWithEntity(AbsenceTimeDC pDataContract, AbsenceTime pEntity)
        {
            ConcurrencyCheck(pDataContract.AbsenceTimeVersion, pEntity);

	        if (pDataContract.Reason.AbsenceReasonOid.HasValue) 
				pEntity.AbsenceReason = DAOFactory.GenericDAO.LoadByID<AbsenceReason>(pDataContract.Reason.AbsenceReasonOid.Value);

	        pEntity.End = pDataContract.End;
            pEntity.Start = pDataContract.Start;
            pEntity.Notice = pDataContract.Notice;
            pEntity.Oid = pDataContract.AbsenceTimeOid;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(AbsenceTimeDC pDC, AbsenceTime pEntity)
        {
            if (pDC.AbsenceTimeOid == null)
            {
                return false;
            }

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