package com.pj.rabbitmq;

import lombok.Data;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;


/**
 * @Author Mechrevo
 * @Date 2023 08 25 12 21
 **/
@Configuration
@Data
public class RabbitmqConfig {

    @Resource
    private RabbitMQProperties rabbitMQProperties;

    @Bean(name = "levelOneConnectionFactory")
    @Primary
    public CachingConnectionFactory levelOneConnectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setHost(rabbitMQProperties.getHost());
        factory.setPort(rabbitMQProperties.getPort());
        factory.setUsername(rabbitMQProperties.getUsername());
        factory.setPassword(rabbitMQProperties.getPassword());
        factory.setVirtualHost(rabbitMQProperties.getLevelOneVirtualHost());
        return factory;
    }
    @Bean(name = "levelOneRabbitListenerContainer")
    public SimpleRabbitListenerContainerFactory levelOneRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                                @Qualifier("levelOneConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean(name = "levelTwoConnectionFactory")
    public CachingConnectionFactory levelTwoConnectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setHost(rabbitMQProperties.getHost());
        factory.setPort(rabbitMQProperties.getPort());
        factory.setUsername(rabbitMQProperties.getLevelTwoUsername());
        factory.setPassword(rabbitMQProperties.getLevelTwoPassword());
        factory.setVirtualHost(rabbitMQProperties.getLevelTwoVirtualHost());
        return factory;
    }
    @Bean(name = "levelTwoRabbitListenerContainer")
    public SimpleRabbitListenerContainerFactory levelTwoRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                                @Qualifier("levelTwoConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean(name = "customsConnectionFactory")
    public CachingConnectionFactory customsConnectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setHost(rabbitMQProperties.getHost());
        factory.setPort(rabbitMQProperties.getPort());
        factory.setUsername(rabbitMQProperties.getCustomsUsername());
        factory.setPassword(rabbitMQProperties.getCustomsPassword());
        factory.setVirtualHost(rabbitMQProperties.getCustomsVirtualHost());
        return factory;
    }
    @Bean(name = "customsRabbitListenerContainer")
    public SimpleRabbitListenerContainerFactory customsRabbitListenerContainer(SimpleRabbitListenerContainerFactoryConfigurer config,
                                                                               @Qualifier("customsConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        config.configure(factory, connectionFactory);
        return factory;
    }

}
