package com.cheng.blzb.net

import com.cheng.blzb.BuildConfig
import com.cheng.blzb.manager.LoginManager
import com.cheng.blzb.utils.ChannelUtils
import com.example.base.utils.AppUtils
import com.example.base.utils.L
import com.example.base.utils.Utils
import com.github.gzuliyujiang.oaid.DeviceIdentifier
import com.cheng.blzb.common.Constants
import com.cheng.blzb.manager.UserConfigManager
import okhttp3.Interceptor
import okhttp3.Response

class RequestHeaderInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        val requestBuilder = request.newBuilder()

        //添加header
        request = requestBuilder
            .addHeader("x-token", LoginManager.getToken())
            .addHeader("x-version", AppUtils.getAppVersionName())
            .addHeader("x-platform", "android")
            .addHeader("x-device-id", DeviceIdentifier.getAndroidID(Utils.getApp()))
            .addHeader("x-mobile-brand", android.os.Build.BRAND)
            .addHeader("x-mobile-model", android.os.Build.MODEL)
            .addHeader("x-base-version", AppUtils.getAppVersionName())
            .addHeader("x-channel", "bole_${ChannelUtils.getChannel()}")
            .addHeader("x-click-id", UserConfigManager.getBDVID())
            .addHeader("x-app-id", Constants.APP_ID)
            .addHeader("x-package", BuildConfig.APPLICATION_ID)
            .build()
        val stringBuilder = StringBuilder()
        stringBuilder.append("-------------header start-------------\n")
        stringBuilder.append("x-token        = ${LoginManager.getToken()}\n")
        stringBuilder.append("x-version      = ${AppUtils.getAppVersionName()}\n")
        stringBuilder.append("x-device-id    = ${DeviceIdentifier.getAndroidID(Utils.getApp())}\n")
        stringBuilder.append("x-mobile-brand = ${android.os.Build.BRAND}\n")
        stringBuilder.append("x-mobile-model = ${android.os.Build.MODEL}\n")
        stringBuilder.append("x-base-version = ${AppUtils.getAppVersionName()}\n")
        stringBuilder.append("x-channel      = bole_${ChannelUtils.getChannel()}\n")
        stringBuilder.append("x-click-id     = ${UserConfigManager.getBDVID()}\n")
        stringBuilder.append("x-package      = ${BuildConfig.APPLICATION_ID}\n")
        stringBuilder.append("-------------header end-------------")
        L.d(stringBuilder)

        return chain.proceed(request)
    }


}