using System;
using System.Linq;

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

using BS.Shared.DataContracts;

using DevExpress.XtraCharts.Native;

namespace BeWo.Service.DCEntityMapper
{
	public class SchedulerAppointmentDC_SchedulerAppointment : AbstractIDCEntityMapper<SchedulerAppointment, SchedulerAppointmentDC>
	{
		public override SchedulerAppointmentDC MergeWithDC(SchedulerAppointment pEntity, SchedulerAppointmentDC pDataContract)
		{
			pDataContract.SchedulerAppointmentOid = pEntity.Oid;
			pDataContract.NewSchedulerAppointmentVersion = pEntity.Version;

			pDataContract.AllDay = pEntity.AllDay;
			pDataContract.EndDate = pEntity.EndDate;
			pDataContract.StartDate = pEntity.StartDate;
			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;

			pDataContract.IsPrivate = pEntity.IsPrivate;
			pDataContract.CustomerList = MapperFactory.CompactCustomerDC_Customer.MapToNewDCs(pEntity.CustomerList);
			pDataContract.ResourceList = MapperFactory.ResourceDC_Resource.MapToNewDCs(pEntity.ResourceList);
			pDataContract.EmployeeList = MapperFactory.Employee2SchedulerAppointmentDC_Employee2SchedulerAppointment.MapToNewDCs(pEntity.EmployeeList);

			pDataContract.FormerBookingSequenceOid = pEntity.FormerBookingSequenceOid;

			if (pEntity.Originator != null)
				pDataContract.Originator = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Originator);

			return pDataContract;
		}

		public override SchedulerAppointment MergeWithEntity(SchedulerAppointmentDC pDataContract, SchedulerAppointment pEntity)
		{
			ConcurrencyCheck(pDataContract.NewSchedulerAppointmentVersion, pEntity);

			pEntity.Oid = pDataContract.SchedulerAppointmentOid;
			pEntity.Version = pDataContract.NewSchedulerAppointmentVersion;

			pEntity.AllDay = pDataContract.AllDay;
			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;

			pEntity.FormerBookingSequenceOid = pDataContract.FormerBookingSequenceOid;

			if (pDataContract.Originator != null)
				pEntity.Originator = MapperFactory.CompactEmployeeDC_Employee.MapToNewEntity(pDataContract.Originator);

			pEntity.IsPrivate = pDataContract.IsPrivate;

			try
			{
				pEntity.EmployeeList = DAOFactory.GenericDAO.LoadByIDs<Employee2SchedulerAppointment>(pDataContract.EmployeeList.Select(e => e.Employee2SchedulerAppointmentOid.Value));
				pEntity.CustomerList = DAOFactory.GenericDAO.LoadByIDs<Customer>(pDataContract.CustomerList.Select(c => c.CustomerOid));
				pEntity.ResourceList = DAOFactory.GenericDAO.LoadByIDs<Resource>(pDataContract.ResourceList.Select(r => r.ResourceOid.Value));
			}
			catch (Exception e)
			{
				throw new InternalException(e.Message);
			}

			return pEntity;
		}

		protected override bool AreDCAndEntityEqual(SchedulerAppointmentDC pDC, SchedulerAppointment pEntity)
		{
			if (pDC.SchedulerAppointmentOid == null)
				return false;

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