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>
/// Provides row-level mapping contributions to the entity mapping process by implementing the IEntityMapContributor interface.
/// </summary>
public class RowMapContributor : IEntityMapContributor
{
    /// <summary>
    /// Registers BSON class maps for <see cref="RowBoxLayout"/>, <see cref="RowDetailsConfig"/>, and <see cref="ObservationRowBoxLayout"/>, configuring default values, enum string serialization, and null-ignoring behavior for nullable members. Each map is registered only if no class map has already been registered for the corresponding type.
    /// </summary>
    public void RegisterMaps()
        {
            if (!BsonClassMap.IsClassMapRegistered(typeof(RowBoxLayout)))
                BsonClassMap.RegisterClassMap<RowBoxLayout>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.GrowPriority).SetDefaultValue(1D);
                    cm.MapMember(c => c.Type)
                        .SetDefaultValue(DisplayConfigEnums.CellType.Default)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.CellType>(BsonType.String));
                    cm.MapMember(c => c.SubType)
                        .SetDefaultValue(DisplayConfigEnums.WebDisplayCellSubtype.Default)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.WebDisplayCellSubtype>(BsonType.String));
                    cm.MapMember(c => c.BgColor).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.Observations)
                        .SetDefaultValue(new List<ObservationRowBoxLayout>());
                    cm.MapMember(c => c.Direction)
                        .SetDefaultValue(DisplayConfigEnums.DirectionEnum.Row)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.DirectionEnum>(BsonType.String));
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(RowDetailsConfig)))
                BsonClassMap.RegisterClassMap<RowDetailsConfig>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.Cells).SetDefaultValue(new List<CellDetails>());
                    cm.MapMember(c => c.Rows).SetDefaultValue(new List<RowDetailsConfig>());
                    cm.MapMember(c => c.GrowPriority).SetDefaultValue(1.0);
                    cm.MapMember(c => c.Title).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.DialogConfig).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Type).SetDefaultValue(DisplayConfigEnums.RowType.Demographic)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.RowType>(BsonType.String));
                });
    
            if (!BsonClassMap.IsClassMapRegistered(typeof(ObservationRowBoxLayout)))
                BsonClassMap.RegisterClassMap<ObservationRowBoxLayout>(cm =>
                {
                    cm.AutoMap();
                    cm.MapMember(c => c.TextRules).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ChartSettings).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ShowTitle).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.GrowPriority).SetDefaultValue(1D);
                    cm.MapMember(c => c.Type)
                        .SetDefaultValue(DisplayConfigEnums.CellType.Default)
                        .SetSerializer(new EnumSerializer<DisplayConfigEnums.CellType>(BsonType.String));
                    cm.MapMember(c => c.SubType).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Name).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Icon).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Title).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.GridColumn).SetDefaultValue(1);
                    cm.MapMember(c => c.IsColumn).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.IsStatic).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.Observations).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BgColor).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.Border).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.BorderRadius).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ValuePathKey).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.ValuePathNested).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Size).SetDefaultValue(15.0);
                    cm.MapMember(c => c.Format).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Length).SetIgnoreIfNull(true);
                    cm.MapMember(c => c.Direction)
                        .SetIgnoreIfNull(true)
                        .SetSerializer(new NullableSerializer<DisplayConfigEnums.DirectionEnum>(
                            new EnumSerializer<DisplayConfigEnums.DirectionEnum>(BsonType.String)));
                });
        }
}