using adas_core.Domain.Enums;
using adas_core.Domain.Models.GroupedObservations;
using Newtonsoft.Json;

namespace adas_core.Domain.Models.SignalR;

public class Message
{
    public long IdMessage { get; set; }

    public MessageData? Data { get; set; }

    public string? Version { get; set; }

    public List<GroupedField>? GroupedFields { get; set; }

    public string OperationStr
    {
        get => Operation.ToString("g");
        set => Operation = (SubscriptionEnum.Type)Enum.Parse(typeof(SubscriptionEnum.Type), value);
    }

    public SubscriptionEnum.Type Operation { get; set; }


    public override string? ToString()
    {
        try
        {
            return JsonConvert.SerializeObject(this, Formatting.None,
                new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        catch
        {
            return base.ToString();
        }
    }
}