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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class TeamDC_Team : AbstractIDCEntityMapper<Team, TeamDC>
    {
        public override TeamDC MergeWithDC(Team pEntity, TeamDC pDataContract)
        {
            pDataContract.Name = pEntity.Name;
            pDataContract.TeamOid = pEntity.Oid;
            pDataContract.TeamVersion = pEntity.Version;
            pDataContract.Notice = pEntity.Notice;
            pDataContract.Leader = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Leader);
            pDataContract.Member = MapperFactory.CompactEmployeeDC_Employee.MapToNewDCs(pEntity.MemberList);

            return pDataContract;
        }

        public override Team MergeWithEntity(TeamDC pDataContract, Team pEntity)
        {
            this.ConcurrencyCheck(pDataContract.TeamVersion, pEntity);

            pEntity.Oid = pDataContract.TeamOid;
            pEntity.Notice = pDataContract.Notice;
            pEntity.Name = pDataContract.Name;

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

            MapperFactory.CompactEmployeeDC_Employee.MergeWithEntitys(pDataContract.Member, pEntity.MemberList);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(TeamDC pDC, Team pEntity)
        {
            if (pDC.TeamOid == null)
            {
                return false;
            }

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