using System;
using System.Collections.Generic;

using Newtonsoft.Json;

namespace FasdCockpitCommunication.TicketOverview
{
    internal sealed class TicketOverviewCountsResponse
    {
        [JsonProperty("counts")]
        public Dictionary<string, int> Counts { get; set; } = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

        public Dictionary<string, int> ToDictionary(IEnumerable<string> expectedKeys)
        {
            var comparer = StringComparer.OrdinalIgnoreCase;
            var output = new Dictionary<string, int>(comparer);

            if (expectedKeys != null)
            {
                foreach (var key in expectedKeys)
                {
                    if (string.IsNullOrWhiteSpace(key))
                        continue;

                    if (Counts != null && Counts.TryGetValue(key, out var count))
                        output[key] = count;
                    else
                        output[key] = 0;
                }

                return output;
            }

            if (Counts != null)
            {
                foreach (var kvp in Counts)
                {
                    if (string.IsNullOrWhiteSpace(kvp.Key))
                        continue;

                    output[kvp.Key] = kvp.Value;
                }
            }

            return output;
        }
    }
}
