using BeWo.Data.Entities;

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

namespace BeWo.Service.DCEntityMapper
{
    public class ResourceDC_Resource : AbstractIDCEntityMapper<Resource, ResourceDC>
    {
        public override ResourceDC MergeWithDC(Resource pEntity, ResourceDC pDataContract)
        {
            pDataContract.ResourceOid = pEntity.Oid;
            pDataContract.ResourceVersion = pEntity.Version;
            pDataContract.Name = pEntity.Name;
            pDataContract.Description = pEntity.Description;
            pDataContract.Color = pEntity.Color;

            pDataContract.IsSystemEntry = pEntity.SystemEntryID != null;

            ValueListEntry2Object lCategory = pEntity.ValueList.FindSingleByType(ValueListEntryType.ResourceCategory);

            if (lCategory != null)
            {
                pDataContract.ResourceCategoryAssoziationOid = lCategory.Oid;
                pDataContract.ResourceCategory = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(lCategory.Entry);
            }

            return pDataContract;
        }

        public override Resource MergeWithEntity(ResourceDC pDataContract, Resource pEntity)
        {
            ConcurrencyCheck(pDataContract.ResourceVersion, pEntity);

            MapperFactory.ValueListEntryDC_ValueListEntry.MapUniqueTypeValueListEntryBack2Entity(pDataContract.ResourceCategory, pEntity.ValueList, ValueListEntryType.ResourceCategory, TableID.Resource);

            pEntity.Oid = pDataContract.ResourceOid;
            pEntity.Name = pDataContract.Name;
            pEntity.Color = pDataContract.Color;
            pEntity.Description = pDataContract.Description;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(ResourceDC pDC, Resource pEntity)
        {
            if (pDC.ResourceOid == null)
            {
                return false;
            }

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