package com.yidian.common.services.http

import com.yidian.common.http.HttpParamsUtils
import com.yidian.common.http.HttpResult
import com.yidian.common.http.HttpResultSubscriber
import com.yidian.common.http.TransformUtil
import com.yidian.common.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 onFailer(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 onFailer(result: HttpResult<Any?>?) {
          apiCallback.getRequestFailure(result)
        }
      })
  }
}
