package com.yidian.bcommon.sdk

import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.yac.core.zap.ZapTicket
import org.json.JSONObject

/**
 * author: yinjiacheng
 * date: 7/27/21 2:01 PM
 * description: BCommon Manager
 */
object BCommonManager {

  const val TAG_HTTP = "merchant-b-http"

  /**
   * 加密接口实现
   */
  private lateinit var encryptIml: IEncryptService

  /**
   * 获取AppId
   */
  fun getAppId(): String {
    return when (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName)) {
      ClientType.MERCHANT_B -> AppConfig.appIdMerchantB
      ClientType.MERCHANT_C -> AppConfig.appIdMerchantC
    }
  }

  /**
   * 获取TowerPro通用配置模块KeyTag
   */
  fun getTowerProKeyTag(): String {
    return when (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName)) {
      ClientType.MERCHANT_B -> AppConfig.TowerApiKeyTagMerchantB
      ClientType.MERCHANT_C -> AppConfig.TowerApiKeyTagMerchantC
    }
  }

  /**
   * 获取C端uid
   */
  fun getUid(): String? {
    var userId: String? = null
    if (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName) == ClientType.MERCHANT_C) {
      ZapTicket(ZapServiceNameConstants.ClientInfoService).withAction(ZapServiceActionConstants.ActionGetUserInfo).onResult {
        userId = (it.result as JSONObject).optString("userId")
      }.ship()
    }
    return userId
  }

  /**
   * 获取C端用户昵称
   */
  fun getUserName(): String? {
    var userName: String? = null
    if (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName) == ClientType.MERCHANT_C) {
      ZapTicket(ZapServiceNameConstants.ClientInfoService).withAction(ZapServiceActionConstants.ActionGetUserInfo).onResult {
        userName = (it.result as JSONObject).optString("userName")
      }.ship()
    }
    return userName
  }

  /**
   * 获取C端用户手机号
   */
  fun getUserPhone(): String? {
    var userPhone: String? = null
    if (JudgeClientUtils.judgeClient(YdBaseApplication.context.packageName) == ClientType.MERCHANT_C) {
      ZapTicket(ZapServiceNameConstants.ClientInfoService).withAction(ZapServiceActionConstants.ActionGetUserInfo).onResult {
        userPhone = (it.result as JSONObject).optString("userPhone")
      }.ship()
    }
    return userPhone
  }

  /**
   * 获取加密接口实现
   */
  fun getEncryptIml(): IEncryptService {
    return encryptIml
  }

  /**
   * 添加加密接口实现
   */
  fun setEncryptIml(service: IEncryptService) {
    encryptIml = service
  }
}
