package com.filestack.util;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.concurrent.TimeUnit;

/**
 * Serves as singleton holder for the OkHttp client and Retrofit services.
 * We only want to instantiate these classes once per app.
 */
public class Networking {
    private static OkHttpClient httpClient;
    private static FilestackService.Cdn cdnService;
    private static FilestackService.Api apiService;
    private static FilestackService.Process processService;
    private static FilestackService.Upload uploadService;

    public static OkHttpClient getHttpClient() {
        if (httpClient == null) {
            httpClient = new OkHttpClient.Builder()
                    .addInterceptor(new HeaderInterceptor())
                    .readTimeout(30, TimeUnit.SECONDS)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .retryOnConnectionFailure(false)
                    .build();
        }
        return httpClient;
    }

    public static FilestackService.Cdn getCdnService() {
        if (cdnService == null) {
            Retrofit retrofit = getRetrofitBuilder().baseUrl(FilestackService.Cdn.URL).build();
            cdnService = retrofit.create(FilestackService.Cdn.class);
        }
        return cdnService;
    }

    public static FilestackService.Api getApiService() {
        if (apiService == null) {
            Retrofit retrofit = getRetrofitBuilder().baseUrl(FilestackService.Api.URL).build();
            apiService = retrofit.create(FilestackService.Api.class);
        }
        return apiService;
    }

    public static FilestackService.Process getProcessService() {
        if (processService == null) {
            Retrofit retrofit = getRetrofitBuilder().baseUrl(FilestackService.Process.URL).build();
            processService = retrofit.create(FilestackService.Process.class);
        }
        return processService;
    }

    public static FilestackService.Upload getUploadService() {
        if (uploadService == null) {
            Retrofit retrofit = getRetrofitBuilder().baseUrl(FilestackService.Upload.URL).build();
            uploadService = retrofit.create(FilestackService.Upload.class);
        }
        return uploadService;
    }

    public static void setCustomClient(OkHttpClient client) {
        if (client == null)
            return;
        httpClient = client;
        invalidate();
    }

    public static void removeCustomClient() {
        httpClient = null;
        invalidate();
    }

    private static Retrofit.Builder getRetrofitBuilder() {
        return new Retrofit.Builder()
                .client(getHttpClient())
                .addConverterFactory(GsonConverterFactory.create());
    }

    /**
     * Sets the services to null so they'll be recreated.
     */
    private static void invalidate() {
        cdnService = null;
        apiService = null;
        processService = null;
        uploadService = null;
    }
}

@@ -22,16 +22,54 @@
 .util;%0A%0A
+import java.util.concurrent.TimeUnit;%0A
 import o
@@ -89,16 +89,16 @@
 Client;%0A
-
 import r
@@ -175,47 +175,8 @@
 y;%0A%0A
-import java.util.concurrent.TimeUnit;%0A%0A
 /**%0A
@@ -606,16 +606,54 @@
 rvice;%0A%0A
+    /** Get http client singleton. */%0A
     publ
@@ -1128,32 +1128,70 @@
 pClient;%0A    %7D%0A%0A
+    /** Get CDN service singleton. */%0A
     public stati
@@ -1467,32 +1467,70 @@
 Service;%0A    %7D%0A%0A
+    /** Get API service singleton. */%0A
     public stati
@@ -1806,32 +1806,74 @@
 Service;%0A    %7D%0A%0A
+    /** Get process service singleton. */%0A
     public stati
@@ -2177,32 +2177,73 @@
 Service;%0A    %7D%0A%0A
+    /** Get upload service singleton. */%0A
     public stati
@@ -2540,32 +2540,69 @@
 Service;%0A    %7D%0A%0A
+    /** Set a custom http client. */%0A
     public stati
@@ -2674,16 +2674,18 @@
 == null)
+ %7B
 %0A       
@@ -2697,16 +2697,26 @@
 return;%0A
+        %7D%0A
         
@@ -2761,24 +2761,92 @@
 e();%0A    %7D%0A%0A
+    /** Remove custom http client. Resets to use default client. */%0A
     public s
@@ -3156,23 +3156,16 @@
 %0A    /**
-%0A     *
  Sets th
@@ -3207,21 +3207,16 @@
 created.
-%0A    
  */%0A    

Fix linter warnings for Networking.