package com.abbidot.baselibrary.network.interceptor

import android.os.Build
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.baselibrary.util.Utils
import okhttp3.FormBody
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.Response


/**
 * 开始请求的拦截器
 * 拦截器获取GET/POST请求参数并添加公共请求头
 */
class CommonRequestInterceptor : Interceptor {

    //https://blog.csdn.net/u014619545/article/details/98634994
//    companion object {
//        //根据headers来动态切换BaseUrl
//        const val HEADERS_NAME = "urlName"
//        const val HEADERS_value = "WXBaseUrl"
//
//        //微信BaseUrl
//        const val WX_BASE_URL = "https://api.weixin.qq.com/sns/"
//    }

    override fun intercept(chain: Interceptor.Chain): Response {
        //获取原始的chain
        val request = chain.request()
        //获取chain的创建者builder
        val builder = request.newBuilder()
        // 手机型品牌
        builder.addHeader("brand", Build.BRAND)
        //手机型号
        builder.addHeader("model", Build.MODEL)

        //请求参数的key+value添加list中
        val paramList = mutableListOf<String>()
        // GET方法
        if (request.method == "GET") {
            val urlBuilder: HttpUrl.Builder = request.url.newBuilder()
            val httpUrl = urlBuilder.build()
            // 打印全部get参数
            val paramKeys: Set<String> = httpUrl.queryParameterNames
            for (key in paramKeys) {
                val value = httpUrl.queryParameter(key)
                LogUtil.e("${request.method}请求参数", "$key,$value")
                paramList.add("$key$value")
            }

            //动态更改BaseUrl
            //获取当前的BaseUrl
//            val oldBaseUrl = request.url
//            //获取头信息的集合
//            val urlNameList: List<String> = request.headers(HEADERS_NAME)
//            if (urlNameList.isNotEmpty()) {
//                val urlName = urlNameList[0]
//                //删除原有配置中的值,就是namesAndValues集合里的值
//                builder.removeHeader(HEADERS_NAME)
//                LogUtil.e("HeaderName$urlName")
//            }
        } else if (request.method == "POST") {
            if (request.body is FormBody) {
                val formBody = request.body as FormBody
                // 打印全部post参数
                for (i in 0 until formBody.size) {
                    val key = formBody.encodedName(i)
                    val value = formBody.encodedValue(i)
                    LogUtil.e("${request.method}请求参数", "${key},${value}")
                    paramList.add("$key$value")
                }
            }
        }

        val sign = if (paramList.size > 0) {
            //按key的字母排序
            paramList.sort()
            Utils.get32Md5Value(Utils.SECRET_KEY + paramList.joinToString(""))
        } else {
            Utils.get32Md5Value(Utils.SECRET_KEY)
        }

        val token = MMKVUtil.getString(MMKVKey.Token)
        builder.addHeader("token", token)
        builder.addHeader("sign", sign)

        return chain.proceed(builder.build())
    }
}