package me.zhengjie.config.rabbitmq.cuisine;

import lombok.Data;
import me.zhengjie.config.rabbitmq.RabbitmqConfig;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
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;

@Data
@Configuration
public class CuisineConsumer {
    @Autowired
    private RabbitmqConfig rabbitmqConfig;
    public static final String NAME = "schedule_consume";//schedule_produce
    public final static String MODULE_NAME = "scheduleC";

    public final static String CONNECTION_CON = MODULE_NAME+RabbitmqConfig.CONNECTION;
    public final static String TEMPLATE_CON = MODULE_NAME+ RabbitmqConfig.TEMPLATE;
    public final static String LISTENER_CON = MODULE_NAME+RabbitmqConfig.LISTENER;
    public final static String ADMIN_CON = MODULE_NAME+RabbitmqConfig.ADMIN;
    public final static String EXCHANGE_CON = MODULE_NAME+RabbitmqConfig.EXCHANGE;
    public final static String QUEUE_CON = MODULE_NAME+RabbitmqConfig.QUEUE;
    public final static String BINDING_CON = MODULE_NAME+RabbitmqConfig.BINDING;

    @Bean(name = CONNECTION_CON)
    @Primary
    public ConnectionFactory connectionFactory(){
        return rabbitmqConfig.connectionFactory(NAME);
    }

    @Bean(name = TEMPLATE_CON)
    @Primary
    public RabbitTemplate rabbitTemplate(@Qualifier(CONNECTION_CON) ConnectionFactory connectionFactory ) {
        return rabbitmqConfig.rabbitTemplate(connectionFactory);
    }

    @Bean(name = LISTENER_CON)
    @Primary
    public SimpleRabbitListenerContainerFactory listenerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier(CONNECTION_CON) ConnectionFactory connectionFactory) {
        return rabbitmqConfig.listenerFactory(configurer,connectionFactory);
    }

    @Bean(name = ADMIN_CON)
    @Primary
    AmqpAdmin amqpAdmin(@Qualifier(CONNECTION_CON) ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean(EXCHANGE_CON)
    @Primary
    public DirectExchange exchange(@Qualifier(ADMIN_CON) AmqpAdmin amqpAdmin) {
        return rabbitmqConfig.exchange(NAME,amqpAdmin);
    }

    @Bean(QUEUE_CON)
    @Primary
    Queue queue(@Qualifier(ADMIN_CON)AmqpAdmin amqpAdmin) {
        return rabbitmqConfig.queue(NAME,amqpAdmin);
    }

    @Bean(BINDING_CON)
    @Primary
    Binding bindingPersonDirect(@Qualifier(ADMIN_CON)AmqpAdmin amqpAdmin) {
        Binding binding = BindingBuilder.bind(queue(amqpAdmin)).to(exchange(amqpAdmin)).with(NAME);
        binding.setAdminsThatShouldDeclare(amqpAdmin);
        return binding;
    }
}
