package cn.superdesk.uniorder.redis.model001.config;

import cn.superdesk.uniorder.redis.model001.channel.ProcessMessagesChannel003;
import cn.superdesk.uniorder.redis.model001.channel.ProcessMessagesChannelAccountingUnifiedCustomer;
import cn.superdesk.uniorder.redis.model001.channel.ProcessMessagesChannelAccountingUnifiedSupplier;
import cn.superdesk.uniorder.common.mq.redis.constants.RedisCommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;


@Slf4j
@AutoConfigureAfter({
		RedisConnectionFactory.class,
		ProcessMessagesChannelAccountingUnifiedCustomer.class,
		ProcessMessagesChannelAccountingUnifiedSupplier.class,
		ProcessMessagesChannel003.class
})
@Configuration
public class SubscriberConfig {


	/****************************************** 类topic channel start ******************************************/


	/**
	 * Redis订阅消息监听器
	 * container
	 * redisMessageListenerContainer
	 *
	 * @param redisConnectionFactory
	 * @param channelAccountingUnifiedCustomerAdapter
	 * @param channelAccountingUnifiedSupplierAdapter
	 * @return
	 */
	@Bean
	public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory,
	                                               MessageListenerAdapter channelAccountingUnifiedCustomerAdapter,
	                                               MessageListenerAdapter channelAccountingUnifiedSupplierAdapter,
	                                               MessageListenerAdapter channelMessageAdapter
	) {

		RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
		redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);

		log.info("RedisMessageListenerContainer INIT");

		// 监听频道1
		redisMessageListenerContainer.addMessageListener(channelAccountingUnifiedCustomerAdapter, new PatternTopic(RedisCommonConstants.CHANNEL_ACCOUNTING_UNIFIED_CUSTOMER));
		// 监听频道2
		redisMessageListenerContainer.addMessageListener(channelAccountingUnifiedSupplierAdapter, new PatternTopic(RedisCommonConstants.CHANNEL_ACCOUNTING_UNIFIED_SUPPLIER));

		redisMessageListenerContainer.addMessageListener(channelMessageAdapter, new PatternTopic(RedisCommonConstants.CHANNEL_QUEUE_F));

		return redisMessageListenerContainer;
	}

	/******************************************  start ******************************************/

	/**
	 * 委托对象 当我们监听的频道1 有新消息到来时,使用defaultListenerMethod来处理订阅的消息
	 * 此处springboot利用反射的技术,使用defaultListenerMethod处理消息
	 *
	 * @param processMessagesChannelAccountingUnifiedCustomer
	 * @return
	 */
	@Bean
	public MessageListenerAdapter channelAccountingUnifiedCustomerAdapter(ProcessMessagesChannelAccountingUnifiedCustomer processMessagesChannelAccountingUnifiedCustomer) {

		// 如果使用此方法,那么Receiver则  implements MessageListener
//		return new MessageListenerAdapter(processMessagesChannelAccountingUnifiedCustomer);

		// 如果使用此方法,那么Receiver则 不用implements MessageListener,可以自定义方法处理消息,下面的第二个参数为方法名
		return new MessageListenerAdapter(processMessagesChannelAccountingUnifiedCustomer, "monitorBroadcast");
	}

	/**
	 * 委托对象 当我们监听的频道2 有新消息到来时,使用 defaultListenerMethod 来处理订阅的消息
	 * 此处springboot利用反射的技术,使用 defaultListenerMethod 处理消息
	 *
	 * @param processMessagesChannelAccountingUnifiedSupplier
	 * @return
	 */
	@Bean
	public MessageListenerAdapter channelAccountingUnifiedSupplierAdapter(ProcessMessagesChannelAccountingUnifiedSupplier processMessagesChannelAccountingUnifiedSupplier) {

		log.info("ProcessMessagesChannelAccountingUnifiedSupplier : {}", processMessagesChannelAccountingUnifiedSupplier);
		return new MessageListenerAdapter(processMessagesChannelAccountingUnifiedSupplier, "monitorBroadcast");
	}

	@Bean
	public MessageListenerAdapter channelMessageAdapter(ProcessMessagesChannel003 processMessagesChannel003) {

		log.info("ProcessMessagesChannel003 : {}", processMessagesChannel003);
		return new MessageListenerAdapter(processMessagesChannel003);
	}

//	@Bean
//	public ProcessMessagesChannel003 processMessagesChannel003(){
//		return new ProcessMessagesChannel003();
//	}
}
