using e_suite.Messaging.Common.Exceptions;
using RabbitMQ.Client;

namespace e_suite.Messaging.Common;

/// <summary>
/// This is a wrapper around the RabbitMQ ConnectionFactory to allow the calls to be mocked giving access to unit testing.
/// </summary>
public class RabbitMqConnectionFactory : IRabbitMqConnectionFactory
{
    private readonly ConnectionFactory _connectionFactory;

    public RabbitMqConnectionFactory()
    {
        _connectionFactory = new ConnectionFactory();
    }

    public string HostName {
        get => _connectionFactory.HostName;
        set => _connectionFactory.HostName = value;
    }

    public string VirtualHost
    {
        get => _connectionFactory.VirtualHost;
        set => _connectionFactory.VirtualHost = value;
    }

    public string UserName
    {
        get => _connectionFactory.UserName;
        set => _connectionFactory.UserName = value;
    }

    public string Password
    {
        get => _connectionFactory.Password;
        set => _connectionFactory.Password = value;
    }
    public IConnection CreateConnection()
    {
        try
        {
            return _connectionFactory.CreateConnectionAsync().GetAwaiter().GetResult();
        }
        catch (Exception ex)
        {
            throw new FailedToConnectException($"Failed to connect to RabbitMq as HostName: {VirtualHost}, VirtualHost: {VirtualHost} Username: {UserName}", ex );
        }
    }
}