using System.Linq;

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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class InvoiceItemDC_InvoiceItem : AbstractIDCEntityMapper<InvoiceItem, InvoiceItemDC>
    {
        public override InvoiceItemDC MergeWithDC(InvoiceItem pEntity, InvoiceItemDC pDataContract)
        {
            pDataContract.AmountTotal = pEntity.AmountTotal;
            pDataContract.AmountPerUnit = pEntity.AmountPerUnit;
            pDataContract.UnitDescription = pEntity.UnitDescription;
            pDataContract.UnitCount = pEntity.UnitCount;
            pDataContract.ItemDescription = pEntity.ItemDescription;
            pDataContract.ItemPeriodEnd = pEntity.ItemPeriodEnd;
            pDataContract.ItemPeriodStart = pEntity.ItemPeriodStart;
            pDataContract.InvoiceItemOid = pEntity.Oid;
            pDataContract.InvoiceItemVersion = pEntity.Version;
            pDataContract.GrossAmountTotal = pEntity.GrossAmountTotal;
            pDataContract.RateFactor = pEntity.RateFactor;
            pDataContract.SupportConceptApprovalPeriodOid = pEntity.SupportConceptApprovalPeriodOid;
            pDataContract.AccountingInterval = pEntity.AccountingInterval;
            pDataContract.ReceivedAmount = pEntity.ReceivedAmount;
            pDataContract.ApprovedPerUnit = pEntity.ApprovedPerUnit;
            pDataContract.ApprovalUnit = pEntity.ApprovalUnit;
            pDataContract.MaxApprovedUnitCount = pEntity.MaxApprovedUnitCount;
            pDataContract.MaxApprovedAmount = pEntity.MaxApprovedAmount;
			pDataContract.Notice = pEntity.Notice;
            //pDataContract.CostRatePeriodOids2Versions = pEntity.CostRatePeriodList.ToDictionary(i => i.Oid.Value, i => i.Version.Value);

            if (pEntity.ServiceRecord != null)
                pDataContract.ServiceRecord = MapperFactory.ServiceRecordDC_ServiceRecord.MapToNewDC(pEntity.ServiceRecord);
            return pDataContract;
        }

        public override InvoiceItem MergeWithEntity(InvoiceItemDC pDataContract, InvoiceItem pEntity)
        {
            this.ConcurrencyCheck(pDataContract.InvoiceItemVersion, pEntity);
            pEntity.Oid = pDataContract.InvoiceItemOid;
            pEntity.AmountTotal = pDataContract.AmountTotal;
            pEntity.AmountPerUnit = pDataContract.AmountPerUnit;
            pEntity.UnitDescription = pDataContract.UnitDescription;
            pEntity.UnitCount = pDataContract.UnitCount;
            pEntity.ItemDescription = pDataContract.ItemDescription;
            pEntity.ItemPeriodEnd = pDataContract.ItemPeriodEnd;
            pEntity.ItemPeriodStart = pDataContract.ItemPeriodStart;
            pEntity.RateFactor = pDataContract.RateFactor;
            pEntity.GrossAmountTotal = pDataContract.GrossAmountTotal;
            pEntity.SupportConceptApprovalPeriodOid = pDataContract.SupportConceptApprovalPeriodOid;
            pEntity.AccountingInterval = pDataContract.AccountingInterval;
            pEntity.ReceivedAmount = pDataContract.ReceivedAmount;
            pEntity.MaxApprovedUnitCount = pDataContract.MaxApprovedUnitCount;
            pEntity.ApprovedPerUnit = pDataContract.ApprovedPerUnit;
            pEntity.ApprovalUnit = pDataContract.ApprovalUnit;
            pEntity.MaxApprovedAmount = pDataContract.MaxApprovedAmount;
			pEntity.Notice = pDataContract.Notice;

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

            //pEntity.CostRatePeriodList.Clear();
            //pDataContract.CostRatePeriodOids2Versions.ToList()
            //    .ForEach(kvp =>
            //             {
            //                 var cr = DAOFactory.GenericDAO.LoadByID<CostRatePeriod>(kvp.Key);
            //                 MapperFactory.CostRatePeriodDC_CostRatePeriod.ConcurrencyCheck(kvp.Value, cr);
            //                 pEntity.CostRatePeriodList.Add(cr);
            //             });

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(InvoiceItemDC pDC, InvoiceItem pEntity)
        {
            if (pDC.InvoiceItemOid == null)
            {
                return false;
            }

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