package com.centreproject.framework.config;

import com.centreproject.common.constant.KeyConstants;
import com.centreproject.common.websocket.BusinessSocketHandler;
import com.centreproject.common.websocket.MyWebSocketHandler;
import org.apache.tomcat.util.threads.VirtualThreadExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author zhonglei.xu
 * @date 2021/3/24 14:50
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final RedisTemplate<String, String> redisTemplate;

    public WebSocketConfig(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myWebSocketHandler(), "/kpi/ws/data")
                .setAllowedOrigins("*");
        registry.addHandler(myWebSocketHandler(), "/kpi/ws/{businessType}/{parkCode}")
                .setAllowedOrigins("*");
    }

    @Bean
    public MyWebSocketHandler myWebSocketHandler() {
        return new MyWebSocketHandler(redisTemplate);
    }

    @Bean
    public BusinessSocketHandler businessSocketHandler() {return new BusinessSocketHandler();}
}