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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class AssessmentSheetEntryDC_AssessmentSheetEntry : AbstractIDCEntityMapper<AssessmentSheetEntry, AssessmentSheetEntryDC>
    {
        public override AssessmentSheetEntryDC MergeWithDC(AssessmentSheetEntry pEntity, AssessmentSheetEntryDC pDataContract)
        {
            pDataContract.AssessmentSheetEntryOid = pEntity.Oid;
            pDataContract.AssessmentSheetEntryVersion = pEntity.Version;

            if (pEntity.Customer != null)
            {
                pDataContract.CustomerOid = pEntity.Customer.Oid;
            }

            if (pEntity.AssessmentSheetCategory != null)
            {
                pDataContract.AssessmentSheetCategoryOid = pEntity.AssessmentSheetCategory.Oid;
            }

            if (pEntity.AssessmentSheetValue != null)
            {
                pDataContract.AssessmentSheetValueOid = pEntity.AssessmentSheetValue.Oid;
            }

            pDataContract.Day = pEntity.Day;

            return pDataContract;
        }

        public override AssessmentSheetEntry MergeWithEntity(AssessmentSheetEntryDC pDataContract, AssessmentSheetEntry pEntity)
        {
            this.ConcurrencyCheck(pDataContract.AssessmentSheetEntryVersion, pEntity);

            pEntity.Day = pDataContract.Day;

            pEntity.Customer = null;
            if (pDataContract.CustomerOid.HasValue)
            {
                pEntity.Customer = DAOFactory.GenericDAO.LoadByID<Customer>(pDataContract.CustomerOid.Value);
            }

            pEntity.AssessmentSheetValue = null;
            if (pDataContract.AssessmentSheetValueOid.HasValue)
            {
                pEntity.AssessmentSheetValue = DAOFactory.GenericDAO.LoadByID<AssessmentSheetValue>(pDataContract.AssessmentSheetValueOid.Value);
            }

            pEntity.AssessmentSheetCategory = null;
            if (pDataContract.AssessmentSheetCategoryOid.HasValue)
            {
                pEntity.AssessmentSheetCategory = DAOFactory.GenericDAO.LoadByID<AssessmentSheetCategory>(pDataContract.AssessmentSheetCategoryOid.Value);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(AssessmentSheetEntryDC pDC, AssessmentSheetEntry pEntity)
        {
            if (pDC.AssessmentSheetEntryOid == null)
            {
                return false;
            }

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