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

using BS.Shared.DataContracts;
using System.Collections.Generic;

namespace BeWo.Service.DCEntityMapper
{
    public class AssessmentSheetCategoryDC_AssessmentSheetCategory : AbstractIDCEntityMapper<AssessmentSheetCategory, AssessmentSheetCategoryDC>
    {
        public override AssessmentSheetCategoryDC MergeWithDC(AssessmentSheetCategory pEntity, AssessmentSheetCategoryDC pDataContract)
        {
            pDataContract.AssessmentSheetCategoryOid = pEntity.Oid;
            pDataContract.AssessmentSheetCategoryVersion = pEntity.Version;
            pDataContract.Description = pEntity.Description;
            pDataContract.PossibleValues = MapperFactory.AssessmentSheetValueDC_AssessmentSheetValue.MapToNewDCs(pEntity.PossibleValues);
            pDataContract.Position = pEntity.Position;
            pDataContract.ScopeType = pEntity.ScopeType.HasValue ? pEntity.ScopeType.Value : ScopeTypeId.Global;

            return pDataContract;
        }

        public override AssessmentSheetCategory MergeWithEntity(AssessmentSheetCategoryDC pDataContract, AssessmentSheetCategory pEntity)
        {
            this.ConcurrencyCheck(pDataContract.AssessmentSheetCategoryVersion, pEntity);

            pEntity.Description = pDataContract.Description;
            pEntity.Position = pDataContract.Position;
            pEntity.ScopeType = pDataContract.ScopeType;
            pEntity.PossibleValues = null;
            pEntity.PossibleValues = DAOFactory.GenericDAO.LoadByIDs<AssessmentSheetValue>(pDataContract.PossibleValues.Select(i => i.AssessmentSheetValueOid.Value));

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(AssessmentSheetCategoryDC pDC, AssessmentSheetCategory pEntity)
        {
            if (pDC.AssessmentSheetCategoryOid == null)
            {
                return false;
            }

            return pDC.AssessmentSheetCategoryOid == pEntity.Oid;
        }

        public override AssessmentSheetCategoryDC CreateNewDC()
        {
            return new AssessmentSheetCategoryDC {PossibleValues = new List<AssessmentSheetValueDC>()};

        }
    }
}