package com.yidian.bcommon.services.http

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.bcommon.services.http.callback.IMBRequestCallBack
import com.yidian.http.ServiceFactory

object MBRemoteDataSource {

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

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

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

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