package com.swago.baseswago.http

import com.google.gson.Gson
import com.readystatesoftware.chuck.ChuckInterceptor
import com.swago.baseswago.BuildConfig
import com.swago.baseswago.constant.UrlConstant
import com.swago.baseswago.util.AppContext
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit

/**
 *@date 2021/8/14 14:05
 *description:
 */
object RetrofitManager {

    var retrofit: Retrofit

    val gson = Gson()
    private var okHttpClient: OkHttpClient? = null

    init {
        val okHttpClientBuilder = OkHttpClient.Builder()
        if (BuildConfig.DEBUG){
            okHttpClientBuilder.addInterceptor(ChuckInterceptor(AppContext.getContext()))
        }
        okHttpClientBuilder.addInterceptor(SwagoInterceptor())
        okHttpClientBuilder.retryOnConnectionFailure(true)
        okHttpClientBuilder.connectTimeout(30000, TimeUnit.MILLISECONDS)
        okHttpClient = okHttpClientBuilder.build()

        retrofit = Retrofit.Builder()
            .baseUrl(UrlConstant.BASE_URL)
            .addConverterFactory(SwagoJsonFactor.create(gson))
            .client(okHttpClient)
            .build()
    }

    fun resetUrl(){
        retrofit = Retrofit.Builder()
            .baseUrl(UrlConstant.BASE_URL)
            .addConverterFactory(SwagoJsonFactor.create(gson))
            .client(okHttpClient)
            .build()
    }

}