package com.miguelgaeta.bootstrap.mg_websocket;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * Created by Miguel Gaeta on 5/19/15.
 *
 * http://bit.ly/1R1lhD7
 *
 * Has some bugs for fetching ready state.  Has
 * a hack to fix it.
 */
@SuppressWarnings("UnusedDeclaration")
public abstract class MGWebsocketClientBase extends WebSocketClient {

    // Fix bug in the client.
    private READYSTATE readystate = null;

    public MGWebsocketClientBase(URI serverURI) {
        super(serverURI);
    }

    public MGWebsocketClientBase(URI serverUri, Draft draft) {
        super(serverUri, draft);
    }

    public MGWebsocketClientBase(URI serverUri, Draft protocolDraft, Map<String, String> httpHeaders, int connectTimeout) {
        super(serverUri, protocolDraft, httpHeaders, connectTimeout);
    }

    public MGWebsocketClientBase(String serverURL) {
        super(createURI(serverURL));
    }

    public MGWebsocketClientBase(String serverURL, Draft draft) {
        super(createURI(serverURL), draft);
    }

    public MGWebsocketClientBase(String serverURL, Draft protocolDraft, Map<String, String> httpHeaders, int connectTimeout) {
        super(createURI(serverURL), protocolDraft, httpHeaders, connectTimeout);
    }

    @Override
    public READYSTATE getReadyState() {

        if (readystate != null) {

            return readystate;
        }

        return super.getReadyState();
    }

    @Override
    public final void onOpen(ServerHandshake serverHandshake) {

        readystate = READYSTATE.OPEN;

        onDidOpen(serverHandshake);
    }

    @Override
    public final void onClose(int code, String reason, boolean remote) {

        readystate = READYSTATE.CLOSED;

        onDidClose(code, reason, remote);
    }

    @Override
    public final void onError(Exception exception) {

        readystate = READYSTATE.CLOSED;

        onDidError(exception);
    }

    @Override
    public final void onMessage(String message) {

        onDidMessage(message);
    }

    public abstract void onDidOpen(ServerHandshake serverHandshake);

    public abstract void onDidClose(int code, String reason, boolean remote);

    public abstract void onDidError(Exception exception);

    public abstract void onDidMessage(String message);

    /**
     * Create a URI object from a
     * provided string.  Throws a runtime
     * exception if the url is invalid.
     */
    private static URI createURI(@android.support.annotation.NonNull String URL) {

        try {
            return new URI(URL);

        } catch (URISyntaxException ignored) {

            throw new RuntimeException("Unable to create URI from provided URL: " + URL);
        }
    }
}

@@ -42,16 +42,64 @@
 ocket;%0A%0A
+import com.miguelgaeta.bootstrap.mg_log.MGLog;%0A%0A
 import o
@@ -250,73 +250,246 @@
 ava.
-net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.util.Map
+io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.nio.ByteBuffer;%0Aimport java.util.Map;%0Aimport java.util.zip.InflaterInputStream
 ;%0A%0A/
@@ -2288,32 +2288,289 @@
 eption);%0A    %7D%0A%0A
+    @Override%0A    public void onMessage(ByteBuffer byteBuffer) %7B%0A%0A        try %7B%0A%0A            onMessage(decompressZlib(byteBuffer.array()));%0A%0A        %7D catch (IOException e) %7B%0A%0A            MGLog.e(e, %22Unable to decompress websocket data.%22);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -3337,11 +3337,535 @@
  %7D%0A    %7D
+%0A%0A    private static String decompressZlib(byte%5B%5D compressedData) throws IOException %7B%0A%0A        final byte%5B%5D buffer = new byte%5BcompressedData.length%5D;%0A%0A        final ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        final InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(compressedData));%0A%0A        for (int bytesRead = 0; bytesRead != -1; bytesRead = in.read(buffer)) %7B%0A%0A            out.write(buffer,0,bytesRead);%0A        %7D%0A%0A        return new String(out.toByteArray(),%22UTF-8%22);%0A    %7D
 %0A%7D%0A

Decompress and send off byte messages from the web socket.