package com.nbusy.sdk.titan.neptulon;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocket;
import okhttp3.ws.WebSocketCall;
import okhttp3.ws.WebSocketListener;
import okio.Buffer;

/**
 * Neptulon connection implementation: https://github.com/neptulon/neptulon
 */
public class ConnImpl implements Conn {
    private static final Logger logger = Logger.getLogger(ConnImpl.class.getSimpleName());
    private final OkHttpClient client;
    private final Request request;
    private final WebSocketCall wsCall;
    private WebSocket ws;

    public ConnImpl() {
        client = new OkHttpClient.Builder()
                .connectTimeout(45, TimeUnit.SECONDS)
                .writeTimeout(300, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .build();

        request = new Request.Builder()
                .url("ws://10.0.2.2:3010")
                .build();

        wsCall = WebSocketCall.create(client, request);
    }

    public void useTLS(byte[] ca, byte[] clientCert, byte[] clientCertKey) {
        // todo: https://github.com/square/okhttp/wiki/HTTPS
    }

    public void connect() {
        // enqueue a listener to initiate the WebSocket connection
        wsCall.enqueue(new WebSocketListener() {
            @Override
            public void onOpen(WebSocket webSocket, Response response) {
                ws = webSocket;
                logger.info("WebSocket connected.");
            }

            @Override
            public void onFailure(IOException e, Response response) {
                logger.warning("WebSocket connection closed with error: " + e.getMessage());
            }

            @Override
            public void onMessage(ResponseBody message) throws IOException {
//                private final Gson gson = new Gson();
//                Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
//                for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
//                    System.out.println(entry.getKey());
//                    System.out.println(entry.getValue().content);
//                }
                throw new RuntimeException("got message!" + message.string());
//                message.close(); needed?
            }

            @Override
            public void onPong(Buffer payload) {
            }

            @Override
            public void onClose(int code, String reason) {
                logger.info("WebSocket closed.");
            }
        });
    }

    public void send() throws IOException {
        ws.sendMessage(RequestBody.create(WebSocket.TEXT, "{\"ID\": \"123\", \"Method\": \"test2\"}"));
    }

    public void close() throws IOException {
        wsCall.cancel();
        ws.close(0, "");
    }
}

@@ -508,16 +508,35 @@
 nts Conn
+, WebSocketListener
  %7B%0A    p
@@ -1381,17 +1381,20 @@
 enqueue 
-a
+this
  listene
@@ -1395,16 +1395,31 @@
 istener 
+implementation 
 to initi
@@ -1474,42 +1474,279 @@
 eue(
-new WebSocketListener() %7B%0A        
+this);%0A    %7D%0A%0A    public void send() throws IOException %7B%0A        ws.sendMessage(RequestBody.create(WebSocket.TEXT, %22%7B%5C%22ID%5C%22: %5C%22123%5C%22, %5C%22Method%5C%22: %5C%22test2%5C%22%7D%22));%0A    %7D%0A%0A    public void close() throws IOException %7B%0A        wsCall.cancel();%0A        ws.close(0, %22%22);%0A    %7D%0A%0A
     
@@ -1751,32 +1751,24 @@
   @Override%0A
-        
     public v
@@ -1816,32 +1816,24 @@
 response) %7B%0A
-        
         ws =
@@ -1844,24 +1844,16 @@
 Socket;%0A
-        
         
@@ -1893,35 +1893,19 @@
 %22);%0A    
-        %7D%0A%0A        
+%7D%0A%0A
     @Ove
@@ -1906,32 +1906,24 @@
   @Override%0A
-        
     public v
@@ -1968,32 +1968,24 @@
 response) %7B%0A
-        
         logg
@@ -2057,39 +2057,23 @@
 age());%0A
-        
     %7D%0A%0A
-        
     @Ove
@@ -2074,32 +2074,24 @@
   @Override%0A
-        
     public v
@@ -2520,24 +2520,16 @@
       %7D%0A
-        
         
@@ -2638,35 +2638,19 @@
 ed?%0A    
-        %7D%0A%0A        
+%7D%0A%0A
     @Ove
@@ -2651,32 +2651,24 @@
   @Override%0A
-        
     public v
@@ -2704,27 +2704,11 @@
     
-        %7D%0A%0A        
+%7D%0A%0A
     
@@ -2717,24 +2717,16 @@
 verride%0A
-        
     publ
@@ -2768,24 +2768,16 @@
 ason) %7B%0A
-        
         
@@ -2814,291 +2814,8 @@
 %22);%0A
-            %7D%0A        %7D);%0A    %7D%0A%0A    public void send() throws IOException %7B%0A        ws.sendMessage(RequestBody.create(WebSocket.TEXT, %22%7B%5C%22ID%5C%22: %5C%22123%5C%22, %5C%22Method%5C%22: %5C%22test2%5C%22%7D%22));%0A    %7D%0A%0A    public void close() throws IOException %7B%0A        wsCall.cancel();%0A        ws.close(0, %22%22);%0A
     

simplify WebSocketListener via reusing ConnImpl class