package com.muyu.cloud.vehicle.gateway.config;

import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Exchanger;

/**
 * rabbitmq配置类
 */
@Log4j2
@Configuration
public class RabbitmqConfig {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(RabbitmqConfig.class);

    /**
     * 交换机
     */
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    /**
     * 队列 车辆上线给事件系统发送vin
     */
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    /**
     * 队列  协议解析
     */
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    /**
     * 队列 车辆下线给事件系统发送vin
     */
    public static final String QUEUE_INFORM_SEND = "queue_inform_send";
    /**
     * 队列  saas系统
     */
    public static final String QUEUE_INFORM_SAAS = "queue_inform_saas";
    /**
     * 路由key  车辆上线给事件系统
     */
    public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
    /**
     * 路由key  协议解析
     */
    public static final String ROUTINGKEY_SMS = "inform.#.sms.#";
    /**
     * 路由key  车辆下线给事件系统
     */
    public static final String ROUTINGKEY_SEND = "inform.#.send.#";
    /**
     * 路由key  saas系统
     */
    public static final String ROUTINGKEY_SAAS = "inform.#.saas.#";


    /**
     * 声明交换机,做持久化
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange exchangeTopicsInform() {
        try {
            Exchange exchange = ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
            log.info("创建的交换机为: {}", EXCHANGE_TOPICS_INFORM);
            return exchange;
        } catch (Exception e) {
            log.error("创建该: {} 交换机失败", EXCHANGE_TOPICS_INFORM, e);
            throw e;
        }
    }

    /**
     * 声明QUEUE_INFORM_EMAIL 队列
     */
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue queueInformEmail() {
        try {
            Queue queue = new Queue(QUEUE_INFORM_EMAIL);
            log.info("创建的队列为: {}", QUEUE_INFORM_EMAIL);
            return queue;
        } catch (Exception e) {
            log.error("创建该: {} 队列失败", QUEUE_INFORM_EMAIL, e);
            throw e;
        }
    }

    /**
     * 声明QUEUE_INFORM_SMS 队列
     */
    @Bean(QUEUE_INFORM_SMS)
    public Queue queueInformSms() {
        try {
            Queue queue = new Queue(QUEUE_INFORM_SMS);
            log.info("创建的队列为: {}", QUEUE_INFORM_SMS);
            return queue;
        } catch (Exception e) {
            log.error("创建该: {} 队列失败", QUEUE_INFORM_SMS, e);
            throw e;
        }
    }

    /**
     * QUEUE_INFORM_SEND 队列
     */
    @Bean(QUEUE_INFORM_SEND)
    public Queue queueInformSend() {
        try {
            Queue queue = new Queue(QUEUE_INFORM_SEND);
            log.info("创建的队列为: {}", QUEUE_INFORM_SEND);
            return queue;
        } catch (Exception e) {
            log.error("创建该: {} 队列失败", QUEUE_INFORM_SEND, e);
            throw e;
        }
    }

    /**
     * QUEUE_INFORM_SAAS 队列
     */
    @Bean(QUEUE_INFORM_SAAS)
    public Queue queueInformSaas() {
        try {
            Queue queue = new Queue(QUEUE_INFORM_SAAS);
            log.info("创建的队列为: {}", QUEUE_INFORM_SAAS);
            return queue;
        } catch (Exception e) {
            log.error("创建该: {} 队列失败", QUEUE_INFORM_SAAS, e);
            throw e;
        }
    }


    /**
     * QUEUE_INFORM_EMAIL队列绑定交换机，指定routingKey ROUTINGKEY_EMAIL
     *
     * @param queue    QUEUE_INFORM_EMAIL
     * @param exchange EXCHANGE_TOPICS_INFORM
     */
    @Bean
    public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                           @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
    }

    /**
     * QUEUE_INFORM_SMS 队列绑定交换机，指定routingKey ROUTINGKEY_SMS
     *
     * @param queue    QUEUE_INFORM_SMS
     * @param exchange EXCHANGE_TOPICS_INFORM
     */
    @Bean
    public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                        @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
    }

    /**
     * QUEUE_INFORM_SEND队列绑定交换机，指定routingKey ROUTINGKEY_SEND
     *
     * @param queue    QUEUE_INFORM_SEND
     * @param exchange EXCHANGE_TOPICS_INFORM
     */
    @Bean
    public Binding bindingRoutingKeySend(@Qualifier(QUEUE_INFORM_SEND) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SEND).noargs();
    }

    /**
     * QUEUE_INFORM_SAAS队列绑定交换机，指定routingKey ROUTINGKEY_SAAS
     *
     * @param queue    QUEUE_INFORM_SAAS
     * @param exchange EXCHANGE_TOPICS_INFORM
     */
    @Bean
    public Binding bindingRoutingKeySaas(@Qualifier(QUEUE_INFORM_SAAS) Queue queue,
                                         @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SAAS).noargs();
    }
}
