package me.zhengjie.config.rabbitmq.order;

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;

@Data
@Configuration
public class OrderProduce {
    @Autowired
    private RabbitmqConfig rabbitmqConfig;
    public static final String NAME_ORDER = "dish_trade_produce";
    public final static String MODULE_NAME_ORDER = "dish_t_p";

    public final static String CONNECTION_ORDER = MODULE_NAME_ORDER+RabbitmqConfig.CONNECTION;
    public final static String TEMPLATE_ORDER = MODULE_NAME_ORDER+ RabbitmqConfig.TEMPLATE;
    public final static String LISTENER_ORDER = MODULE_NAME_ORDER+RabbitmqConfig.LISTENER;
    public final static String ADMIN_ORDER = MODULE_NAME_ORDER+RabbitmqConfig.ADMIN;
    public final static String EXCHANGE_ORDER = MODULE_NAME_ORDER+RabbitmqConfig.EXCHANGE;
    public final static String QUEUE_ORDER = MODULE_NAME_ORDER+RabbitmqConfig.QUEUE;
    public final static String BINDING_ORDER = MODULE_NAME_ORDER+RabbitmqConfig.BINDING;

    @Bean(name = CONNECTION_ORDER)
    public ConnectionFactory connectionFactory(){
        return rabbitmqConfig.connectionFactory(NAME_ORDER);
    }

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

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

    @Bean(ADMIN_ORDER)
    AmqpAdmin amqpAdmin(@Qualifier(CONNECTION_ORDER) ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean(EXCHANGE_ORDER)
    public DirectExchange exchange(@Qualifier(ADMIN_ORDER) AmqpAdmin amqpAdmin) {
        return rabbitmqConfig.exchange(NAME_ORDER,amqpAdmin);
    }

    @Bean(QUEUE_ORDER)
    Queue queue(@Qualifier(ADMIN_ORDER)AmqpAdmin amqpAdmin) {
        return rabbitmqConfig.queue(NAME_ORDER,amqpAdmin);
    }

    @Bean(BINDING_ORDER)
    Binding bindingPersonDirect(@Qualifier(ADMIN_ORDER)AmqpAdmin amqpAdmin) {
        Binding binding = BindingBuilder.bind(queue(amqpAdmin)).to(exchange(amqpAdmin)).with(NAME_ORDER);
        binding.setAdminsThatShouldDeclare(amqpAdmin);
        return binding;
    }
}
