package com.renrenliao.im.http


import com.renrenliao.im.utils.DataCacheUtils
import com.renrenliao.im.utils.MDateUtils
import com.renrenliao.im.utils.MapUtils
import okhttp3.Interceptor
import okhttp3.Response


class HeaderInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {

        val orignrequest = chain.request()
        val builder = orignrequest.newBuilder()
        val time = MDateUtils.currentTimMillis().toString()//System.currentTimeMillis().toString()

        builder.header("Accept-Language", "zh-CN")
        builder.header("time", time)
        builder.header("accessToken",DataCacheUtils.getToken())
        builder.header("userUid", DataCacheUtils.getUid())
        builder.header("secret", DataCacheUtils.getSecret(time))
        builder.header("deviceId", DataCacheUtils.getDeviceId())
        //添加固定头部
        MapUtils.getHttpHeader().entries.forEach {
            builder.header(it.key,it.value)
        }
//        for (Map.Entry<String, String> entry : MapU.entrySet()) {
//            builder.header(entry.getKey(), entry.getValue());
//        }


        //todo 添加头部header 一般情况下 if isLogin 从kv中获取token .addHeader("token",kv.decodeString(KEY_TOKEN,""))
        //从kv读取cookie cookie存储的方案二
       /* val cookies = kv.decodeStringSet(KEY_COOKIE, emptySet())
        for (cookie in cookies) {
            builder.addHeader("Cookie", cookie)
        }*/

        val build = builder.build()
        return chain.proceed(build)

    }

}