package com.yidian.shenghuoquan.buserorder.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.buserorder.dto.*
import com.yidian.shenghuoquan.buserorder.http.callback.IApplyRefundCallback
import com.yidian.shenghuoquan.buserorder.http.callback.IGetOrderDetailCallback
import com.yidian.shenghuoquan.buserorder.http.callback.IGetPickupPointListCallback
import org.json.JSONObject

/**
 * author: yinjiacheng
 * date: 2021/8/23 11:02
 * description: 订单组件数据源
 */
object OrderRemoteDataSource {

  private val api = ServiceFactory.getInstance().createService(OrderApiService::class.java)

  /**
   * 获取订单详情
   */
  fun getOrderDetail(apiCallback: IGetOrderDetailCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api.getOrderDetail(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<OrderDetailDto>() {
        override fun onSuccess(result: HttpResult<OrderDetailDto>?) {
          apiCallback.getOrderDetailSuccess(result?.result)
        }

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

  /**
   * 申请退款
   */
  fun applyRefund(apiCallback: IApplyRefundCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api.applyRefund(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any>() {
        override fun onSuccess(result: HttpResult<Any>?) {
          apiCallback.applyRefundSuccess()
        }

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

  /**
   * 获取确认订单
   */
  fun requestOrderConfirm(requestParams: JSONObject, callback: ((OrderConfirmResDto?) -> Unit)? = null) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    ServiceFactory.getInstance().createService(OrderApiService::class.java)
      .requestOrderConfirm(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<OrderConfirmResDto?>() {
        override fun onSuccess(result: HttpResult<OrderConfirmResDto?>?) {
          callback?.invoke(result?.result)
        }

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

        }
      })
  }

  /**
   * 创建订单
   */
  fun requestCreateOrder(requestParams: JSONObject, callback: ((CreateOrderResDto?) -> Unit)? = null) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    ServiceFactory.getInstance().createService(OrderApiService::class.java)
      .requestCreateOrder(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<CreateOrderResDto?>() {
        override fun onSuccess(result: HttpResult<CreateOrderResDto?>?) {
          callback?.invoke(result?.result)
        }

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

        }
      })
  }

  /**
   * 支付订单
   */
  fun requestPayOrder(requestParams: HashMap<String, String?>, callback: ((List<PingPayResult>?) -> Unit)? = null) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    ServiceFactory.getInstance().createService(OrderApiService::class.java)
      .requestOrderPay(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<List<PingPayResult>?>() {
        override fun onSuccess(result: HttpResult<List<PingPayResult>?>?) {
          callback?.invoke(result?.result)
        }

        override fun onFailure(result: HttpResult<List<PingPayResult>?>?) {

        }
      })
  }

  /**
   * 获取活动设置的自提点列表
   */
  fun getPickupPointList(callback: IGetPickupPointListCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    ServiceFactory.getInstance().createService(OrderApiService::class.java)
      .getPickupPointList(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<PickupPointListDto>() {
        override fun onSuccess(result: HttpResult<PickupPointListDto>?) {
          callback.getPickupPointListSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<PickupPointListDto>?) {
          callback.getPickupPointListFailure(result?.reason)
        }
      })
  }
}
