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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class FollowUpDC_FollowUp : AbstractIDCEntityMapper<FollowUp, FollowUpDC>
    {
        public override FollowUpDC MergeWithDC(FollowUp pEntity, FollowUpDC pDataContract)
        {
            pDataContract.FollowUpOid = pEntity.Oid;
            pDataContract.FollowUpVersion = pEntity.Version;
            pDataContract.InsertedBy = pEntity.InsUser;
            pDataContract.InsertedOn = pEntity.InsTs;
            pDataContract.Description = pEntity.Notice;
            pDataContract.ExpirationDate = pEntity.ExpirationDate;
            pDataContract.IsHandled = pEntity.IsHandled;

            if (pEntity.Employee != null)
            {
                pDataContract.Employee = MapperFactory.EmployeeDC_Employee.MapToNewDC(pEntity.Employee);
            }

            if (pEntity.Responsible != null)
            {
                pDataContract.Responsible = MapperFactory.EmployeeDC_Employee.MapToNewDC(pEntity.Responsible);
            }

            return pDataContract;
        }

        public override FollowUp MergeWithEntity(FollowUpDC pDataContract, FollowUp pEntity)
        {
            this.ConcurrencyCheck(pDataContract.FollowUpVersion, pEntity);
            pEntity.Oid = pDataContract.FollowUpOid;
            pEntity.Notice = pDataContract.Description;
            pEntity.ExpirationDate = pDataContract.ExpirationDate;
            pEntity.IsHandled = pDataContract.IsHandled;

            if (pEntity.Employee == null || pDataContract.Employee.EmployeeOid != pEntity.Employee.Oid)
            {
                pEntity.Employee = DAOFactory.GenericDAO.LoadByID<Employee>(pDataContract.Employee.EmployeeOid.Value);
            }
            else
            {
                this.ConcurrencyCheck(pDataContract.Employee.EmployeeVersion, pEntity.Employee);
            }

            if (pEntity.Responsible == null || pDataContract.Responsible.EmployeeOid != pEntity.Responsible.Oid)
            {
                pEntity.Responsible = DAOFactory.GenericDAO.LoadByID<Employee>(pDataContract.Responsible.EmployeeOid.Value);
            }
            else
            {
                this.ConcurrencyCheck(pDataContract.Responsible.EmployeeVersion, pEntity.Responsible);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(FollowUpDC pDC, FollowUp pEntity)
        {
            if (pDC.FollowUpOid == null)
            {
                return false;
            }

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