package com.doverfuelingsolutions.issp.api.basic

import com.doverfuelingsolutions.issp.data.DataStore
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitUtil {

    private val mOkHttpClient = OkHttpClient.Builder().apply {
        addInterceptor {
            val request = it.request().newBuilder()
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader("DeviceSN", DataStore.sn)
            if (DataStore.accessToken.isNotBlank()) request.header("Authorization", "Bearer ${DataStore.accessToken}")
            if (DataStore.buId.isNotBlank()) request.header("CurrentBuId", DataStore.buId)
            it.proceed(request.build())
        }
    }

    fun getGsonBuilder(): Retrofit.Builder {
        return Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
    }

    fun getAuthBuilder(): Retrofit.Builder {
        return getGsonBuilder()
            .client(mOkHttpClient.build())
    }
}