package com.prolog.cs.biz.outbound.config;

import com.prolog.cs.biz.outbound.service.impl.WaveServiceRbMQ;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class WaveHandleConfig {
    @Value("${mq.wave.response.queueName}")
    String responseQueue;

    @Value("${mq.wave.request.queueName}")
    String requestQueueName;

    @Value("${mq.wave.response.topicName}")
    String responseDebugTopic;

    @Bean("WaveServiceRbMQ")
    @ConditionalOnMissingBean
    public WaveServiceRbMQ createBizLogServiceImpl() {
        return new WaveServiceRbMQ();
    }

    @Bean("waveRequestQueue")
    @ConditionalOnBean(name = {"WaveServiceRbMQ"})
    public Queue createWaveRequestQueue() {
        return new Queue(requestQueueName);
    }


    @Bean("waveTopicExchange")
    @ConditionalOnBean(name = {"WaveServiceRbMQ"})
    public TopicExchange createWaveTopicExchange() {
        return new TopicExchange("waveTopicExchange");
    }


    @Bean
    @ConditionalOnBean(name = {"WaveServiceRbMQ"})
    Binding binding(@Qualifier("waveRequestQueue") Queue topicQueue,
                    @Qualifier("waveTopicExchange") TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue).to(topicExchange).with("topic.wave.request");
    }


}
