using e_suite.API.Common;
using e_suite.Messaging.Common.models;
using eSuite.Core.Clock;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client.Events;

namespace e_suite.MessageProcessor.Handlers;

public class EFlowImportQueueHandler : QueueHandlerBase
{
    private readonly ILogger _logger;
    private readonly IClock _clock;
    private readonly IEFlowSync _eFlowSync;

    public EFlowImportQueueHandler(IClock clock, ILogger logger, IEFlowSync eFlowSync)
    {
        _clock = clock;
        _logger = logger;
        _eFlowSync = eFlowSync;
    }

    public override async Task OnReceived(string queueName, BasicDeliverEventArgs basicDeliverEventArgs)
    {
        var message = await TranslateMessage<EFlowSyncMessage>(basicDeliverEventArgs);
        _logger.LogInformation("{DateTime}: {MessageType} message received.", _clock.GetNow, message.MessageType);
        switch (message.MessageType)
        {
            case EFlowSyncMessageTypes.PrinterCategorySync:
                var fullMessage = await TranslateMessage<EFlowPrinterCategorySyncMessage>(basicDeliverEventArgs);
                await _eFlowSync.SyncEFlowPrinterCategories(fullMessage.Domains);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(basicDeliverEventArgs), message.MessageType, $"Unexpected message type : {message.MessageType}");
        }

        _logger.LogInformation("{DateTime}: {MessageType} message processing finished.", _clock.GetNow, message.MessageType);
    }
}