package com.yidian.shenghuoquan.bgeneralbusiness.http

import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.constant.RunConfig
import com.yidian.bcommon.http.*
import com.yidian.bcommon.http.bean.GetCommonConfigBean
import com.yidian.bcommon.http.callback.IGetCommonConfigCallback
import com.yidian.http.ServiceFactory

object GeneralBusinessRemoteDataService {

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

  private val towerApi by lazy {
    ServiceFactory.getInstance().createService(
      GeneralBusinessApiService::class.java, if (RunConfig.TOWER_API_ENV == AppConfig.TowerApiEnvTest) {
        AppConfig.TowerApiDomainTest
      } else {
        AppConfig.TowerApiDomainPro
      }
    )
  }

  // 获取短信验证码
  fun sendMsgCode(requestParams: HashMap<String, String?>, callback: ((Boolean) -> Unit)? = null) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api.sendMsgCode(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any?>() {
        override fun onSuccess(result: HttpResult<Any?>?) {
          callback?.invoke(true)
        }

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

  // TowerPro 获取配置数据
  fun getCommonConfig(apiCallback: IGetCommonConfigCallback, requestParams: HashMap<String, String>) {
    towerApi
      .getCommonConfig(requestParams)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultObserver<GetCommonConfigBean.GetCommonConfigResponse>() {
        override fun onSuccess(result: HttpResult<GetCommonConfigBean.GetCommonConfigResponse>?) {
          apiCallback.getCommonConfigSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<GetCommonConfigBean.GetCommonConfigResponse>?) {
          apiCallback.getCommonConfigFailure(result?.status)
        }

        override fun onNetworkFailure(result: HttpResult<GetCommonConfigBean.GetCommonConfigResponse>?) {
          apiCallback.getCommonConfigFailure(result?.status)
        }
      })
  }
}
