

using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace SonarLint.VisualStudio.Integration.Telemetry.Payload
{
    public sealed class ServerNotifications
    {
        [JsonProperty("disabled")]
        public bool IsDisabled { get; set; }

        [JsonProperty("count_by_type")]
        [XmlIgnore]
        public Dictionary<string, ServerNotificationCounter>  ServerNotificationCounters { get; set; } = new Dictionary<string, ServerNotificationCounter>();

        [JsonIgnore]
        [XmlArray(nameof(ServerNotificationCounters))]
        public SerializableKeyValuePair<string, ServerNotificationCounter>[] ServerNotificationCountersAsArray
        {
            get { return ServerNotificationCounters?.Select(p => p.ToSerializablePair()).ToArray(); }
            set { ServerNotificationCounters = value?.ToDictionary(p => p.Key, p => p.Value); }
        }
    }

    public sealed class ServerNotificationCounter
    {
        [JsonProperty("received")]
        public int ReceivedCount { get; set; }

        [JsonProperty("clicked")]
        public int ClickedCount { get; set; }
    }

    [XmlType("KeyValue"), XmlRoot("KeyValue")]
    public class SerializableKeyValuePair<TKey, TValue>
    {
        public TKey Key { get; set; }
        public TValue Value { get; set; }
    }

    public static class SerializableKeyValuePairExtensions
    {
        public static SerializableKeyValuePair<TKey, TValue> ToSerializablePair<TKey, TValue>(this KeyValuePair<TKey, TValue> pair)
        {
            return new SerializableKeyValuePair<TKey, TValue> { Key = pair.Key, Value = pair.Value };
        }
    }
}
