package com.yidian.shenghuoquan.bgoods.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.bgoods.dto.CategoryListDto
import com.yidian.shenghuoquan.bgoods.dto.CommodityManagementListDto
import com.yidian.shenghuoquan.bgoods.dto.CommodityStatus
import com.yidian.shenghuoquan.bgoods.http.callback.ICategoryListCallback
import com.yidian.shenghuoquan.bgoods.http.callback.ICommodityOperationCallback
import com.yidian.shenghuoquan.bgoods.http.callback.IGetCommodityManagementListCallback
import com.yidian.shenghuoquan.bgoods.http.callback.IGetCommodityStatusListCallback

/**
 * 优惠券数据源
 */
object CouponDataSource {

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

  /**
   * 获取类目列表
   */
  fun getCategoryList(apiCallback: ICategoryListCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api
      .getCategoryList(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<MutableList<CategoryListDto>?>() {
        override fun onSuccess(result: HttpResult<MutableList<CategoryListDto>?>?) {
          apiCallback.getCategoryListSuccess(result?.result)
        }

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

  /**
   * 获取商品管理列表
   */
  fun getCommodityManagementList(apiCallback: IGetCommodityManagementListCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api.getCommodityManagementList(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<CommodityManagementListDto>() {
        override fun onSuccess(result: HttpResult<CommodityManagementListDto>?) {
          apiCallback.getCommodityListSuccess(result?.result)
        }

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

  /**
   * 获取商品状态列表
   */
  fun getCommodityStatusList(apiCallback: IGetCommodityStatusListCallback) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(hashMapOf(), timeStamp)
    api.getCommodityStatusList(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<ArrayList<CommodityStatus>>() {
        override fun onSuccess(result: HttpResult<ArrayList<CommodityStatus>>?) {
          apiCallback.getCommodityStatusListSuccess(result?.result)
        }

        override fun onFailure(result: HttpResult<ArrayList<CommodityStatus>>?) {
          apiCallback.getCommodityStatusListFailure(result?.reason)
        }
      })
  }

  /**
   * 商品上架
   */
  fun commodityOnline(apiCallback: ICommodityOperationCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api.commodityOnline(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any>() {
        override fun onSuccess(result: HttpResult<Any>?) {
          apiCallback.commodityOnlineSuccess()
        }

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

  /**
   * 商品下架
   */
  fun commodityOffline(apiCallback: ICommodityOperationCallback, requestParams: HashMap<String, String?>) {
    val timeStamp = System.currentTimeMillis()
    val publicParamsMap = HttpParamsUtils.getPublicParamsMap(timeStamp)
    val privateParamsMap = HttpParamsUtils.getPrivateParamsMap(requestParams, timeStamp)
    api.commodityOffline(publicParamsMap, privateParamsMap)
      .compose(TransformUtil.defaultSchedulers())
      .subscribe(object : HttpResultSubscriber<Any>() {
        override fun onSuccess(result: HttpResult<Any>?) {
          apiCallback.commodityOfflineSuccess()
        }

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