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, resolver: ResultResolver) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    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, resolver: ResultResolver) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    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)
        }
      })
  }
}
