package com.dahe.gldriver.net

import kotlin.Throws
import com.dahe.gldriver.base.App
import android.text.TextUtils
import android.util.Log
import com.dahe.gldriver.utils.SPUtils
import com.dahe.mylibrary.utils.BaseUtils
import com.dahe.mylibrary.utils.AppUtils
import com.dahe.mylibrary.utils.BaseSPUtils
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * 头部Interceptor
 */
class RequestHeadInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val builder: Request.Builder = chain.request().newBuilder()
        if (BaseSPUtils.hasUserInfo(App.app) && !TextUtils.isEmpty(SPUtils.instance.getUserInfo(App.app)?.token)) {

//            builder.addHeader("Authorization", SPUtils.getUserInfo(App.getApp()).getToken());
            builder.addHeader("Authorization",
                "Bearer " + SPUtils.instance.getUserInfo(App.app)?.token)
        }
        val request: Request = builder
            .addHeader("versionCode", AppUtils.getAppVersionCode().toString() + "")
            .addHeader("versionValue", AppUtils.getAppVersionName() + "")
            .addHeader("deviceType", "ANDROID")
            .addHeader("language", BaseUtils.getLanguage(App.app).toString() + "")
            .build()
        Log.i(TAG, "intercept: " + request.headers)
        return chain.proceed(request)
    }

    companion object {
        private const val TAG = "RequestHeadInterceptor"
    }
}