using System;
using BeWo.Data.Entities;

using BS.Shared.DataContracts;
using BeWo.Data.Access;


namespace BeWo.Service.DCEntityMapper
{
    public class AppointmentDC_Appointment : AbstractIDCEntityMapper<Appointment, AppointmentDC>
    {
        public override AppointmentDC MergeWithDC(Appointment pEntity, AppointmentDC pDataContract)
        {
            pDataContract.AppointmentOid = pEntity.Oid;
            pDataContract.AppointmentVersion = pEntity.Version;
            pDataContract.AllDay = pEntity.AllDay;
            pDataContract.CustomerOid = pEntity.CustomerOid;
            pDataContract.EmployeeOid = pEntity.EmployeeOid;
            pDataContract.EndDate = pEntity.EndDate ?? DateTime.MaxValue;
            pDataContract.StartDate = pEntity.StartDate ?? DateTime.MinValue;
            pDataContract.Status = pEntity.Status;
            pDataContract.Subject = pEntity.Subject;
            pDataContract.Type = pEntity.Type;
            pDataContract.Location = pEntity.Location;
            pDataContract.Description = pEntity.Notice;
            pDataContract.RecurrenceInfo = pEntity.RecurrenceInfo;
            pDataContract.ReminderInfo = pEntity.ReminderInfo;
            
            if (pEntity.AppointmentCategory != null)
                pDataContract.AppointmentCategory = MapperFactory.AppointmentCategoryDC_AppointmentCategory.MapToNewDC(pEntity.AppointmentCategory);

            return pDataContract;
        }

        public override Appointment MergeWithEntity(AppointmentDC pDataContract, Appointment pEntity)
        {
            ConcurrencyCheck(pDataContract.AppointmentVersion, pEntity);

            pEntity.Oid = pDataContract.AppointmentOid;
            pEntity.AllDay = pDataContract.AllDay;
            pEntity.CustomerOid = pDataContract.CustomerOid;
            pEntity.EmployeeOid = pDataContract.EmployeeOid;
            pEntity.EndDate = pDataContract.EndDate;
            pEntity.StartDate = pDataContract.StartDate;
            pEntity.Status = pDataContract.Status;
            pEntity.Subject = pDataContract.Subject;
            pEntity.Type = pDataContract.Type;
            pEntity.Location = pDataContract.Location;
            pEntity.Notice = pDataContract.Description;
            pEntity.RecurrenceInfo = pDataContract.RecurrenceInfo;
            pEntity.ReminderInfo = pDataContract.ReminderInfo;

	        if (pDataContract.AppointmentCategory != null)
		        pEntity.AppointmentCategory = pDataContract.AppointmentCategory.AppointmentCategoryOid.HasValue
			        ? DAOFactory.GenericDAO.LoadByID<AppointmentCategory>(pDataContract.AppointmentCategory.AppointmentCategoryOid.Value)
			        : null;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(AppointmentDC pDC, Appointment pEntity)
        {
            if (pDC.AppointmentOid == null)
                return false;

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