package com.img.rabbit.provider.utils

import android.util.Log
import com.github.gzuliyujiang.oaid.DeviceIdentifier
import com.img.rabbit.provider.storage.PreferenceUtil
import com.img.rabbit.BuildConfig
import com.img.rabbit.config.Constants
import com.img.rabbit.config.Constants.LOG_REQUEST
import com.img.rabbit.provider.storage.PreferenceUtil.getBDVID
import com.img.rabbit.provider.utils.HeadParamUtils.applicationContext
import com.img.rabbit.provider.utils.HeadParamUtils.getAppVersionName
import com.img.rabbit.utils.ChannelUtils
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

class HeaderInterceptor : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val original = chain.request()
        try {
            val mBuilder = original.newBuilder()
            val accessToken = PreferenceUtil.getXToken()?:""
            mBuilder.header("x-token", accessToken)
            mBuilder.header("x-version", getAppVersionName()?:"")
            mBuilder.header("x-platform", "android")
            mBuilder.header("x-device-id", DeviceIdentifier.getAndroidID(applicationContext))
            mBuilder.header("x-mobile-brand", android.os.Build.BRAND)
            mBuilder.header("x-mobile-model", android.os.Build.MODEL)
            mBuilder.header("x-package", BuildConfig.APPLICATION_ID)
            mBuilder.header("x-base-version", getAppVersionName()?:"")
            mBuilder.header("x-channel", "rabbit_${ChannelUtils.getChannel(applicationContext)}")
            mBuilder.header("x-click-id", getBDVID())
            mBuilder.header("x-app-id", Constants.AppId)

            val stringBuilder = StringBuilder()
            stringBuilder.append("-------------> 📤 header start<-------------\n")
            stringBuilder.append("│ x-token        = $accessToken\n")
            stringBuilder.append("│ x-version      = ${getAppVersionName()}\n")
            stringBuilder.append("│ x-device-id    = ${DeviceIdentifier.getAndroidID(applicationContext)}\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 = ${getAppVersionName()}\n")
            stringBuilder.append("│ x-channel      = rabbit_${ChannelUtils.getChannel(applicationContext)}\n")
            stringBuilder.append("│ x-click-id     = ${getBDVID()}\n")
            stringBuilder.append("│ x-package      = ${BuildConfig.APPLICATION_ID}\n")
            stringBuilder.append("------------->    header end     <-------------")

            Log.i(LOG_REQUEST, stringBuilder.toString())


            val request = mBuilder
                .method(original.method, original.body)
                .build()

            val response = chain.proceed(request)
            /*
            // 获取响应头
            val headers = response.headers
            // 处理响应头
            headers.forEach { header ->
                //Log.d(LOG_REQUEST, "ResponseHeader:${header.first} ${header.second}")
                if (header.first == "Access-Token") {
                    authorization = header.second
                }
            }
             */

            return response
        } catch (e: Exception) {
            e.printStackTrace()
            return chain.proceed(original)
        }
    }
}