using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using C4IT.FASD.Cockpit.Communication;
#if isDemo
using FasdCockpitCommunicationDemo;
#endif

namespace FasdDesktopUi.Basics.Services.Models
{
    internal interface ITicketOverviewCommunication
    {
        bool IsDemo();

        Task<Dictionary<string, int>> GetTicketOverviewCounts(string[] overviewKeys, bool useRoleScope);

#if isDemo
        void RegisterGeneratedTicket(DemoTicketRecord record);
#endif
    }

    internal interface ITicketOverviewCommunicationSource
    {
        ITicketOverviewCommunication Resolve();
    }

    internal sealed class TicketOverviewCommunicationSource : ITicketOverviewCommunicationSource
    {
        public ITicketOverviewCommunication Resolve()
        {
            var communication = cFasdCockpitCommunicationBase.Instance;
            return communication == null ? null : new TicketOverviewCommunicationAdapter(communication);
        }
    }

    internal sealed class TicketOverviewCommunicationAdapter : ITicketOverviewCommunication
    {
        private readonly cFasdCockpitCommunicationBase _communication;

        internal TicketOverviewCommunicationAdapter(cFasdCockpitCommunicationBase communication)
        {
            _communication = communication ?? throw new ArgumentNullException(nameof(communication));
        }

        public bool IsDemo()
        {
            return _communication.IsDemo();
        }

        public async Task<Dictionary<string, int>> GetTicketOverviewCounts(string[] overviewKeys, bool useRoleScope)
        {
            var rawCounts = await _communication.GetTicketOverviewCounts(overviewKeys, useRoleScope).ConfigureAwait(false);
            return rawCounts == null
                ? new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
                : new Dictionary<string, int>(rawCounts, StringComparer.OrdinalIgnoreCase);
        }

#if isDemo
        public void RegisterGeneratedTicket(DemoTicketRecord record)
        {
            var demoCommunication = _communication as cFasdCockpitCommunicationDemo;
            demoCommunication?.RegisterGeneratedTicket(record);
        }
#endif
    }
}
