package com.rc.httpcore

import android.content.Context
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.rc.httpcore.client.factory.ClientFactory
import com.rc.httpcore.client.factory.RetrofitFactory
import com.rc.httpcore.config.OkHttpDNS
import com.rc.httpcore.converter.NullOnEmptyConverterFactory
import com.rc.httpcore.interceptor.*
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit


object HttpClient {

    //    const val TIMEOUT_DEFAULT = 3 * 60L
    const val TIMEOUT_DEFAULT = 10L

    private var mAppContext: Context? = null

    @Volatile
    var token: String? = null

    var vName: String = "1.0.0"

    fun init(appContext: Context) {
        this.mAppContext = appContext
    }

    fun getAppContext() = mAppContext

    fun createClientFactory(): ClientFactory {
        return when (HttpConfig.HTTP_STRATEGY_Retrofit) {
            HttpConfig.HTTP_STRATEGY -> RetrofitFactory()
            else -> RetrofitFactory()
        }
    }

    fun <T> createRetrofitApi(
        apiClass: Class<T>,
        baseUrl: String = HttpConfig.API_BASE_URL,
    ): T {
        return buildRetrofit(baseUrl).create(apiClass)
    }

    private fun buildRetrofit(
        baseUrl: String = HttpConfig.API_BASE_URL,
        okHttpClient: OkHttpClient = buildHttpClient()
    ): Retrofit {
        return Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(baseUrl)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(NullOnEmptyConverterFactory())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    private fun buildHttpClient(): OkHttpClient {
        val logPath = "/sdcard/logs/"
        val config = LogUtils.getConfig()
        // log开关控制
        config.setLogSwitch(true)
        // log控制台开关
        config.setConsoleSwitch(true)
        // logTag
        config.setGlobalTag("Jayce")
        // log头部信息开关
        config.setLogHeadSwitch(true)
        // log文件开关
        config.setLog2FileSwitch(true)
        config.setDir(logPath)
        // log文件前缀
        config.setFilePrefix(AppUtils.getAppName())
        // log边框开关
        config.setBorderSwitch(true)
        // log文件保存天数
        config.setSaveDays(7)
//        val httpLoggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptorLog())//创建拦截对象
//        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY//这一句一定要记得写，否则没有数据输出
        return OkHttpClient.Builder()
            .readTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置读取超时时间
            .connectTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置请求超时时间
            .writeTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置写入超时时间
            .addNetworkInterceptor(TokenHeaderInterceptor())
//            .addInterceptor(RequestInterceptor())
            .addInterceptor(ParameterValidationInterceptor())
            .addInterceptor(ResponseInterceptor())
//            .addInterceptor(HttpLoggingInterceptorLog())//添加请求日志
//            .addInterceptor(NullToEmptyInterceptor())//添加请求日志
            .retryOnConnectionFailure(true) // 设置出现错误进行重新连接
            .dns(OkHttpDNS())
            .build()
//        if (!BuildConfig.DEBUG) {//debug模式下打印
//        }else{
//            return OkHttpClient.Builder()
//                .readTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置读取超时时间
//                .connectTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置请求超时时间
//                .writeTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置写入超时时间
//                .addNetworkInterceptor(TokenHeaderInterceptor())
//                .retryOnConnectionFailure(true) // 设置出现错误进行重新连接
//                .build()
//        }
    }


}