package com.gyee.wisdom.test;

import com.gyee.wisdom.common.data.timeseries.TsData;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandler;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class StompClient {

    private SockJsClient sockJsClient;

    private WebSocketStompClient stompClient;

    private StompSession stompSession;

    private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();

    public StompClient() {
        List<Transport> transports = new ArrayList<>();
        transports.add(new WebSocketTransport(new StandardWebSocketClient()));
        this.sockJsClient = new SockJsClient(transports);

        this.stompClient = new WebSocketStompClient(sockJsClient);
        this.stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    }

    public Map<String, TsData> getLatest(String keys) {
        return null;
    }

    public void connect(StompSessionHandler handler) {
        try {
            stompSession = stompClient.connect("ws://localhost:8011/wisdom", headers, handler).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public void sendRequest(StompSessionHandler handler) {
        if (stompSession != null) {
            StompHeaders headers = new StompHeaders();
            headers.setDestination("/app/latest");
            headers.add("keyStr", "MHSFJ.NX_GD_MHSF_FJ_P1_L1_001_AI0001,MHSFJ.NX_GD_MHSF_FJ_P1_L1_001_AI0005");
            stompSession.subscribe(headers, handler);
        }
    }

    public void sendSubscribe(StompSessionHandler handler) {
        if (stompSession != null) {
            StompHeaders headers = new StompHeaders();
            headers.setDestination("/app/subscribe");
            headers.add("topic", "/topic/test");
            stompSession.subscribe(headers, handler);

        }
    }

    public static void main2(String[] args) throws InterruptedException {

        System.out.println("11111");
        ClientStompSessionHandler handler = new ClientStompSessionHandler();
        System.out.println("22222");

        StompClient sc = new StompClient();
        sc.connect(handler);

        boolean isToConnect = true;
        while (isToConnect) {
            Thread.sleep(10);
            System.out.println("循环等待握手成功");
            if (handler.connect) {
                sc.sendSubscribe(handler);
                isToConnect = false;
            }
        }

        //sc.sendRequest(handler);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
