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 CuisineProduce {
    @Autowired
    private RabbitmqConfig rabbitmqConfig;
    public static final String NAME = "schedule_produce";
    private final static String MODULE_NAME = "scheduleP";

    public final static String CONNECTION = MODULE_NAME+RabbitmqConfig.CONNECTION;
    public final static String TEMPLATE = MODULE_NAME+ RabbitmqConfig.TEMPLATE;
    public final static String LISTENER = MODULE_NAME+RabbitmqConfig.LISTENER;
    public final static String ADMIN = MODULE_NAME+RabbitmqConfig.ADMIN;
    public final static String EXCHANGE = MODULE_NAME+RabbitmqConfig.EXCHANGE;
    public final static String QUEUE = MODULE_NAME+RabbitmqConfig.QUEUE;
    public final static String BINDING = MODULE_NAME+RabbitmqConfig.BINDING;

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

    @Bean(name = TEMPLATE)
    public RabbitTemplate rabbitTemplate(@Qualifier(CONNECTION) ConnectionFactory connectionFactory ) {
        return rabbitmqConfig.rabbitTemplate(connectionFactory);
    }

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

    @Bean(name = ADMIN)
    AmqpAdmin amqpAdmin(@Qualifier(CONNECTION) ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        rabbitAdmin.declareExchange(exchange(rabbitAdmin));
        rabbitAdmin.declareQueue(queue(rabbitAdmin));
        return rabbitAdmin;
    }

    @Bean(EXCHANGE)
    public DirectExchange exchange(@Qualifier(ADMIN) AmqpAdmin amqpAdmin) {
        return rabbitmqConfig.exchange(NAME,amqpAdmin);
    }

    @Bean(QUEUE)
    Queue queue(@Qualifier(ADMIN)AmqpAdmin amqpAdmin) {
        return rabbitmqConfig.queue(NAME,amqpAdmin);
    }

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