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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class ServiceInvoicePeriodDC_ServiceInvoicePeriod : AbstractIDCEntityMapper<ServiceInvoicePeriod, ServiceInvoicePeriodDC>
    {
        public override ServiceInvoicePeriodDC MergeWithDC(ServiceInvoicePeriod pEntity, ServiceInvoicePeriodDC pDataContract)
        {
            pDataContract.ApprovedFixedAmount = pEntity.ApprovedFixedAmount;
            pDataContract.ApprovedHours = pEntity.ApprovedHours;
            pDataContract.End = pEntity.End;
            pDataContract.InvoiceItems = MapperFactory.InvoiceItemDC_InvoiceItem.MapToNewDCs(pEntity.InvoiceItemList);
            pDataContract.InvoiceItems.Sort((x, y) => x.ItemPeriodStart.Value.CompareTo(y.ItemPeriodStart.Value));
            pDataContract.ServiceInvoicePeriodOid = pEntity.Oid;
            pDataContract.ServiceInvoicePeriodVersion = pEntity.Version;
            pDataContract.Start = pEntity.Start;
            pDataContract.ServiceUnitName = pEntity.ServiceUnitName;
            pDataContract.ApprovedBE = pEntity.ApprovedBE;
            pDataContract.Claim = pEntity.Claim;
            pDataContract.ApprovedAmountDefaultHourlyRate = pEntity.ApprovedAmountDefaultHourlyRate;
            pDataContract.HoursNotBillableAbsence = pEntity.HoursNotBillableAbsence;
            pDataContract.HoursNotBillableNotApproved = pEntity.HoursNotBillableNotApproved;
            pDataContract.AccountingInterval = pEntity.AccountingInterval;

            if (pEntity.Customer != null)
                pDataContract.Customer = MapperFactory.CompactCustomerDC_CustomerCompact.MapToNewDC(pEntity.Customer);
	        if (pEntity.SupportConceptApprovalPeriod != null)
	        {
		        try
		        {
					pDataContract.SupportConceptApprovalPeriod =
						MapperFactory.SupportConceptApprovalPeriodDC_SupportConceptApprovalPeriod.MapToNewDC(
							pEntity.SupportConceptApprovalPeriod);
		        }
		        catch (Exception)
		        {
			        //ignore
		        }
		        
	        }
	        return pDataContract;
        }

        public override ServiceInvoicePeriod MergeWithEntity(ServiceInvoicePeriodDC pDataContract, ServiceInvoicePeriod pEntity)
        {
            this.ConcurrencyCheck(pDataContract.ServiceInvoicePeriodVersion, pEntity);

            pEntity.ApprovedFixedAmount = pDataContract.ApprovedFixedAmount;
            pEntity.ApprovedHours = pDataContract.ApprovedHours;
            pEntity.End = pDataContract.End;
            pEntity.Start = pDataContract.Start;
            pEntity.ServiceUnitName = pDataContract.ServiceUnitName;
            pEntity.ApprovedBE = pDataContract.ApprovedBE;
            pEntity.Claim = pDataContract.Claim;
            pEntity.ApprovedAmountDefaultHourlyRate = pDataContract.ApprovedAmountDefaultHourlyRate;
            pEntity.HoursNotBillableAbsence = pDataContract.HoursNotBillableAbsence;
            pEntity.HoursNotBillableNotApproved = pDataContract.HoursNotBillableNotApproved;
            pEntity.AccountingInterval = pDataContract.AccountingInterval;

            if (pDataContract.Customer != null)
                pEntity.Customer = DAOFactory.GenericDAO.LoadByID<Customer>(pDataContract.Customer.CustomerOid);
            else
                pEntity.Customer = null;

            if (pDataContract.SupportConceptApprovalPeriod != null)
                pEntity.SupportConceptApprovalPeriod = DAOFactory.GenericDAO.LoadByID<SupportConceptApprovalPeriod>(pDataContract.SupportConceptApprovalPeriod.SupportConceptApprovalPeriodOid.Value);
            else
                pEntity.SupportConceptApprovalPeriod = null;

            MapperFactory.InvoiceItemDC_InvoiceItem.MergeWithEntitys(pDataContract.InvoiceItems, pEntity.InvoiceItemList);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(ServiceInvoicePeriodDC pDC, ServiceInvoicePeriod pEntity)
        {
            if (pDC.ServiceInvoicePeriodOid == null)
            {
                return false;
            }

            return pDC.ServiceInvoicePeriodOid.Equals(pEntity.Oid);
        }
    }
}