using e_suite.API.Common;
using e_suite.Messaging.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 SigmaImportQueueHandler : QueueHandlerBase
{
    private readonly ISigmaImporter _sigmaImporter;
    private readonly ILogger _logger;
    private readonly IClock _clock;
    private readonly IEFlowSyncMessageSender _eFlowSyncMessageSender;

    public SigmaImportQueueHandler(IClock clock, ILogger logger, ISigmaImporter sigmaImporter, IEFlowSyncMessageSender eFlowSyncMessageSender)
    {
        _clock = clock;
        _logger = logger;
        _sigmaImporter = sigmaImporter;
        _eFlowSyncMessageSender = eFlowSyncMessageSender;
    }

    public override async Task OnReceived(string queueName, BasicDeliverEventArgs basicDeliverEventArgs)
    {
        var message = await TranslateMessage<SigmaImportMessage>(basicDeliverEventArgs);
        _logger.LogInformation("{DateTime}: {MessageType} message received.", _clock.GetNow, message.MessageType);
        switch (message.MessageType)
        {
            case SigmaImportMessageTypes.ImportGMGProfiles:
                await _sigmaImporter.ImportGMGProfiles();
                break;
            case SigmaImportMessageTypes.ImportPrintSpecifications:
                await _sigmaImporter.ImportPrintSpecifications();
                _eFlowSyncMessageSender.SyncEFlowPrinterCategories();
                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);
    }
}