using BS.Shared;
using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class ServiceAccountingDC_ServiceAccounting : AbstractIDCEntityMapper<ServiceAccounting, ServiceAccountingDC>
    {
        public override ServiceAccountingDC MergeWithDC(ServiceAccounting pEntity, ServiceAccountingDC pDataContract)
        {
            pDataContract.ServiceAccountingOid = pEntity.Oid;
            pDataContract.ServiceAccountingVersion = pEntity.Version;
            pDataContract.IsDefault = pEntity.IsDefault;
            pDataContract.ServiceDescription = MapperFactory.ServiceDescriptionDC_ServiceDescription.MapToNewDC(pEntity.ServiceDescription);
            pDataContract.BillableAmount = pEntity.BillableAmount;
            pDataContract.AccountingInterval = pEntity.AccountingInterval;


            return pDataContract;
        }

        public override ServiceAccounting MergeWithEntity(ServiceAccountingDC pDataContract, ServiceAccounting pEntity)
        {
            this.ConcurrencyCheck(pDataContract.ServiceAccountingVersion, pEntity);
            pEntity.Oid = pDataContract.ServiceAccountingOid;
            pEntity.BillableAmount = pDataContract.BillableAmount;
            pEntity.AccountingInterval = pDataContract.AccountingInterval;
            pEntity.IsDefault = pEntity.IsDefault;

            if (pDataContract.ServiceDescription.ServiceDescriptionOid.HasValue)
            {
                pEntity.ServiceDescription =
                    DAOFactory.GenericDAO.LoadByID<ServiceDescription>(
                        pDataContract.ServiceDescription.ServiceDescriptionOid.Value);
                pEntity.ServiceDescription.Name = pDataContract.ServiceDescription.Name;
            }
            else
            {
                pEntity.ServiceDescription =
                    MapperFactory.ServiceDescriptionDC_ServiceDescription.MapToNewEntity(pDataContract.ServiceDescription);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(ServiceAccountingDC pDC, ServiceAccounting pEntity)
        {
            if (pDC.ServiceAccountingOid == null)
            {
                return false;
            }

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