package net.kwmt27.codesearch.model;

import android.text.TextUtils;

import net.kwmt27.codesearch.BuildConfig;
import net.kwmt27.codesearch.ModelLocator;
import net.kwmt27.codesearch.model.rx.GsonFactory;
import net.kwmt27.codesearch.model.rx.RxErrorHandlingCallAdapterFactory;

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

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

    private static final String BASE_API_URL = BuildConfig.BASE_API_URL;
    public final GitHubService api;
    public final GitHubLoginService login;
    public final Retrofit mRetrofit;

    private OkHttpClient mClient;

    public ApiClient() {

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        mClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new HeaderInterceptor())
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();


        mRetrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_API_URL)
                .client(mClient)
                .addConverterFactory(GsonConverterFactory.create(GsonFactory.create()))
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .build();

        api = mRetrofit.create(GitHubService.class);

        Retrofit retrofitForLogin = new Retrofit.Builder()
                .baseUrl("https://github.com")
                .client(mClient)
                .addConverterFactory(GsonConverterFactory.create(GsonFactory.create()))
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .build();
        login = retrofitForLogin.create(GitHubLoginService.class);
    }



    private static class HeaderInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder builder = original.newBuilder();
            if(!TextUtils.isEmpty(ModelLocator.getLoginModel().getAccessToken())) {
                builder.header("Authorization", "token " + ModelLocator.getLoginModel().getAccessToken());
            }
            return chain.proceed(builder.build());
        }
    }


    public String buildUrl(String path) {
        return BASE_API_URL + path;
    }

    /**
     * リクエスト共通処理
     *
     * @param path エンドポイント
     * @param body POST内容
     */
    public Response request(final String path, final RequestBody body) throws IOException {

        String url = buildUrl(path);
        Request.Builder builder = new Request.Builder()
                .url(url);

        if (body != null) {
            builder = builder.post(body);
        }
        final Request request = builder.build();
        return mClient.newCall(request).execute();
    }

}

@@ -425,36 +425,8 @@
 st;%0A
-import okhttp3.RequestBody;%0A
 impo
@@ -446,16 +446,16 @@
 sponse;%0A
+
 import o
@@ -795,43 +795,8 @@
 t;%0A%0A
-    private OkHttpClient mClient;%0A%0A
     
@@ -980,18 +980,30 @@
         
-mC
+OkHttpClient c
 lient = 
@@ -1433,34 +1433,33 @@
         .client(
-mC
+c
 lient)%0A         
@@ -1831,18 +1831,17 @@
 .client(
-mC
+c
 lient)%0A 
@@ -2628,24 +2628,24 @@
 r.build());%0A
+
         %7D%0A  
@@ -2653,586 +2653,6 @@
  %7D%0A%0A
-%0A    public String buildUrl(String path) %7B%0A        return BASE_API_URL + path;%0A    %7D%0A%0A    /**%0A     * %E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E5%85%B1%E9%80%9A%E5%87%A6%E7%90%86%0A     *%0A     * @param path %E3%82%A8%E3%83%B3%E3%83%89%E3%83%9D%E3%82%A4%E3%83%B3%E3%83%88%0A     * @param body POST%E5%86%85%E5%AE%B9%0A     */%0A    public Response request(final String path, final RequestBody body) throws IOException %7B%0A%0A        String url = buildUrl(path);%0A        Request.Builder builder = new Request.Builder()%0A                .url(url);%0A%0A        if (body != null) %7B%0A            builder = builder.post(body);%0A        %7D%0A        final Request request = builder.build();%0A        return mClient.newCall(request).execute();%0A    %7D%0A%0A
 %7D%0A

delete unused method