package com.webchat.config.configuration;

import com.webchat.common.enums.RedisMessageChannelTopicEnum;
import com.webchat.service.listener.*;
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.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import javax.annotation.Resource;

@Configuration
public class RedisConfig {

    @Resource
    private RedisChatMessageListener chatMessageListener;
    @Resource
    private RedisPushMessageListener pushMessageListener;
    @Resource
    private RedisApplyMessageListener redisApplyMessageListener;
    @Resource
    private RedisVideoOfferSendMessageListener videoOfferSendMessageListener;
    @Resource
    private RedisVideoOfferReceiveMessageListener videoOfferReceiveMessageListener;
    @Resource
    private RedisGroupVideoOfferSendMessageListener groupVideoOfferSendMessageListener;
    @Resource
    private RedisGroupVideoOnlineMessageListener groupVideoOnlineMessageListener;
    @Resource
    private RedisGroupVideoOfflineMessageListener groupVideoOfflineMessageListener;

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        // 设置连接工厂
        container.setConnectionFactory(redisConnectionFactory);
        // 配置消息监听器
        container.addMessageListener(chatMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.CHAT.getChannel()));
        container.addMessageListener(pushMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.PUSH_ARTICLE.getChannel()));
        container.addMessageListener(redisApplyMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.APPLY.getChannel()));
        container.addMessageListener(videoOfferSendMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.VIDEO_OFFER_SEND.getChannel()));
        container.addMessageListener(videoOfferReceiveMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.VIDEO_OFFER_RECEIVER.getChannel()));
        container.addMessageListener(groupVideoOfferSendMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.GROUP_VIDEO_OFFER_SEND.getChannel()));
        container.addMessageListener(groupVideoOnlineMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.GROUP_VIDEO_ONLINE.getChannel()));
        container.addMessageListener(groupVideoOfflineMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.GROUP_VIDEO_OFFLINE.getChannel()));
        return container;
    }

}
