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

import com.yidian.bcommon.http.*
import com.yidian.http.ServiceFactory
import com.yidian.shenghuoquan.bgeneralbusiness.service.net.callback.IBNARequestCallBack
import com.yidian.xarc.xrouter.ResultResolver

object BNARemoteDataSource {

  /**
   * 发起get请求
   * 加密接口
   */
  fun launchEncryptGetRequest(
    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)
        }
      })
  }

  /**
   * 发起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)
    ServiceFactory.getInstance().createService(BNAService::class.java)
      .mbGetRequest(url, publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultObserver<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.getRequestSuccess(result, resolver)
        }

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

  /**
   * 发起post请求
   * 加密接口
   */
  fun launchEncryptPostRequest(
    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)
        }
      })
  }

  /**
   * 发起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)
    ServiceFactory.getInstance().createService(BNAService::class.java)
      .mbPostRequest(url, publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultObserver<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.getRequestSuccess(result, resolver)
        }

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