using BeWo.Data.Entities;

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

namespace BeWo.Service.DCEntityMapper
{
    public class ResourceBookingDC_ResourceBooking : AbstractIDCEntityMapper<ResourceBooking, ResourceBookingDC>
    {
        public override ResourceBookingDC MergeWithDC(ResourceBooking pEntity, ResourceBookingDC pDataContract)
        {
            pDataContract.ResourceBookingOid = pEntity.Oid;
            pDataContract.ResourceBookingVersion = pEntity.Version;
            pDataContract.Notice = pEntity.Notice;
            if (pEntity.Start != null && pEntity.End != null)
            {
                pDataContract.Duration = new DateTimeSpan { StartDateTime = pEntity.Start.Value, EndDateTime = pEntity.End.Value };
            }

            pDataContract.IsDeleted = pEntity.IsDeleted;
            pDataContract.SequencePosition = pEntity.SequencePosition;
            if (pEntity.Employee != null)
            {
                pDataContract.Employee = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Employee);
            }

            return pDataContract;
        }

        public override ResourceBooking MergeWithEntity(ResourceBookingDC pDataContract, ResourceBooking pEntity)
        {
            this.ConcurrencyCheck(pDataContract.ResourceBookingVersion, pEntity);

            pEntity.Oid = pDataContract.ResourceBookingOid;
            if (pDataContract.Duration != null)
            {
                pEntity.Start = pDataContract.Duration.StartDateTime;
                pEntity.End = pDataContract.Duration.EndDateTime;
            }

            pEntity.Notice = pDataContract.Notice;
            pEntity.SequencePosition = pDataContract.SequencePosition;
            pEntity.IsDeleted = pDataContract.IsDeleted;
            if (pDataContract.Employee != null)
            {
                pEntity.Employee = MapperFactory.CompactEmployeeDC_Employee.MapToNewEntity(pDataContract.Employee);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(ResourceBookingDC pDC, ResourceBooking pEntity)
        {
            if (pDC.ResourceBookingOid == null)
            {
                return false;
            }

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