using BeWo.Data.Entities;

using BS.Shared.DataContracts.Compact;

namespace BeWo.Service.DCEntityMapper
{
    public class CompactPersonDC_Person : AbstractIDCEntityMapper<Person, CompactPersonDC>
    {
        public override CompactPersonDC MergeWithDC(Person pEntity, CompactPersonDC pDataContract)
        {
            pDataContract.DateOfBirth = pEntity.DateOfBirth;
            pDataContract.FirstName = pEntity.FirstName;
            pDataContract.LastName = pEntity.LastName;
            pDataContract.PersonOid = pEntity.Oid.Value;
            pDataContract.PersonVersion = pEntity.Version.Value;
            pDataContract.ActivationType = pEntity.IsActive;
            pDataContract.PersonType = pEntity.Type;

            if (pEntity.Title != null)
            {
                pDataContract.Title = pEntity.Title.Value;
            }

            if (pEntity.Function != null)
            {
                pDataContract.Function = pEntity.Function.Value;
            }

            if (pEntity.Address != null)
            {
                pDataContract.Street = pEntity.Address.Street;
                pDataContract.PostalCode = pEntity.Address.PostalCode;
                pDataContract.Town = pEntity.Address.Town;
            }

            pDataContract.OrganisationRelations = MapperFactory.Organisation2PersonDC_Organisation2Person.MapToNewDCs(pEntity.Organisation2Persons);

            return pDataContract;
        }

        public override Person MergeWithEntity(CompactPersonDC pDataContract, Person pEntity)
        {
            this.ConcurrencyCheck(pDataContract.PersonVersion, pEntity);

            pEntity.Oid = pDataContract.PersonOid;
            pEntity.DateOfBirth = pDataContract.DateOfBirth;
            pEntity.FirstName = pDataContract.FirstName;
            pEntity.LastName = pDataContract.LastName;
            pEntity.IsActive = pDataContract.ActivationType;

            MapperFactory.Organisation2PersonDC_Organisation2Person.MergeWithEntitys(pDataContract.OrganisationRelations, pEntity.Organisation2Persons);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(CompactPersonDC pDC, Person pEntity)
        {
            return pDC.PersonOid == pEntity.Oid;
        }
    }
}