package com.yono.messeripos.api;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yono.messeripos.BuildConfig;

import java.util.concurrent.TimeUnit;

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

public class ApiHelper {
    public static final String BASE_URL = "https://rest-api-meser.herokuapp.com/";
    public static final String VERSI_API_1 = "api/v1/";
    public static final String BASE_URL_IMAGE = "https://storage.googleapis.com/rest-api-meser.appspot.com/images/";

    public static Retrofit request(String baseUrl){

        OkHttpClient.Builder okHttp = new OkHttpClient().newBuilder();
        okHttp.retryOnConnectionFailure(true);
        okHttp.connectTimeout(60, TimeUnit.SECONDS);
        okHttp.writeTimeout(60, TimeUnit.SECONDS);
        okHttp.readTimeout(60, TimeUnit.SECONDS);

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(s -> Log.e("API-LOG", s));
        interceptor.level(HttpLoggingInterceptor.Level.BODY);
        if (BuildConfig.DEBUG) okHttp.addInterceptor(interceptor);

        okHttp.addInterceptor(chain -> {
            Request request = chain.request();
            Request newReq = request.newBuilder()
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                    .addHeader("Accept-Encoding", "identity")
                    .addHeader("Connection", "close")
                    .addHeader("Transfer-Encoding", "chunked")
                    .build();
            return chain.proceed(newReq);
        });

        OkHttpClient client = okHttp.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
//                .client(client)
                .addConverterFactory(GsonConverterFactory.create(
                        new GsonBuilder().setLenient().create()
                ))
                .build();

        return retrofit;
    }
}
