using System;

using BeWo.Data.Entities;

using BS.Shared.DataContracts.Compact;

namespace BeWo.Service.DCEntityMapper
{
    public class CompactSupportConceptDC_SupportConcept : AbstractIDCEntityMapper<SupportConcept, CompactSupportConceptDC>
    {
        #region Public Methods

        public override CompactSupportConceptDC MergeWithDC(SupportConcept pEntity, CompactSupportConceptDC pDataContract)
        {
            pDataContract.SupportConceptOid = pEntity.Oid.Value;
            pDataContract.SupportConceptVersion = pEntity.Version.Value;
            pDataContract.Customer = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(pEntity.Customer);
            pDataContract.ActivationType = pEntity.IsActive;
            pDataContract.ConferenceDate = pEntity.ConferenceDate;

            foreach (var iCBRel in pEntity.CostBearer2SupportConceptList)
            {
                CompactCostBearerDC costBearer = new CompactCostBearerDC();

                if (iCBRel.CostBearer.Organisation != null)
                {
                    CompactOrganisationDC orgDC = new CompactOrganisationDC();
                    orgDC.Name = iCBRel.CostBearer.Organisation.Name;
                    orgDC.OrganisationOid = iCBRel.CostBearer.Organisation.Oid.Value;

                    if (iCBRel.CostBearer.Organisation.Address != null)
                    {
                        orgDC.Street = iCBRel.CostBearer.Organisation.Address.Street;
                        orgDC.PostalCode = iCBRel.CostBearer.Organisation.Address.PostalCode;
                        orgDC.Town = iCBRel.CostBearer.Organisation.Address.Town;
                    }

                    costBearer.Organisation = orgDC;
                }
                costBearer.CostBearerID = iCBRel.CostBearer.ID;
                costBearer.CostBearerOid = iCBRel.CostBearer.Oid.Value;
                costBearer.CostBearer2SupportConceptOid = iCBRel.Oid.Value;
                costBearer.SupportConceptStatus = iCBRel.Status;
                costBearer.RequestedStartDate = iCBRel.RequestedStartDate;
                costBearer.RequestedEndDate = iCBRel.RequestedEndDate;
                costBearer.ApprovedStartDate = iCBRel.ApprovedStartDate;
                costBearer.ApprovedEndDate = iCBRel.ApprovedEndDate;

// costBearer.ApprovedFLS = iCBRel.ApprovedFLS;
                // costBearer.ApprovedFLSTotal = iCBRel.ApprovedFLSTotal;
                costBearer.CustomerReferenceNumber = iCBRel.CustomerReferenceNumber;
                costBearer.IsCalculatingWithFactor = iCBRel.CostBearer.IsCalculatingWithFactor;

                pDataContract.IsApproved = iCBRel.ApprovedStartDate.HasValue && iCBRel.ApprovedEndDate.HasValue;
                pDataContract.CostBearerList.Add(costBearer);
                pDataContract.CostBearerOids2CostBearerRelOids.Add(costBearer.Oid.Value, iCBRel.Oid.Value);
                pDataContract.CustomerReferenceNumbers.Add(iCBRel.CustomerReferenceNumber);
                pDataContract.CostBearerRelOids.Add(iCBRel.Oid.Value);
            }

            return pDataContract;
        }

        public override SupportConcept MergeWithEntity(CompactSupportConceptDC pDataContract, SupportConcept pEntity)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Methods

        protected override bool AreDCAndEntityEqual(CompactSupportConceptDC pDC, SupportConcept pEntity)
        {
            return pDC.SupportConceptOid == pEntity.Oid;
        }

        #endregion
    }
}