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


namespace BeWo.Service.DCEntityMapper
{
    public class CustomerTeamRelDC_Team2CustomerMapper : AbstractIDCEntityMapper<Team2Customer, CustomerTeamRelationDC>
    {
        public override CustomerTeamRelationDC MergeWithDC(Team2Customer pEntity, CustomerTeamRelationDC pDataContract)
        {
            if (pEntity.ValueList.Count > 0)
            {
                pDataContract.RelationRole = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.ValueList[0].Entry);
            }
            pDataContract.Team = MapperFactory.CompactTeamDC_Team.MapToNewDC(pEntity.Team);
            pDataContract.Customer = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(pEntity.Customer);
            pDataContract.Team2CustomerOid = pEntity.Oid;    
            pDataContract.Team2CustomerVersion = pEntity.Version;
            pDataContract.Notice = pEntity.Notice;

            return pDataContract;
        }

        public override Team2Customer MergeWithEntity(CustomerTeamRelationDC pDataContract, Team2Customer pEntity)
        {
            ConcurrencyCheck(pDataContract.Team2CustomerVersion, pEntity);

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

            if (pDataContract.Team != null)
                pEntity.Team = DAOFactory.GenericDAO.LoadByID<Team>(pDataContract.Team.TeamOid);
            if (pDataContract.Customer != null)
                pEntity.Customer = DAOFactory.GenericDAO.LoadByID<Customer>(pDataContract.Customer.CustomerOid);
            pEntity.Oid = pDataContract.Team2CustomerOid;
            pEntity.Notice = pDataContract.Notice;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(CustomerTeamRelationDC pDC, Team2Customer pEntity)
        {
            if (pDC.Team2CustomerOid == null)
                return false;
           return pDC.Team2CustomerOid == pEntity.Oid;
        }
    }


}
