package com.renrenliao.im.utils

import com.google.gson.GsonBuilder
import com.renrenliao.im.config.Const
import com.renrenliao.im.provider.ClarityPotion
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File



object MapUtils {


    fun map2JsonRequestBody(map: Map<String, Any>): RequestBody {
        val gson = GsonBuilder().serializeNulls().create()
        val jsonStr = gson.toJson(map)
        return jsonStr.toRequestBody("application/json; charset=utf-8".toMediaType())
    }

    fun map2JsonStr(map: Map<String, Any>): String {
        val gson = GsonBuilder().serializeNulls().create()
        return gson.toJson(map)
    }


    /**
     * filePath 本地路径
     * fileKey  上传key  如：file
     */
    fun file2MultipartBody(filePath: String, fileKey: String): MultipartBody {
        val file = File(filePath) //filePath为图片位置
        val fileBody = file.asRequestBody("image/*".toMediaType())
        return MultipartBody.Builder()
            .addFormDataPart(fileKey, file.name, fileBody)
            .build()
    }

    fun file2RequestBody(filePath: String, fileKey: String): RequestBody {

        //android 10 以上 根据uri找到文件

        val file = File(filePath)/*= if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
           FileUtils.getFileFromUri(ClarityPotion.clarityPotion,Uri.parse(filePath))
        }else{

        }*/
        //filePath为图片位置
        val fileBody = file.asRequestBody("image/*".toMediaType())
        return MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart(fileKey, file.name, fileBody)
            .build()
    }

    fun getComParam(map: HashMap<String, String>):HashMap<String,String>{
        map["modleType"] = "android"
        map["loginId"] = DataCacheUtils.getUid()
        map["userUid"] = DataCacheUtils.getUid()
        return map
    }

    fun getComHeaders():HashMap<String,String>{
        val map = HashMap<String,String>()
        val time = MDateUtils.currentTimMillis().toString()
        val uid = DataCacheUtils.getUid()
        val token = DataCacheUtils.getToken()
        val str = Const.APICODE + time + uid + token
        val secret = DigestUtils.md5(str).trim()
        map["time"] = time
        map["secret"] = secret
        map["userUid"] = uid
        map["accessToken"] = token

        return map
    }

    /**
     * 公共的头文件 只生成一次 所以一些动态实时的头文件是不可以放在这里的
     */
    fun getHttpHeader(): HashMap<String,String>{
        val paramMap = HashMap<String,String>()
        paramMap["versionName"] = DeviceUtils.getVersionName()
        paramMap["versionCode"] = DeviceUtils.getVersionCode().toString()
        paramMap["modleType"] = DeviceUtils.getMobileType() //手机类型
        paramMap["brand"] = DeviceUtils.getPhoneManu() //手机品牌
        paramMap["systemVersion"] = DeviceUtils.getSdkVertion().toString() //系统版本
        paramMap["networkType"] = NetworkUtils.getNetworkTypeStr(ClarityPotion.healingSalve) //网络类型
      //  paramMap.put("channelType", "local1") //渠道号
        paramMap["Content-Type"] = "application/json;charset=UTF-8"
        return paramMap
    }


}
