package com.yidian.shenghuoquan.bgeneralbusiness.service.net

import com.yidian.bcommon.http.HttpParamsUtils
import com.yidian.bcommon.http.HttpResult
import com.yidian.bcommon.http.HttpResultSubscriber
import com.yidian.bcommon.http.TransformUtil
import com.yidian.http.ServiceFactory
import com.yidian.shenghuoquan.bgeneralbusiness.service.net.callback.IBNARequestCallBack
import com.yidian.xarc.xrouter.ResultResolver

object BNARemoteDataSource {

  /**
   * 发起get请求
   */
  fun launchGetRequest(
    apiCallback: IBNARequestCallBack,
    requestParams: HashMap<String, String?>,
    url: String,
    isEncrypt: Boolean,
    resolver: ResultResolver
  ) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap =
      if (isEncrypt) HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp) else HttpParamsUtils.getPrivateParamsMap(requestParams)
    ServiceFactory.getInstance().createService(BNAService::class.java)
      .mbGetRequest(url, publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.getRequestSuccess(result, resolver)
        }

        override fun onFailure(result: HttpResult<Any?>?) {
          apiCallback.getRequestFailure(result, resolver)
        }
      })
  }

  /**
   * 发起post请求
   */
  fun launchPostRequest(
    apiCallback: IBNARequestCallBack,
    requestParams: HashMap<String, String?>,
    url: String,
    isEncrypt: Boolean,
    resolver: ResultResolver
  ) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap =
      if (isEncrypt) HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp) else HttpParamsUtils.getPrivateParamsMap(requestParams)
    ServiceFactory.getInstance().createService(BNAService::class.java)
      .mbPostRequest(url, publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.getRequestSuccess(result, resolver)
        }

        override fun onFailure(result: HttpResult<Any?>?) {
          apiCallback.getRequestFailure(result, resolver)
        }
      })
  }
}
