package com.yidian.bcommon.http

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.utils.NetWorkUtils
import com.yidian.framework.mobile.xdiamond.SecretUtil
import com.yidian.utils.LogUtil
import com.yidian.utils.ToastUtil
import com.yidian.yac.ftdevicefinger.core.FtDeviceFingerManager
import java.lang.reflect.Type

class HttpParamsUtils {
  companion object {
    fun getPublicParamsMap(timeStamp: Long): HashMap<String, String> {
      val networkType = NetWorkUtils.getNetWorkType(YdBaseApplication.context)
      val queryParamsMap = HashMap<String, String>()
      queryParamsMap["appid"] = AppConfig.appid
      queryParamsMap["cv"] = AppConfig.cv
      queryParamsMap["version"] = AppConfig.version
      queryParamsMap["distribution"] = AppConfig.distribution
      queryParamsMap["net"] = networkType
      queryParamsMap["platform"] = AppConfig.platform
      queryParamsMap["device_finger"] = getDeviceFinger()
      queryParamsMap["reqid"] = getRequestId(timeStamp)
      return queryParamsMap
    }

    fun getPrivateParamsMap(paramsMap: HashMap<String, String?>, timeStamp: Long): HashMap<String, String?> {
      return if (AppConfig.IS_ENCRYPT) {
        val reqId = getRequestId(timeStamp)
        paramsMap["secret"] = getSignString(reqId, timeStamp)
        paramsMap["ts"] = timeStamp.toString()
        val paramsJson = Gson().toJson(paramsMap)
        LogUtil.show("请求参数：$paramsJson")
        val tok = SecretUtil.rsaEncrypt(paramsJson)
        val queryParamsMap = HashMap<String, String?>()
        queryParamsMap["tok"] = tok
        queryParamsMap
      } else {
        paramsMap
      }
    }

    fun <T> rsaDecryptResult(res: HttpResult<Any?>, type: Type): HttpResult<T> {
      val rsaResult = res.result
      val decodeResult = HttpResult<T>()
      decodeResult.code = res.code
      decodeResult.reason = res.reason
      decodeResult.status = res.status
      if (rsaResult != null) {
        val gson = Gson()
        if (AppConfig.IS_ENCRYPT) {
          val result = SecretUtil.rsaDecrypt(rsaResult as String)
          LogUtil.show("返回参数：$result")
          val decryptResult = gson.fromJson<HttpDecryptResult<T>>(result, object : TypeToken<HttpDecryptResult<T>>() {}.type)
          val verifyParams = decryptResult.reqid + decryptResult.ts
          val secretNet = decryptResult.secret
          val verify = SecretUtil.verifySign(verifyParams, secretNet)
          return if (verify) {
            LogUtil.show("===============验签成功==================")
            val resultJson = gson.toJson(decryptResult.data)
            decodeResult.result = gson.fromJson<T>(resultJson, type)
            decodeResult
          } else {
            ToastUtil.showToast(YdBaseApplication.context, "验签失败")
            decodeResult
          }
        } else {
          val resultJson = gson.toJson(rsaResult)
          decodeResult.result = gson.fromJson<T>(resultJson, type)
          return decodeResult
        }
      }
      LogUtil.show("===============rsaResult为空了==================")
      return decodeResult
    }

    private fun getDeviceFinger(): String {
      var deviceFinger = ""
      val tempId = FtDeviceFingerManager.getDeviceFinger()
      if (tempId != null) {
        deviceFinger = tempId
      }
      return deviceFinger
    }

    private fun getRequestId(timeStamp: Long): String {
      val sb = StringBuilder()
      sb.append(AppConfig.appid)
      sb.append('_')
      sb.append(getDeviceFinger())
      sb.append('_')
      sb.append(timeStamp)
      return sb.toString()
    }

    private fun getSignString(reqId: String, timeStamp: Long): String? {
      val sb = StringBuilder()
      sb.append(reqId).append(timeStamp)
      return SecretUtil.sign(sb.toString())
    }
  }
}
