using System.Text.Json;
using System.Text.Json.Serialization;
using Quadarax.Foundation.Core.Json.Extensions;

namespace Quadarax.Foundation.Core.QMonitor
{
    public class MonData : IEquatable<MonData>
    {
        [JsonPropertyName("iid")]
        public string InstanceIdentifier { get; set; }

        [JsonPropertyName("nam")]
        public string? Name { get; set; }
        [JsonPropertyName("tms-ftutc")]
        public long TimestampFtUtc { get; set; }
        [JsonPropertyName("dta")]
        public string Data { get; set; } = string.Empty;

        public IDictionary<string, JsonDocumentExt.DiffOccurrence>? Diff(MonData other)
        {
            if (other == null) throw new ArgumentNullException(nameof(other));

            var thisDoc = JsonDocument.Parse(Data);
            var otherDoc = JsonDocument.Parse(other.Data);
            thisDoc.Diff(otherDoc);
            return thisDoc.Diff(otherDoc);
        }

        public JsonDocument DataToJsonDocument()
        {
            return JsonDocument.Parse(Data);
        } 
        public bool Equals(MonData? other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return InstanceIdentifier == other.InstanceIdentifier && Name == other.Name && Data == other.Data;
        }

        public override bool Equals(object? obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((MonData)obj);
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(InstanceIdentifier, Name, Data);
        }
    }
}
