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

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

namespace BeWo.Service.DCEntityMapper
{
    public class CustomerEmployeeRelDC_Employee2CustomerMapper : AbstractIDCEntityMapper<Employee2Customer, CustomerEmployeeRelationDC>
    {
        public override CustomerEmployeeRelationDC MergeWithDC(Employee2Customer pEntity, CustomerEmployeeRelationDC pDataContract)
        {
            if (pEntity.ValueList.Count > 0)
            {
                pDataContract.RelationRole = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.ValueList[0].Entry);
            }

            pDataContract.Employee = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Employee);
            pDataContract.Customer = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(pEntity.Customer);
            pDataContract.Employee2CustomerOid = pEntity.Oid;
            pDataContract.Employee2CustomerVersion = pEntity.Version;
            pDataContract.Notice = pEntity.Notice;

            return pDataContract;
        }

        public override Employee2Customer MergeWithEntity(CustomerEmployeeRelationDC pDataContract, Employee2Customer pEntity)
        {
            this.ConcurrencyCheck(pDataContract.Employee2CustomerVersion, pEntity);

            MapperFactory.ValueListEntryDC_ValueListEntry.MapUniqueTypeValueListEntryBack2Entity(pDataContract.RelationRole, pEntity.ValueList, ValueListEntryType.StaffRoleType, TableID.Employee2Customer);

            if (pDataContract.Employee != null)
            {
                pEntity.Employee = DAOFactory.GenericDAO.LoadByID<Employee>(pDataContract.Employee.EmployeeOid);
            }

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

            pEntity.Oid = pDataContract.Employee2CustomerOid;
            pEntity.Notice = pDataContract.Notice;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(CustomerEmployeeRelationDC pDC, Employee2Customer pEntity)
        {
            if (pDC.Employee2CustomerOid == null)
            {
                return false;
            }

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