package chat.rocket.android.helper;

import android.content.Context;
import android.support.annotation.NonNull;
import com.facebook.stetho.okhttp3.StethoInterceptor;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.rest.CookieInterceptor;
import chat.rocket.android.api.rest.DefaultCookieProvider;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Helper class for OkHttp client.
 */
public class OkHttpHelper {
  private static OkHttpClient defaultHttpClient;
  private static OkHttpClient httpClientForUploadFile;
  private static OkHttpClient httpClientForDownloadFile;
  private static OkHttpClient httpClientForWS;
  private static String contentType;

  public static OkHttpClient getDefaultHttpClient() {
    if (defaultHttpClient == null) {
      defaultHttpClient = new OkHttpClient();
    }
    return defaultHttpClient;
  }

  public static OkHttpClient getClientForDownloadFile(Context context) {
    if (httpClientForDownloadFile == null) {
      httpClientForDownloadFile = new OkHttpClient.Builder()
          .addNetworkInterceptor(new StethoInterceptor())
          .addInterceptor(
              new CookieInterceptor(new DefaultCookieProvider(new RocketChatCache(context))))
          .build();
    }
    return httpClientForDownloadFile;
  }

  public static OkHttpClient getClientForUploadFile() {
    if (httpClientForUploadFile == null) {
      httpClientForUploadFile = new OkHttpClient.Builder()
          .addNetworkInterceptor(new StethoInterceptor())
          .build();
    }
    return httpClientForUploadFile;
  }

  /**
   * acquire OkHttpClient instance for WebSocket connection.
   */
  public static OkHttpClient getClientForWebSocket() {
    if (httpClientForWS == null) {
      httpClientForWS = new OkHttpClient.Builder().readTimeout(0, TimeUnit.NANOSECONDS)
          .addNetworkInterceptor(new StethoInterceptor())
          .build();
    }
    return httpClientForWS;
  }

  public static String getContentType(String uri) {
    Request request = new Request.Builder()
        .url(uri)
        .head()
        .build();
    getDefaultHttpClient().newCall(request).enqueue(new Callback() {
      @Override
      public void onFailure(@NonNull Call call, @NonNull IOException ioException) {
        ioException.printStackTrace();
      }

      @Override
      public void onResponse(@NonNull Call call, @NonNull final Response response)
          throws IOException {
        if (!response.isSuccessful()) {
          throw new IOException("Unexpected code: " + response);
        } else {
          contentType = response.header("Content-Type");
        }
      }
    });
    return contentType;
  }
}

@@ -66,51 +66,8 @@
 xt;%0A
-import android.support.annotation.NonNull;%0A
 impo
@@ -121,36 +121,8 @@
 r;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -332,115 +332,20 @@
 tp3.
-Call;%0Aimport okhttp3.Callback;%0Aimport okhttp3.OkHttpClient;%0Aimport okhttp3.Request;%0Aimport okhttp3.Response
+OkHttpClient
 ;%0A%0A/
@@ -418,57 +418,8 @@
 r %7B%0A
-  private static OkHttpClient defaultHttpClient;%0A
   pr
@@ -576,223 +576,8 @@
 rWS;
-%0A  private static String contentType;%0A%0A  public static OkHttpClient getDefaultHttpClient() %7B%0A    if (defaultHttpClient == null) %7B%0A      defaultHttpClient = new OkHttpClient();%0A    %7D%0A    return defaultHttpClient;%0A  %7D
 %0A%0A  
@@ -1655,735 +1655,5 @@
   %7D%0A
-%0A  public static String getContentType(String uri) %7B%0A    Request request = new Request.Builder()%0A        .url(uri)%0A        .head()%0A        .build();%0A    getDefaultHttpClient().newCall(request).enqueue(new Callback() %7B%0A      @Override%0A      public void onFailure(@NonNull Call call, @NonNull IOException ioException) %7B%0A        ioException.printStackTrace();%0A      %7D%0A%0A      @Override%0A      public void onResponse(@NonNull Call call, @NonNull final Response response)%0A          throws IOException %7B%0A        if (!response.isSuccessful()) %7B%0A          throw new IOException(%22Unexpected code: %22 + response);%0A        %7D else %7B%0A          contentType = response.header(%22Content-Type%22);%0A        %7D%0A      %7D%0A    %7D);%0A    return contentType;%0A  %7D%0A%7D%0A
+%7D

Delete unused methods.