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

using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class WohnheimCustomerRelDC_Customer2WohnheimMapper : AbstractIDCEntityMapper<Customer2Wohnheim, WohnheimCustomerRelationDC>
    {
        public override WohnheimCustomerRelationDC MergeWithDC(Customer2Wohnheim pEntity, WohnheimCustomerRelationDC pDataContract)
        {
            if (pEntity.ValueList.Count > 0)
            {
                pDataContract.RelationRole = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.ValueList[0].Entry);
            }

            pDataContract.Customer = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(pEntity.Customer);
            pDataContract.Wohnheim = MapperFactory.CompactWohnheimDC_Wohnheim.MapToNewDC(pEntity.Wohnheim);
            pDataContract.Customer2WohnheimOid = pEntity.Oid;
            pDataContract.Customer2WohnheimVersion = pEntity.Version;
            pDataContract.Notice = pEntity.Notice;

            return pDataContract;
        }

        public override Customer2Wohnheim MergeWithEntity(WohnheimCustomerRelationDC pDataContract, Customer2Wohnheim pEntity)
        {
            this.ConcurrencyCheck(pDataContract.Customer2WohnheimVersion, pEntity);

            MapperFactory.ValueListEntryDC_ValueListEntry.MapUniqueTypeValueListEntryBack2Entity(pDataContract.RelationRole, pEntity.ValueList, ValueListEntryType.StaffRoleType, TableID.Customer2Wohnheim);

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

            if (pDataContract.Wohnheim != null)
            {
                pEntity.Wohnheim = DAOFactory.GenericDAO.LoadByID<Wohnheim>(pDataContract.Wohnheim.WohnheimOid);
            }

            pEntity.Oid = pDataContract.Customer2WohnheimOid;
            pEntity.Notice = pDataContract.Notice;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(WohnheimCustomerRelationDC pDC, Customer2Wohnheim pEntity)
        {
            if (pDC.Customer2WohnheimOid == null)
            {
                return false;
            }

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