package jj.tech.paolu.config.wsclient;

import java.net.URI;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class SpringWebSocketClient extends TextWebSocketHandler{
	
	@Value("${websocket.url}")
    private String url;
	private WebSocketSession clientSession;
	
	public SpringWebSocketClient () throws Exception{
	        var client = new StandardWebSocketClient();
	        
	        this.clientSession = client.execute(new TTextWebSocketHandler(), "wss://echo.websocket.org", new Object())
	        		.get();
//	        this.clientSession =
//	        client.doHandshake( new TTextWebSocketHandler(), "wss://echo.websocket.org", new Object()).get();
	        
	        //this.clientSession = client.doHandshake(this, new TTextWebSocketHandler(), URI.create("wss://echo.websocket.org")).get();
	        
	}

//	@Bean
//    public WebSocketClient webSocketClient() throws Exception {
//        WebSocketClient client = new StandardWebSocketClient();
//        client.execute(new TTextWebSocketHandler(), url, null);
//        return client;
//		
//    }
	
	
	
	public static void main(String[] args) throws Exception {
		var sampleClient =  new SpringWebSocketClient();
		sampleClient.getClientSession().sendMessage(new TextMessage("Hello!"));
		sampleClient.getClientSession().sendMessage(new TextMessage("ssss"));
	}

	public WebSocketSession getClientSession() {
		return clientSession;
	}

	public void setClientSession(WebSocketSession clientSession) {
		this.clientSession = clientSession;
	}

}

class TTextWebSocketHandler extends TextWebSocketHandler{
	@Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) {
        System.err.println(message.getPayload());
    }
}
