package com.yidian.bcommon.http

import com.google.gson.reflect.TypeToken
import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.BuildConfig
import com.yidian.bcommon.app.YdBaseApplication
import com.yidian.bcommon.http.bean.CommitFeedbackBean
import com.yidian.bcommon.http.bean.GetCommonConfigBean
import com.yidian.bcommon.http.bean.GetKSYunObjectIdBean
import com.yidian.bcommon.http.bean.GetKSYunTokenBean
import com.yidian.bcommon.http.callback.ICommitFeedbackCallback
import com.yidian.bcommon.http.callback.IGetCommonConfigCallback
import com.yidian.bcommon.http.callback.IGetKSYunObjectIdCallback
import com.yidian.http.ServiceFactory
import com.yidian.utils.ToastUtil

/**
 * author: yinjiacheng
 * date: 7/8/21 4:54 PM
 * description: Common网络请求实现
 */
object CommonDataSource {

  // Tower-Api 域名
  private val domainTowerApi by lazy {
    if (BuildConfig.TOWER_API_ENV == AppConfig.TowerApiEnvTest) {
      CommonService.SCHEME_HTTP + CommonService.DOMAIN_TOWER_API_TEST
    } else {
      CommonService.SCHEME_HTTPS + CommonService.DOMAIN_TOWER_API_PRO
    }
  }

  private val towerApi by lazy {
    ServiceFactory.getInstance().createService(CommonService::class.java, domainTowerApi)
  }

  private val ydJiraApi by lazy {
    ServiceFactory.getInstance().createService(CommonService::class.java, CommonService.SCHEME_HTTP + CommonService.DOMAIN_YD_JIRA)
  }

  // 301 获取金山云token接口
  fun getKSYunToken(requestParams: HashMap<String, String?>): HttpResult<GetKSYunTokenBean.Response>? {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    val res: HttpResult<Any?>? = ServiceFactory.getInstance().createService(CommonService::class.java)
      .getKSYunToken(publicParamsMap, privateParamsMap)
      .execute().body()
    if (res?.code == 0) {
      return HttpParamsUtils.rsaDecryptResult(res, object : TypeToken<GetKSYunTokenBean.Response>() {}.type)
    } else {
      val decodeResult = HttpResult<GetKSYunTokenBean.Response>()
      decodeResult.code = res?.code!!
      decodeResult.reason = res.reason
      decodeResult.status = res.status
      ToastUtil.showToast(YdBaseApplication.context, res.reason)
    }
    return null
  }

  // 302 获取金山云ObjectId和Bucket接口
  fun getKSYunObjectId(apiCallback: IGetKSYunObjectIdCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    ServiceFactory.getInstance().createService(CommonService::class.java)
      .getKSYunObjectId(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<GetKSYunObjectIdBean.Response>() {
        override fun onSuccess(result: HttpResult<GetKSYunObjectIdBean.Response>?) {
          apiCallback.getKSYunObjectIdSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<GetKSYunObjectIdBean.Response>?) {
          apiCallback.getKSYunObjectIdFailure(result?.reason)
        }
      })
  }

  // TowerPro 获取配置数据
  fun getCommonConfig(apiCallback: IGetCommonConfigCallback, requestParams: HashMap<String, String>) {
    towerApi
      .getCommonConfig(requestParams)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultObserver<GetCommonConfigBean.GetCommonConfigResponse>() {
        override fun onSuccess(result: HttpResult<GetCommonConfigBean.GetCommonConfigResponse>?) {
          apiCallback.getCommonConfigSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<GetCommonConfigBean.GetCommonConfigResponse>?) {
          apiCallback.getCommonConfigFailure(result?.status)
        }
      })
  }

  // 用户反馈提交
  fun commitFeedback(apiCallback: ICommitFeedbackCallback, requestParams: CommitFeedbackBean.CommitFeedbackRequest) {
    ydJiraApi
      .commitFeedback(requestParams)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultObserver<CommitFeedbackBean.CommitFeedbackResponse>() {
        override fun onSuccess(result: HttpResult<CommitFeedbackBean.CommitFeedbackResponse>?) {
          apiCallback.commitFeedbackSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<CommitFeedbackBean.CommitFeedbackResponse>?) {
          apiCallback.commitFeedbackFailure(result?.reason)
        }

      })
  }
}
