package com.yidian.shenghuoquan.bsetting.http

import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.http.*
import com.yidian.bcommon.http.bean.CommitFeedbackBean
import com.yidian.bcommon.http.callback.ICommitFeedbackCallback
import com.yidian.http.ServiceFactory
import com.yidian.shenghuoquan.bsetting.bean.MobileLoginBean

object SettingRemoteDataSource {

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

  private val jiraApi by lazy {
    ServiceFactory.getInstance().createService(SettingApiService::class.java, AppConfig.JiraDomain)
  }

  /**
   * 手机号登录
   */
  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?>?) {
        }
      })
  }

  /**
   * 用户反馈提交
   */
  fun commitFeedback(apiCallback: ICommitFeedbackCallback, requestParams: CommitFeedbackBean.CommitFeedbackRequest) {
    jiraApi
      .commitFeedback(requestParams)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultObserver<CommitFeedbackBean.CommitFeedbackResponse>() {
        override fun onSuccess(result: HttpResult<CommitFeedbackBean.CommitFeedbackResponse>?) {
          apiCallback.commitFeedbackSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<CommitFeedbackBean.CommitFeedbackResponse>?) {
          apiCallback.commitFeedbackFailure(result?.reason)
        }

      })
  }

}
