using System.Linq;

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

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

namespace BeWo.Service.DCEntityMapper
{
    public class CompactCustomerDC_Customer : AbstractIDCEntityMapper<Customer, CompactCustomerDC>
    {
        public override CompactCustomerDC MergeWithDC(Customer pEntity, CompactCustomerDC pDataContract)
        {
            pDataContract.CustomerOid         = pEntity.Oid.Value;
            pDataContract.PersonOid           = pEntity.Person.Oid.Value;
            pDataContract.CustomerVersion     = pEntity.Version.Value;
            pDataContract.FirstName           = pEntity.Person.FirstName;
            pDataContract.LastName            = pEntity.Person.LastName;
            pDataContract.DateOfBirth         = pEntity.Person.DateOfBirth;
			pDataContract.Sex                 = pEntity.Person.Sex;
            pDataContract.ActivationType      = pEntity.IsActive;
            pDataContract.TerminationDate     = pEntity.TerminationDate;
            pDataContract.AssistanceBegin     = pEntity.AssistanceBegin;
            pDataContract.EquityContribution  = pEntity.EquityContribution;
            pDataContract.CustomerAlias       = pEntity.CustomerAlias;

            if (pEntity.Person.Address != null)
            {
                pDataContract.Street     = pEntity.Person.Address.Street;
                pDataContract.PostalCode = pEntity.Person.Address.PostalCode;
                pDataContract.Town       = pEntity.Person.Address.Town;
            }

            pDataContract.AbsenceTimes = MapperFactory.AbsenceTimeDC_AbsenceTime.MapToNewDCs(pEntity.AbsenceTimes);

            pEntity.Customer2CostBearerList.DoForEach(c2cb => pDataContract.CostBearerReferenceNumbers[c2cb.CostBearer.Oid.Value] = c2cb.ReferenceNumber);

	        pDataContract.RelatedTeamOids = pEntity.Team2CustomerList.Select(t2c => t2c.TeamOid.Value).ToList();

            return pDataContract;
        }

        // Are not editable
        public override Customer MergeWithEntity(CompactCustomerDC pDataContract, Customer pEntity)
        {
            return DAOFactory.GenericDAO.LoadByID<Customer>(pDataContract.CustomerOid);
        }

        protected override bool AreDCAndEntityEqual(CompactCustomerDC pDC, Customer pEntity)
        {
            return pDC.CustomerOid == pEntity.Oid;
        }
    }
}