package com.its.pis.websocket;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Slf4j
@RequiredArgsConstructor
@Configuration
@EnableWebSocket
//@Order(2)
public class PisWebSocketConfig implements WebSocketConfigurer {

    public static final String WS_C2F_ENDPOINT = "/ws/pis-c2f.do";
    public static final String WS_C2C_ENDPOINT = "/ws/pis-c2c.do";

    private final PisWebSocketHandlerC2F pisWebsocketHandlerC2F;
    private final PisWebSocketHandlerC2C pisWebsocketHandlerC2C;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

        registry.addHandler(this.pisWebsocketHandlerC2F, PisWebSocketConfig.WS_C2F_ENDPOINT)
                //.setAllowedOrigins("*")
                .setAllowedOriginPatterns("*")
                //.addInterceptors(new ItsWebSocketHandshakeInterceptor()).setAllowedOriginPatterns("*")
                .withSockJS()
        //.setSessionCookieNeeded(false)
        ;  // sockjs

        registry.addHandler(this.pisWebsocketHandlerC2F, PisWebSocketConfig.WS_C2F_ENDPOINT)
                .setAllowedOriginPatterns("*")
        //.addInterceptors(new ItsWebSocketHandshakeInterceptor()).setAllowedOriginPatterns("*")
        //.setAllowedOrigins("*")
        ; // 그냥 websocket 지원

        registry.addHandler(this.pisWebsocketHandlerC2C, PisWebSocketConfig.WS_C2C_ENDPOINT)
                //.setAllowedOrigins("*")
                .setAllowedOriginPatterns("*")
                //.addInterceptors(new ItsWebSocketHandshakeInterceptor()).setAllowedOriginPatterns("*")
                .withSockJS()
        //.setSessionCookieNeeded(false)
        ;  // sockjs

        registry.addHandler(this.pisWebsocketHandlerC2C, PisWebSocketConfig.WS_C2C_ENDPOINT)
                .setAllowedOriginPatterns("*")
        //.addInterceptors(new ItsWebSocketHandshakeInterceptor()).setAllowedOriginPatterns("*")
        //.setAllowedOrigins("*")
        ; // 그냥 websocket 지원

        log.info("PisWebSocketConfig.registerWebSocketHandlers: ..End.");
    }

}
