package com.yidian.shenghuoquan.bsetting.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.http.ServiceFactory
import com.yidian.shenghuoquan.bsetting.bean.MobileLoginBean

object SettingRemoteDataSource {

  private var api = ServiceFactory.getInstance().createService(SettingApiService::class.java)

  /**
   * 手机号登录
   */
  fun getMobileLogin(requestParams: HashMap<String, String?>, callback: ((MobileLoginBean.Response?) -> Unit)? = null) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api
      .mobileLogin(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<MobileLoginBean.Response?>() {
        override fun onSuccess(result: HttpResult<MobileLoginBean.Response?>?) {
          callback?.invoke(result?.result)
        }

        override fun onFailure(result: HttpResult<MobileLoginBean.Response?>?) {
        }
      })
  }

  /**
   * 用户退出登录接口
   */
  fun userLogout(callback: ((Boolean) -> Unit)? = null) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(HashMap(), timeStamp)
    api.userLogout(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          callback?.invoke(true)
        }

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

}
