using adas_core.Domain.Enums;
using adas_core.Domain.Models.MongoModels;
using adas_core.Domain.Utils;
using adas_core.Infrastructure.Utils.MongoMaps.Interfaz;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
namespace adas_core.Infrastructure.Utils.MongoMaps;
///
/// Represents a contributor that provides device-related mapping logic for entities.
/// Implements the interface to participate in the entity mapping process.
///
public class DeviceMapContributor : IEntityMapContributor
{
///
/// Registers BSON class maps for , , , and types used in MongoDB serialization.
///
///
/// Each map is only registered if no class map is already registered for the type. The registration applies auto-mapping and customizes serialization for enum members (stored as strings), applies default values for required properties, and configures nullable members to be ignored when null.
///
public void RegisterMaps()
{
if (!BsonClassMap.IsClassMapRegistered(typeof(DeviceActionType)))
{
BsonClassMap.RegisterClassMap(cm => cm.AutoMap() );
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DeviceAction)))
{
BsonClassMap.RegisterClassMap(cm =>
{
cm.AutoMap();
cm.MapMember(c => c.Type)
.SetDefaultValue(DeviceActionType.Unknown)
.SetSerializer(new EnumSerializer(BsonType.String));
cm.MapMember(c => c.ConfigObservationId).SetIgnoreIfNull(true);
cm.MapMember(c => c.AlarmName).SetIgnoreIfNull(true);
cm.MapMember(c => c.ValueOnSingleClick)
.SetDefaultValue(new object())
.SetSerializer(new ComplexObjectValueTypeSerializer());
cm.MapMember(c => c.ValueOnDoubleClick)
.SetDefaultValue(new object())
.SetSerializer(new ComplexObjectValueTypeSerializer());
cm.MapMember(c => c.ValueOnHoldClick)
.SetDefaultValue(new object())
.SetSerializer(new ComplexObjectValueTypeSerializer());
}
);
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DeviceSettings)))
{
BsonClassMap.RegisterClassMap(cm =>
{
cm.AutoMap();
cm.MapMember(c => c.Action).SetDefaultValue(new DeviceAction());
}
);
}
if (!BsonClassMap.IsClassMapRegistered(typeof(Device)))
{
BsonClassMap.RegisterClassMap(cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id).SetElementName("_id");
cm.MapMember(c => c.DeviceType)
.SetDefaultValue(DeviceType.Unknown)
.SetSerializer(new EnumSerializer(BsonType.String));
cm.MapMember(c => c.Uuid).SetIgnoreIfNull(true);
cm.MapMember(c => c.MacAddr).SetIgnoreIfNull(true);
cm.MapMember(c => c.CreatedAt).SetDefaultValue(DateTime.UtcNow);
cm.MapMember(c => c.UpdatedAt).SetDefaultValue(DateTime.UtcNow);
cm.MapMember(c => c.Key).SetIgnoreIfNull(true);
cm.MapMember(c => c.Color).SetIgnoreIfNull(true);
cm.MapMember(c => c.Battery).SetIgnoreIfNull(true);
cm.MapMember(c => c.Name).SetIgnoreIfNull(true);
cm.MapMember(c => c.SerialNumber).SetIgnoreIfNull(true);
cm.MapMember(c => c.PointOfCareIds).SetDefaultValue(new List());
cm.MapMember(c => c.Connected).SetDefaultValue(false).SetIgnoreIfNull(true);
cm.MapMember(c => c.Ready).SetDefaultValue(false).SetIgnoreIfNull(true);
cm.MapMember(c => c.DeviceType)
.SetSerializer(new EnumSerializer(BsonType.String));
cm.MapMember(c => c.Settings).SetDefaultValue(new DeviceSettings());
}
);
}
}
}