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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class InvoiceDC_Invoice : AbstractIDCEntityMapper<Invoice, InvoiceDC>
    {
        public override InvoiceDC MergeWithDC(Invoice pEntity, InvoiceDC pDataContract)
        {
            pDataContract.Amount = pEntity.Amount;
            pDataContract.InvoiceDate = pEntity.InvoiceDate;
            pDataContract.InvoiceNumber = pEntity.InvoiceNumber;
            pDataContract.InvoiceOid = pEntity.Oid;
            pDataContract.InvoiceVersion = pEntity.Version;
            pDataContract.InvoicingPeriodEnd = pEntity.InvoicingPeriodEnd;
            pDataContract.InvoicingPeriodStart = pEntity.InvoicingPeriodStart;
            pDataContract.Notice = pEntity.Notice;
            pDataContract.RecipientAddress = pEntity.RecipientAddress;
            pDataContract.RecipientName = pEntity.RecipientName;
            pDataContract.PaidDate = pEntity.PaidDate;

            if (pEntity.Organisation != null)
            {
                pDataContract.OrganisationOid = pEntity.Organisation.Oid;
            }

            if (pEntity.SupportConcept != null)
            {
                pDataContract.SupportConceptOid = pEntity.SupportConcept.Oid;
            }

            if (pEntity.Customer != null)
            {
                pDataContract.CustomerOid = pEntity.Customer.Oid;
            }

            return pDataContract;
        }

        public override Invoice MergeWithEntity(InvoiceDC pDataContract, Invoice pEntity)
        {
            this.ConcurrencyCheck(pDataContract.InvoiceVersion, pEntity);
            pEntity.Oid = pDataContract.InvoiceOid;
            pEntity.Notice = pDataContract.Notice;
            pEntity.Amount = pDataContract.Amount;
            pEntity.InvoiceDate = pDataContract.InvoiceDate;
            pEntity.InvoiceNumber = pDataContract.InvoiceNumber;
            pEntity.InvoicingPeriodEnd = pDataContract.InvoicingPeriodEnd;
            pEntity.InvoicingPeriodStart = pDataContract.InvoicingPeriodStart;
            pEntity.PaidDate = pDataContract.PaidDate;
            pEntity.RecipientName = pDataContract.RecipientName;
            pEntity.RecipientAddress = pDataContract.RecipientAddress;

            pEntity.Organisation = null;
            if (pDataContract.OrganisationOid.HasValue)
            {
                pEntity.Organisation = DAOFactory.GenericDAO.LoadByID<Organisation>(pDataContract.OrganisationOid.Value);
            }

            pEntity.SupportConcept = null;
            if (pDataContract.SupportConceptOid.HasValue)
            {
                pEntity.SupportConcept = DAOFactory.GenericDAO.LoadByID<SupportConcept>(pDataContract.SupportConceptOid.Value);
            }

            pEntity.Customer = null;
            if (pDataContract.CustomerOid.HasValue)
            {
                pEntity.Customer = DAOFactory.GenericDAO.LoadByID<Customer>(pDataContract.CustomerOid.Value);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(InvoiceDC pDC, Invoice pEntity)
        {
            if (pDC.InvoiceOid == null)
            {
                return false;
            }

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