using System.Linq;

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

using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class AccountingTransactionDC_AccountingTransaction : AbstractIDCEntityMapper<AccountingTransaction, AccountingTransactionDC>
    {
        public override AccountingTransactionDC MergeWithDC(AccountingTransaction pEntity, AccountingTransactionDC pDataContract)
        {
            pDataContract.AccountingTransactionOid = pEntity.Oid;
            pDataContract.AccountingTransactionVersion = pEntity.Version;
            pDataContract.Amount = pEntity.Amount;
            pDataContract.InsertedOn = pEntity.InsTs;
            pDataContract.InsUser = pEntity.InsUser;

            var lCat = pEntity.ValueList.SingleOrDefault(entry => entry.Entry.Type == ValueListEntryType.AccountingTransactionType);
            if (lCat != null)
            {
                pDataContract.Category = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(lCat.Entry);
            }

            if (pEntity.CostBearer2SupportConcept != null)
            {
                pDataContract.SupportConceptCostBearerRelDC = MapperFactory.SupportConceptCostBearerRelDC_CostBearer2SupportConcept.MapToNewDC(pEntity.CostBearer2SupportConcept);
            }

            pDataContract.Notice = pEntity.Notice;
            pDataContract.BookingDate = pEntity.BookingDate;
            pDataContract.ValidityDate = pEntity.ValidityDate;

            return pDataContract;
        }

        public override AccountingTransaction MergeWithEntity(AccountingTransactionDC pDataContract, AccountingTransaction pEntity)
        {
            this.ConcurrencyCheck(pDataContract.AccountingTransactionVersion, pEntity);

            pEntity.Oid = pDataContract.AccountingTransactionOid;
            pEntity.Amount = pDataContract.Amount;
            pEntity.BookingDate = pDataContract.BookingDate;
            pEntity.ValidityDate = pDataContract.ValidityDate;
            pEntity.Notice = pDataContract.Notice;

            MapperFactory.ValueListEntryDC_ValueListEntry.MapUniqueTypeValueListEntryBack2Entity(pDataContract.Category, pEntity.ValueList, ValueListEntryType.AccountingTransactionType, TableID.AccountingTransaction);

            if (pDataContract.SupportConceptCostBearerRelDC != null)
            {
                pEntity.CostBearer2SupportConcept = DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(pDataContract.SupportConceptCostBearerRelDC.CostBearer2SupportConceptOid.Value);
            }
            else
            {
                pEntity.CostBearer2SupportConcept = null;
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(AccountingTransactionDC pDC, AccountingTransaction pEntity)
        {
            if (pDC.AccountingTransactionOid == null)
            {
                return false;
            }

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