package com.table.transfer.config;

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.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

// RabbitConfig.java
@Component
public class RabbitConfig {
    public static final String TRANSFER_EXCHANGE = "transfer";

    // 请求队列和路由键
    public static final String TRANSFER_REQUEST_QUEUE = "transfer.request";
    public static final String TRANSFER_ETH = "ETH";
    public static final String TRANSFER_BSC = "BSC";
    public static final String TRANSFER_TRON = "TRON";

    // 结果队列和路由键
    public static final String TRANSFER_RESULT_QUEUE = "transfer.result";
    public static final String TRANSFER_RESULT = "result";

    // 请求队列
    @Bean
    public Queue transferRequestQueue() {
        return QueueBuilder.durable(TRANSFER_REQUEST_QUEUE)
                .singleActiveConsumer()
                .build();
    }

    // 结果队列
    @Bean
    public Queue transferResultQueue() {
        return QueueBuilder.durable(TRANSFER_RESULT_QUEUE)
                .singleActiveConsumer()
                .build();
    }

    // 只需要一个交换机
    @Bean
    public DirectExchange transferExchange() {
        return new DirectExchange(TRANSFER_EXCHANGE);
    }

    // 请求队列的绑定（修正路由键）
    @Bean
    public Binding transferETHBinding() {
        return BindingBuilder.bind(transferRequestQueue())
                .to(transferExchange())
                .with(TRANSFER_ETH);
    }

    @Bean
    public Binding transferBSCBinding() {
        return BindingBuilder.bind(transferRequestQueue())
                .to(transferExchange())
                .with(TRANSFER_BSC);
    }

    @Bean
    public Binding transferTRONBinding() {
        return BindingBuilder.bind(transferRequestQueue())
                .to(transferExchange())
                .with(TRANSFER_TRON);
    }

    // 结果队列的绑定
    @Bean
    public Binding transferResultBinding() {
        return BindingBuilder.bind(transferResultQueue())
                .to(transferExchange())
                .with(TRANSFER_RESULT);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 配置RabbitTemplate使用JSON转换器
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    /**
     * 配置监听容器工厂使用JSON转换器
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter());

        // 可选：配置并发等参数
        factory.setConcurrentConsumers(1);
        factory.setMaxConcurrentConsumers(1);
        factory.setPrefetchCount(1); // 串行处理，每次只处理一个消息

        return factory;
    }

}