using adas_core.Domain.Enums;
using adas_core.Domain.Models.MongoModels;
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;

/// <summary>
/// Represents a contributor that participates in populating a cell-based map by implementing the entity map contribution contract.
/// </summary>
public class CellMapContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for <see cref="Cell"/>, <see cref="CellDetails"/>, <see cref="IconValueList"/>, <see cref="DialogConfig"/>, and <see cref="MedicalConfig"/> when they have not already been registered. Each registration configures default values, ignore-if-null behavior, and enum serialization for the corresponding type's members to control how instances are persisted in MongoDB.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(Cell)))
                BsonClassMap.RegisterClassMap<Cell>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Type)
                        .SetDefaultValue(DisplayConfigEnums.CellType.Default)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.CellType>(BsonType.String));
                    cm.MapMember(c => c.ChartSettings).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.GrowPriority).SetDefaultValue(1.0);
                    cm.MapMember(c => c.Name).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IconValueList)
                        .SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Border).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BorderRadius).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Direction)
                        .SetIgnoreIfNull(true)
                        .SetSerializer(new NullableSerializer<DisplayConfigEnums.DirectionEnum>(
                            new EnumSerializer<DisplayConfigEnums.DirectionEnum>(BsonType.String)));
                    cm.MapMember(c => c.SubType).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Size).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.HideName).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IsVisible).SetDefaultValue(true);
                    cm.MapMember(c => c.IsColumn).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BgColor).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Title).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ValuePathKey).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingTop).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingBottom).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingLeft).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingRight).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Color).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BackgroundColor).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ShowIcon).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.FlexBasis).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Grow).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Shrink).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ShowTitle).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IndicatorHorizontal).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.DialogConfig).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ShowIndicator).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.OnlyNumber).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ShowArrow).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.GraphConf).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Names).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ObservationName).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ObservationTitle).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.GraphConf).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ValuePathNested).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.SubObs).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.TextRules).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IsStatic).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(CellDetails)))
                BsonClassMap.RegisterClassMap<CellDetails>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.TextRules).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Type)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.CellType>(BsonType.String))
                        .SetDefaultValue(DisplayConfigEnums.CellType.Default);
                    cm.MapMember(c => c.ChartSettings).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.SubType).SetDefaultValue(string.Empty);
                    cm.MapMember(c => c.GrowPriority).SetDefaultValue(1.0);
                    cm.MapMember(c => c.Cells).SetDefaultValue(new List<CellDetails>());
                    cm.MapMember(c => c.Name).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Title).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IsVisible).SetDefaultValue(true);
                    cm.MapMember(c => c.ValuePathNested).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ValuePathKey).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IconValueList).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ObservationName).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingTop).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingBottom).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingLeft).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.PaddingRight).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Color).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BackgroundColor).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ShowIcon).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.FlexBasis).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.FlexDirection).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Grow).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Shrink).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ShowTitle).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.DialogConfig).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IsStatic).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(IconValueList)))
                BsonClassMap.RegisterClassMap<IconValueList>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.MinValue).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.MaxValue).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Width).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Height).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IconList).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(DialogConfig)))
                BsonClassMap.RegisterClassMap<DialogConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.IsDraggable).SetDefaultValue(false);
                    cm.MapMember(c => c.MedicalConfig).SetIgnoreIfNull(true);
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(MedicalConfig)))
                BsonClassMap.RegisterClassMap<MedicalConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.HasFinalizeTime).SetDefaultValue(true);
                    cm.MapMember(c => c.HasStartTime).SetDefaultValue(true);
                });
        }
}