package com.bonus.common.houqin.mq;

import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

//@AutoConfiguration
@Configuration
@EnableConfigurationProperties({MQProperties.class})
@Import({PropertyConfiguration.class})
public class MQConfiguration {
   public MQConfiguration(RocketMQProperties rocketMQProperties, MQProperties mqProperties) {
      rocketMQProperties.setNameServer(mqProperties.getRocketmq().getNameServer());
      rocketMQProperties.setAccessChannel(mqProperties.getRocketmq().getAccessChannel());
      rocketMQProperties.setConsumer(mqProperties.getRocketmq().getConsumer());
      RocketMQProperties.PushConsumer consumer = rocketMQProperties.getConsumer();
      if (consumer.getAccessKey() == null) {
         consumer.setAccessKey(mqProperties.getRocketmq().getAccessKey());
      }

      if (consumer.getSecretKey() == null) {
         consumer.setAccessKey(mqProperties.getRocketmq().getSecretKey());
      }

      rocketMQProperties.setProducer(mqProperties.getRocketmq().getProducer());
      RocketMQProperties.Producer producer = rocketMQProperties.getProducer();
      if (producer != null) {
         if (producer.getAccessKey() == null) {
            producer.setAccessKey(mqProperties.getRocketmq().getAccessKey());
         }

         if (producer.getSecretKey() == null) {
            producer.setSecretKey(mqProperties.getRocketmq().getSecretKey());
         }
      }

      rocketMQProperties.setPullConsumer(mqProperties.getRocketmq().getPullConsumer());
      RocketMQProperties.PullConsumer pullConsumer = rocketMQProperties.getPullConsumer();
      if (pullConsumer.getAccessKey() == null) {
         pullConsumer.setAccessKey(mqProperties.getRocketmq().getAccessKey());
      }

      if (pullConsumer.getSecretKey() == null) {
         pullConsumer.setSecretKey(mqProperties.getRocketmq().getSecretKey());
      }

   }

   @Bean
   @ConditionalOnMissingBean({MQMessageConverter.class})
   public MQMessageConverter createRocketMQMessageConverter() {
      return new MQMessageConverter();
   }

   @Bean
   @ConditionalOnProperty(
      prefix = "spring.mq",
      value = {"type"},
      havingValue = "rocketmq"
   )
   public RabbitTemplate rabbitTemplate() {
      return new RabbitTemplate(new CachingConnectionFactory()) {
         public <T> T execute(ChannelCallback<T> action) {
            return (T) "";
         }
      };
   }
}
