package com.ttiglobal.inttilectgpchat.util;

import androidx.multidex.MultiDexApplication;

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

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


public class MainApplication extends MultiDexApplication {

    Retrofit restClient;
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

    @Override
    public void onCreate() {
        super.onCreate();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        restClient = new Retrofit.Builder()
                .baseUrl(GlobalData.SERVER_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .readTimeout(60, TimeUnit.SECONDS)
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request request = chain.request();
                                HttpUrl url = request.url().newBuilder().build();
                                request = request.newBuilder().url(url).build();
                                return chain.proceed(request);
                            }
                        }).addInterceptor(loggingInterceptor)
                        .build())
                .build();

    }

    public void addAccessToken(final String accessToken) {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        restClient = new Retrofit.Builder()
                .baseUrl(GlobalData.SERVER_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .readTimeout(60, TimeUnit.SECONDS)
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request.Builder ongoing = chain.request().newBuilder();
                                ongoing.header("Authorization", accessToken);
                                ongoing.header("Accept", "application/json");
                                ongoing.method(chain.request().method(), chain.request().body());
                                return chain.proceed(ongoing.build());
                            }
                        }).addInterceptor(loggingInterceptor)
                        .build())
                .build();
    }

    public ApiService getClient() {
        return restClient.create(ApiService.class);
    }

}
