package com.sptg.repeater.rabbitmq.provider;


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.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * topic（主题）大豪信息 提供者
 */
@Configuration
public class TopicDHProvider {

    public static final String TRANSIENT_EXCHANGE_DH = "TRANSIENT_EXCHANGE_DH";
    public static final String PERSISTENT_EXCHANGE_DH = "PERSISTENT_EXCHANGE_DH";
//    public static final String TRANSIENT_WAIT_EXCHANGE_DH = "TRANSIENT_WAIT_EXCHANGE_DH";
    public static final String TRANSIENT_DH = "TRANSIENT_DH";
    public static final String PERSISTENT_DH = "PERSISTENT_DH";
//    public static final String TRANSIENT_WAIT_DH = "TRANSIENT_WAIT_DH";
    public static final String ROUTING_KEY = "dh.*";
//    public static final String DH_DEAD_LETTER_EXCHANGE_DH = "DH_DEAD_LETTER_EXCHANGE_DH";
//    public static final String DH_DEAD_LETTER_QUEUE_DH = "DH_DEAD_LETTER_QUEUE_DH";
//    public static final String WAIT_ROUTING_KEY = "dh_wait";
//    public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "dh_dead.*";

    /**
     * topic（主题）交换机 起名：TRANSIENT_EXCHANGE
     * @return
     */
    @Bean
    TopicExchange transientDHExchange() {
        return new TopicExchange(TRANSIENT_EXCHANGE_DH);
    }

    /**
     * 队列1名：TRANSIENT_DH
     * @return
     */
    @Bean
    public Queue transientDHQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-message-ttl", 1000 * 60 * 5); //5分钟有效期
        return QueueBuilder.durable(TRANSIENT_DH).withArguments(args).build();
//        return new Queue(TRANSIENT_DH, true);
    }

    /**
     * 绑定交换机，并设置匹配键：dh.*
     * @return
     */
    @Bean
    Binding bindingTransientDHExchange() {
        return BindingBuilder.bind(transientDHQueue()).to(transientDHExchange()).with(ROUTING_KEY);
    }

    /**
     * topic（主题）交换机 起名：PERSISTENT_EXCHANGE
     * @return
     */
    @Bean
    TopicExchange persistentDHExchange() {
        return new TopicExchange(PERSISTENT_EXCHANGE_DH);
    }

    // 队列2名：PERSISTENT_DH
    @Bean
    public Queue persistentDHQueue() {
        return new Queue(PERSISTENT_DH, true);
    }

    /**
     * 绑定交换机，并设置匹配键：dh.*
     * @return
     */
    @Bean
    Binding bindingPersistentDHExchange() {
        return BindingBuilder.bind(persistentDHQueue()).to(persistentDHExchange()).with(ROUTING_KEY);
    }

    /**
     * topic（主题）交换机 起名：PERSISTENT_EXCHANGE
     * @return
     */
//    @Bean
//    DirectExchange transientWaitDHExchange() {
//        return new DirectExchange(TRANSIENT_WAIT_EXCHANGE_DH);
//    }

    /**
     * 队列3名：transientWaitDHQueue，专门用于接收待转到死信队列的中转队列。
     * 正常的业务队列接收完一条消息后，发送一条特定的中转消息到中转队列，但该中转队列，不接收任何中转的消息
     * 只是让消息自动过期，过期后就会被抛到死信队列，再由死信队列的消费者进行其他处理。
     * @return
     */
//    @Bean
//    public Queue transientWaitDHQueue() {
//        Map<String, Object> args = new HashMap<>(3);
//        // x-message-ttl    这里声明消息过期时间是20秒
//        args.put("x-message-ttl", 1000*10);
//        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", DH_DEAD_LETTER_EXCHANGE_DH);
//        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
//        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_ROUTING_KEY);
//        return QueueBuilder.durable(TRANSIENT_WAIT_DH).withArguments(args).build();
//    }

    /**
     * 绑定交换机，并设置匹配键：dh_wait*
     * @return
     */
//    @Bean
//    Binding bindingTransientWaitDHExchange() {
//        return BindingBuilder.bind(transientWaitDHQueue()).to(transientWaitDHExchange()).with(WAIT_ROUTING_KEY);
//    }

    /**
     * topic死信交换机 起名：TRANSIENT_EXCHANGE
     * @return
     */
//    @Bean
//    public TopicExchange deadLetterDHExchange() {
//        return new TopicExchange(DH_DEAD_LETTER_EXCHANGE_DH);
//    }

    /**
     * 死信队列 DEAD_LETTER_QUEUE_DH
     * @return
     */
//    @Bean
//    public Queue deadLetterDHQueue() {
//        return new Queue(DH_DEAD_LETTER_QUEUE_DH, true);
//    }

    /**
     * 绑定死信交换机，并设置匹配键：dh_dead
     * @return
     */
//    @Bean
//    public Binding deadLetterBinding() {
//
//        return BindingBuilder.bind(deadLetterDHQueue()).to(deadLetterDHExchange()).with(DEAD_LETTER_QUEUE_ROUTING_KEY);
//    }
}
