package com.doverfuelingsolutions.issp.api.basic

import com.doverfuelingsolutions.issp.api.WayneApiConfig
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.time.Duration
import java.util.concurrent.TimeUnit

object RetrofitUtil {

    private val loggingInterceptor = LoggingInterceptor()
    private val authInterceptor = AuthInterceptor()
    private val basicHttpBuilder = OkHttpClient.Builder()
        .connectTimeout(WayneApiConfig.timeoutConnect, TimeUnit.SECONDS)
        .callTimeout(WayneApiConfig.timeoutAll, TimeUnit.SECONDS) // timeout for complete call
        .addInterceptor(loggingInterceptor)
    private val authHttpBuilder = OkHttpClient.Builder()
        .connectTimeout(WayneApiConfig.timeoutConnect, TimeUnit.SECONDS)
        .callTimeout(WayneApiConfig.timeoutAll, TimeUnit.SECONDS)
        .addInterceptor(loggingInterceptor)
        .addInterceptor(authInterceptor)

    private val mGsonConverterFactory: GsonConverterFactory by lazy { GsonConverterFactory.create() }

    fun getGsonBuilder(): Retrofit.Builder {
        return Retrofit.Builder()
            .addConverterFactory(mGsonConverterFactory)
            .client(basicHttpBuilder.build())
    }

    fun getAuthBuilder(): Retrofit.Builder {
        return Retrofit.Builder()
            .addConverterFactory(mGsonConverterFactory)
            .client(authHttpBuilder.build())
    }
}