using System.Collections.Generic;
using BS.Shared.DataContracts.Compact;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class AdditionalServiceGroupOfPeopleRelationDC_AdditionalService2GroupOfPeople : AbstractIDCEntityMapper<AdditionalService2GroupOfPeople, AdditionalServiceGroupOfPeopleRelationDC>
    {
        public override AdditionalServiceGroupOfPeopleRelationDC MergeWithDC(AdditionalService2GroupOfPeople pEntity, AdditionalServiceGroupOfPeopleRelationDC pDataContract)
        {
            pDataContract.AdditionalService2GroupOfPeopleNotice = pEntity.Notice;
            pDataContract.AdditionalService2GroupOfPeopleOid = pEntity.Oid;
            pDataContract.AdditionalService2GroupOfPeopleVersion = pEntity.Version;

            pDataContract.RoundedDuration = pEntity.RoundedDuration;
            pDataContract.StartDate = pEntity.StartDate;
            pDataContract.EndDate = pEntity.EndDate;
            pDataContract.Date = pEntity.Date;

            pDataContract.Participations = new Dictionary<CompactCustomerDC, bool>();

            foreach (var kvpair in pEntity.Participations)
            {
                var c = kvpair.Key;
                pDataContract.Participations.Add(MapperFactory.CompactCustomerDC_Customer.MapToNewDC(c), kvpair.Value);
            }
                

            pDataContract.AdditionalService = MapperFactory.AdditionalServiceDC_AdditionalService.MapToNewDC(pEntity.AdditionalService);
            pDataContract.AdditionalServiceGroupOfPeople = MapperFactory.AdditionalServiceGroupOfPeopleCD_AdditionalServiceGroupOfPeople.MapToNewDC(pEntity.AdditionalServiceGroupOfPeople);

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

            return pDataContract;
        }

        public override AdditionalService2GroupOfPeople MergeWithEntity(AdditionalServiceGroupOfPeopleRelationDC pDataContract, AdditionalService2GroupOfPeople pEntity)
        {
            pEntity.Oid = pDataContract.AdditionalService2GroupOfPeopleOid;
            pEntity.Notice = pDataContract.AdditionalService2GroupOfPeopleNotice;
            pEntity.Version = pDataContract.AdditionalService2GroupOfPeopleVersion;
            pEntity.RoundedDuration = pDataContract.RoundedDuration;
            pEntity.StartDate = pDataContract.StartDate;
            pEntity.EndDate = pDataContract.EndDate;
            pEntity.Date = pDataContract.Date;

            pEntity.Participations = new Dictionary<Customer, bool>();
            foreach (var kv in pDataContract.Participations)
            {
                CompactCustomerDC c = kv.Key;
                pEntity.Participations.Add(MapperFactory.CompactCustomerDC_Customer.MapToNewEntity(c), kv.Value);
            }

            pEntity.AdditionalService =
                DAOFactory.GenericDAO.LoadByID<AdditionalService>(
                    pDataContract.AdditionalService.AdditionalServiceOid.Value);

            pEntity.AdditionalServiceGroupOfPeople =
                DAOFactory.GenericDAO.LoadByID<AdditionalServiceGroupOfPeople>(pDataContract.AdditionalServiceGroupOfPeople.AdditionalServiceGroupOfPeopleOid.Value);

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

            
            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(AdditionalServiceGroupOfPeopleRelationDC pDC, AdditionalService2GroupOfPeople pEntity)
        {
            if (pDC.AdditionalService2GroupOfPeopleOid == null)
                return false;

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