using System.Collections.Generic;

using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;

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

namespace BeWo.Service.DCEntityMapper
{
    public class AdditionalServiceBookingDC_AdditionalServiceBooking : AbstractIDCEntityMapper<AdditionalServiceBooking, AdditionalServiceBookingDC>
    {
        public override AdditionalServiceBookingDC MergeWithDC(AdditionalServiceBooking pEntity, AdditionalServiceBookingDC pDataContract)
        {
            pDataContract.AdditionalServiceBookingOid = pEntity.Oid;
            pDataContract.AdditionalServiceBookingVersion = pEntity.Version;
            pDataContract.AdditionalServiceRegion = MapperFactory.AdditionalServiceRegionDC_AdditionalServiceRegion.MapToNewDC(pEntity.AdditionalServiceRegion);
            pDataContract.StartDate = pEntity.StartDate;
            pDataContract.EndDate = pEntity.EndDate;
            pDataContract.RoundedDuration = pEntity.RoundedDuration;
            pDataContract.Date = pEntity.Datum;

	        pDataContract.CustomerOidDict = new Dictionary<long, bool>();

	        if (pEntity.Customer2AddServiceBookings != null)
	        {
		        foreach (var c in pEntity.Customer2AddServiceBookings)
		        {
			        //Customer c = kvpair.Key;
			        pDataContract.CustomerOidDict.Add(c.CustomerOid, c.Participated);
		        }
	        }

	        if (pEntity.AdditionalServiceRegion != null)
                pDataContract.AdditionalServiceRegion = MapperFactory.AdditionalServiceRegionDC_AdditionalServiceRegion.MapToNewDC(pEntity.AdditionalServiceRegion);

	        pDataContract.EmployeeOidList = new List<long>();

	        if (pEntity.Employee2AddServiceBookings != null)
	        {
		        foreach (var e2a in pEntity.Employee2AddServiceBookings)
		        {
			        pDataContract.EmployeeOidList.Add(e2a.EmployeeOid);
		        }
	        }
				

            return pDataContract;
        }

        public override AdditionalServiceBooking MergeWithEntity(AdditionalServiceBookingDC pDataContract, AdditionalServiceBooking pEntity)
        {
            pEntity.Oid = pDataContract.AdditionalServiceBookingOid;
            pEntity.Notice = pDataContract.AdditionalServiceBookingNotice;
            pEntity.Version = pDataContract.AdditionalServiceBookingVersion;
            pEntity.RoundedDuration = pDataContract.RoundedDuration;
            pEntity.StartDate = pDataContract.StartDate;
            pEntity.EndDate = pDataContract.EndDate;
            pEntity.Datum = pDataContract.Date;

			if (pEntity.Customer2AddServiceBookings != null)
				pEntity.Customer2AddServiceBookings.Clear();
			else
				pEntity.Customer2AddServiceBookings = new List<Customer2AddServiceBooking>();

            foreach (var kv in pDataContract.CustomerOidDict)
            {
	            var newC2A = new Customer2AddServiceBooking();
	            newC2A.CustomerOid = kv.Key;
	            newC2A.Participated = kv.Value;

                pEntity.Customer2AddServiceBookings.Add(newC2A);
            }

            if (pDataContract.AdditionalServiceRegion != null)
                pEntity.AdditionalServiceRegion =
                    DAOFactory.GenericDAO.LoadByID<AdditionalServiceRegion>(pDataContract.AdditionalServiceRegion.AdditionalServiceRegionOid.Value);

			if (pEntity.Employee2AddServiceBookings != null)
				pEntity.Employee2AddServiceBookings.Clear();
			else
				pEntity.Employee2AddServiceBookings = new List<Employee2AddServiceBooking>();
			
			foreach (var eoid in pDataContract.EmployeeOidList)
			{
				var newE2A = new Employee2AddServiceBooking();
				newE2A.EmployeeOid = eoid;

				pEntity.Employee2AddServiceBookings.Add(newE2A);
			}
			//pEntity.EmployeeList = MapperFactory.CompactEmployeeDC_Employee.MapToNewEntities(pDataContract.EmployeeList);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(AdditionalServiceBookingDC pDC, AdditionalServiceBooking pEntity)
        {
            if (pDC.AdditionalServiceBookingOid == null)
                return false;

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