package ga.piroro.rnt;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
import se.akerfeldt.okhttp.signpost.SigningInterceptor;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class TwitterStream {
    private final Call call;

    public TwitterStream(final ReadableMap tokens, final String url, final NativeEmitter emitter) {
        OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(
                tokens.getString("consumerKey"),
                tokens.getString("consumerSecret")
        );
        consumer.setTokenWithSecret(
                tokens.getString("accessToken"),
                tokens.getString("accessTokenSecret")
        );
        Request request = new Request.Builder()
            .url(url)
            .get()
            .build();
        call = new OkHttpClient.Builder()
            .addInterceptor(new SigningInterceptor(consumer))
            .readTimeout(0, TimeUnit.SECONDS)
            .build()
            .newCall(request);
        call.enqueue(new Callback() {
            public void onResponse(Call call, Response response) throws IOException {
                try (ResponseBody responseBody = response.body()) {
                    if (!response.isSuccessful()) {
                        emitter.emit("twitterError", responseBody.string());
                        return;
                    }
                    BufferedReader reader = new BufferedReader(responseBody.charStream());
                    StringBuilder builder = new StringBuilder();
                    int c;
                    boolean rFlag = false;
                    while ((c = reader.read()) != -1) {
                        if (rFlag && c == '\n') {
                            String data = builder.deleteCharAt(builder.length() - 1).toString(); // Remove \r.
                            if (data != "") {
                                emitter.emit("data", data);
                            }
                            builder.setLength(0);
                        } else {
                            builder.append((char)c);
                        }
                        rFlag = c == '\r';
                    }
                }
            }

            @Override
            public void onFailure(Call call, IOException e) {
                emitter.emit("networkError", e.getMessage());
            }
        });
    }

    public void close() {
        call.cancel();
    }
}

@@ -779,36 +779,32 @@
 er(%0A            
-    
 tokens.getString
@@ -816,28 +816,24 @@
 sumerKey%22),%0A
-    
             
@@ -919,36 +919,32 @@
 et(%0A            
-    
 tokens.getString
@@ -956,28 +956,24 @@
 essToken%22),%0A
-    
             
@@ -1935,16 +1935,43 @@
  false;%0A
+                    int l;%0A
         
@@ -2100,133 +2100,38 @@
     
-String data = builder.deleteCharAt(builder.length() - 1).toString(); // Remove %5Cr.%0A                            if (data != %22%22
+if ((l = builder.length()) %3E 1
 ) %7B%0A
@@ -2187,14 +2187,62 @@
 a%22, 
-data);
+builder.deleteCharAt(l - 1).toString()); // Remove %5Cr.
 %0A   

Fix #17