package com.yidian.shenghuoquan.bmanagercenter.personnel.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.bmanagercenter.personnel.bean.AccountListDto
import com.yidian.shenghuoquan.bmanagercenter.personnel.http.callback.*

/**
 * 人员管理远程数据源
 */
object PersonalRemoteDataSource {

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

  /**
   * 获取账号列表
   */
  fun getAccountList(apiCallback: IPersonalAccountListCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api
      .getAccountList(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<AccountListDto?>() {
        override fun onSuccess(result: HttpResult<AccountListDto?>?) {
          apiCallback.getAccountListSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<AccountListDto?>?) {
          apiCallback.getAccountListFailure(result?.reason)
        }
      })
  }

  /**
   * 校验短信验证码
   */
  fun getCheckSmsCode(apiCallback: IPersonalCheckSmsCodeCallBack, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api
      .getCheckSmsCode(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.checkSmsCodeSuccess()
        }

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

  /**
   * 更换管理员
   */
  fun getChangeManager(apiCallback: IPersonalChangeAccountCallBack, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api
      .getChangeManager(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.changeAccountSuccess()
        }

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

  /**
   * 添加非管理员
   */
  fun postAddUser(apiCallback: IPersonalAddUserCallBack, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api
      .postAddUser(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.addUserSuccess()
        }

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

  /**
   * 解除关系
   */
  fun getDissolveRelationship(apiCallback: IPersonalDissolveCallBack, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api
      .getDissolveRelationship(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          apiCallback.dissolveSuccess()
        }

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