package com.yidian.shenghuoquan.bgeneralbusiness.config

import com.yidian.bcommon.BuildConfig
import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.constant.RunConfig
import com.yidian.bcommon.http.ApiSaveCookiesInterceptor
import com.yidian.bcommon.http.ApiSetCookiesInterceptor
import com.yidian.bcommon.sdk.ClientType
import com.yidian.bcommon.sdk.JudgeClientUtils
import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.framework.mobile.insight.config.ServerUrlConfig
import com.yidian.framework.mobile.insight.config.XDiamondProvide
import com.yidian.framework.mobile.insight.manage.XInsight
import com.yidian.framework.mobile.insight.manage.XInsightConfig
import com.yidian.framework.mobile.xdiamond.SecretUtil
import com.yidian.http.ServiceFactory
import com.yidian.yac.core.zap.ZapTicket
import com.yidian.yac.ftdevicefinger.core.FtDeviceFingerManager
import org.json.JSONObject

/**
 * author: yinjiacheng
 * date: 8/3/21 11:01 AM
 * description: 需要延迟初始化的SDK
 */
object LazyInitConfig {

  /**
   * 初始化Http框架
   */
  fun initHttp() {
    ServiceFactory.getInstance().init(true, RunConfig.API_URL, 10, 10)
    if (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName) == ClientType.MERCHANT_B) {
      // 添加cookie拦截器
      ServiceFactory.getInstance().addInterceptor(ApiSaveCookiesInterceptor())
      ServiceFactory.getInstance().addInterceptor(ApiSetCookiesInterceptor())
    }
    // 初始化磐石SDK
    SecretUtil.init(RunConfig.IS_ENCRYPT_DEBUG_KEY, BuildConfig.DEBUG)
  }

  /**
   * 初始化慧眼
   */
  fun initXInsight() {
    var appId: String? = null // 业务线
    var cv: String? = null // 客户端版本号
    var distribution: String? = null // 渠道
    var version: String? = null // 服务端版本号
    if (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName) == ClientType.MERCHANT_C) {
      // C端XInsight初始化参数通过服务获取
      ZapTicket(ZapServiceNameConstants.ClientInfoService).withAction(ZapServiceActionConstants.ActionGetRequestPublicParameters)
        .onResult {
          val result = it.result as JSONObject
          appId = result.optString("appid")
          cv = result.optString("cv")
          distribution = result.optString("distribution")
          version = result.optString("version")
        }.ship()
    } else {
      appId = AppConfig.appIdMerchantB
      cv = AppConfig.cv
      distribution = AppConfig.distribution
      version = RunConfig.SERVICE_VERSION
    }
    val config = XInsightConfig()
    config.cv = cv
    config.device_finger = FtDeviceFingerManager.getDeviceFinger()
    config.distribution = distribution
    config.isEncryption = RunConfig.IS_ENCRYPT
    config.version = version
    config.serverUrlConfig = when (RunConfig.XINSIGHT_ENV) {
      AppConfig.XInsightEnvTest -> ServerUrlConfig.TEST_SERVER_URL
      AppConfig.XInsightEnvPre -> ServerUrlConfig.PRE_SERVER_URL
      AppConfig.XInsightEnvPro -> ServerUrlConfig.PRO_SERVER_URL
      else -> ServerUrlConfig.DEV_SERVER_URL
    }
    val provide = object : XDiamondProvide {
      override fun sign(str: String?): String? {
        if (str == null) return null
        return SecretUtil.sign(str)
      }

      override fun rsaEncrypt(str: String?): String? {
        if (str == null) return null
        return SecretUtil.rsaEncrypt(str)
      }

      override fun rsaDecrypt(str: String?): String? {
        if (str == null) return null
        return SecretUtil.rsaDecrypt(str)
      }

      override fun aesEncrypt(str: String?): String? {
        if (str == null) return null
        return SecretUtil.aesEncrypt(str)
      }

      override fun aesDecrypt(str: String?): String? {
        if (str == null) return null
        return SecretUtil.aesDecrypt(str)
      }

      override fun verifySign(src: String?, sign: String?): Boolean {
        if (src == null || sign == null) return false
        return SecretUtil.verifySign(src, sign)
      }

    }
    XInsight.init(YdBaseApplication.context, appId, BuildConfig.DEBUG, config, provide)
  }
}
