using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Configuration;
using fatd_faturamento.BLL.Repositorio;
using fatd_faturamento.ENTIDADE;

namespace fatd_faturamento.BLL.RegraNegocio
{
    public class regraNegocioRabbitMQ
    {

        public void Send(string message, string nameFile)
        {

            var factory = new ConnectionFactory() { HostName = ConfigurationManager.AppSettings["server_rabbitmq"], UserName = ConfigurationManager.AppSettings["u_rabbitmq"], Password = ConfigurationManager.AppSettings["p_rabbitmq"] };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: nameFile,
                                     durable: true,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                var body = Encoding.UTF8.GetBytes(message);

                var properties = channel.CreateBasicProperties();
                properties.Persistent = true;

                channel.BasicPublish(exchange: "",
                                     routingKey: nameFile,
                                     basicProperties: properties,
                                     body: body);

            }

        }

        public void Receive(string nameFile)
        {
            entidadeRabbitMQ objRabbitMQ = new entidadeRabbitMQ();
            repositorioRabbitMQ repositorioRabbitMQ = new repositorioRabbitMQ();

            var factory = new ConnectionFactory() { HostName = ConfigurationManager.AppSettings["server_rabbitmq"], UserName = ConfigurationManager.AppSettings["u_rabbitmq"], Password = ConfigurationManager.AppSettings["p_rabbitmq"] };

            var connection = factory.CreateConnection();
            var channel = connection.CreateModel();

            channel.QueueDeclare(queue: nameFile,
                                     durable: true,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                try
                {

                    var body = ea.Body.ToArray();
                    var message = Encoding.UTF8.GetString(body);

                    objRabbitMQ.JSON = message;
                    objRabbitMQ.NOME_FILA = nameFile;
                    objRabbitMQ.FILA_LIDA = false;

                    repositorioRabbitMQ.Insert(objRabbitMQ);

                    channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);

                }
                catch (Exception ex)
                {
                    channel.BasicNack(ea.DeliveryTag, false, false);
                }


            };

            channel.BasicConsume(queue: nameFile,
                     autoAck: false,
                     consumer: consumer);

        }
    }
}
