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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class ContractDC_Contract : AbstractIDCEntityMapper<Contract, ContractDC>
    {
        public override ContractDC MergeWithDC(Contract pEntity, ContractDC pDataContract)
        {
            pDataContract.ContractOid = pEntity.Oid;
            pDataContract.ContractVersion = pEntity.Version;
            pDataContract.CancellationPeriod = pEntity.CancellationPeriod;
            pDataContract.ContractEndDate = pEntity.ContractEndDate;
            pDataContract.EntryDate = pEntity.EntryDate;
            pDataContract.HourlyRate = pEntity.HourlyRate;
            pDataContract.LeaveDays = pEntity.LeaveDays;
            pDataContract.ProbationPeriod = pEntity.ProbationPeriod;
            pDataContract.WeeklyDays = pEntity.WeeklyDays ?? 5;
	        if (pDataContract.WeeklyDays == 0)
		        pDataContract.WeeklyDays = 5;
            pDataContract.WeeklyFLS = pEntity.WeeklyFLS;
            pDataContract.WeeklyTotalHours = pEntity.WeeklyTotalHours;
            pDataContract.Notice = pEntity.Notice;

            if (pEntity.EmploymentType != null)
            {
                pDataContract.EmploymentType = MapperFactory.EmploymentTypeDC_EmploymentType.MapToNewDC(pEntity.EmploymentType);
            }

            return pDataContract;
        }

        public override Contract MergeWithEntity(ContractDC pDataContract, Contract pEntity)
        {
            this.ConcurrencyCheck(pDataContract.ContractVersion, pEntity);

            pEntity.CancellationPeriod = pDataContract.CancellationPeriod;
            pEntity.ContractEndDate = pDataContract.ContractEndDate;
            pEntity.EntryDate = pDataContract.EntryDate;
            pEntity.HourlyRate = pDataContract.HourlyRate;
            pEntity.LeaveDays = pDataContract.LeaveDays;
            pEntity.ProbationPeriod = pDataContract.ProbationPeriod;
            pEntity.WeeklyDays = pDataContract.WeeklyDays;
            pEntity.WeeklyFLS = pDataContract.WeeklyFLS;
            pEntity.WeeklyTotalHours = pDataContract.WeeklyTotalHours;
            pEntity.Notice = pDataContract.Notice;

            pEntity.Oid = pDataContract.ContractOid;

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

            
            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(ContractDC pDC, Contract pEntity)
        {
            if (pDC.ContractOid == null)
            {
                return false;
            }

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