using System;
using BeWo.Data.Entities;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class ServiceRecordHistoryDC_ServiceRecordHistory : AbstractIDCEntityMapper<ServiceRecordHistory, ServiceRecordHistoryDC>
    {
        public override ServiceRecordHistoryDC MergeWithDC(ServiceRecordHistory pEntity, ServiceRecordHistoryDC pDataContract)
        {
            pDataContract.ServiceRecordOid = pEntity.ServiceRecordOid;
            pDataContract.Start = pEntity.Start;
            pDataContract.End = pEntity.End;
            pDataContract.Notice = pEntity.Notice;
            pDataContract.Notice2 = pEntity.Notice2;
            pDataContract.Notice3 = pEntity.Notice3;
            pDataContract.Notice4 = pEntity.Notice4;
            pDataContract.Notice5 = pEntity.Notice5;

            pDataContract.TimeStamp = pEntity.TimeStamp;
            pDataContract.ChangeType = pEntity.ChangeType;
			pDataContract.CostBearer2SupportConceptOid = pEntity.CostBearer2SupportConceptOid;

			pDataContract.End = pEntity.End;
			pDataContract.GroupOid = pEntity.GroupOid;
			pDataContract.GroupEmployeeCount = pEntity.GroupEmployeeCount;
			pDataContract.GroupPersonCount = pEntity.GroupPersonCount;
			pDataContract.GroupRoundedDuration = pEntity.GroupRoundedDuration;
			pDataContract.IP = pEntity.IP;
			pDataContract.Notice = pEntity.Notice;
			pDataContract.RoundedDuration = pEntity.RoundedDuration;
			pDataContract.ServiceRecordType = pEntity.ServiceRecordType;
			pDataContract.ServiceRecordInsertedOn = pEntity.InsTs;
			pDataContract.ServiceRecordVersion = pEntity.Version.Value;
			pDataContract.InsUser = pEntity.InsUser;
			pDataContract.ServiceRecordInsUser = pEntity.ServiceRecordInsUser;
			pDataContract.ServiceRecordUpdUser = pEntity.UdpUser;
			pDataContract.Start = pEntity.Start;
	        pDataContract.DistanceInMeter = pEntity.DistanceInMeter;
			pDataContract.Relevance = pEntity.Relevance;
	        pDataContract.IsCreatedInMobileClient = pEntity.IsCreatedInMobileClient;

	        try
	        {
				if (pEntity.CostBearer2SupportConcept != null && pEntity.CostBearer2SupportConcept.CostBearer != null)
				{
					pDataContract.Organisation =
						MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(
							pEntity.CostBearer2SupportConcept.CostBearer.Organisation);


				}
	        }
	        catch (Exception ex)
	        {
		        //ignore any errors. CostBearer2SupportConcept könnte zwischenzeitlich gelöscht worden sein
	        }
			

	        if (pEntity.Customer != null)
	        {
		        pDataContract.Customer = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(pEntity.Customer);
	        }

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

	        if (pEntity.Group != null)
	        {
		        pDataContract.Group = MapperFactory.ServiceRecordGroupDC_ServiceRecordGroup.MapToNewDC(pEntity.Group);
	        }

	        if (pEntity.ServiceDescription != null)
	        {
		        pDataContract.ServiceDescription =
			        MapperFactory.ServiceDescriptionDC_ServiceDescription.MapToNewDC(pEntity.ServiceDescription);
	        }

			if (pEntity.SupportConcept != null)
	        {
		        pDataContract.SupportConcept =
			        MapperFactory.CompactSupportConceptDC_SupportConcept.MapToNewDC(pEntity.SupportConcept);
	        }

	        return pDataContract;
        }

        public override ServiceRecordHistory MergeWithEntity(ServiceRecordHistoryDC pDataContract, ServiceRecordHistory pEntity)
        {
            return null;
        }

        protected override bool AreDCAndEntityEqual(ServiceRecordHistoryDC pDC, ServiceRecordHistory pEntity)
        {
            if (pDC.ServiceRecordHistoryOid == null)
            {
                return false;
            }

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