package com.yidian.shenghuoquan.commodity.http

import com.yidian.common.http.HttpParamsUtils
import com.yidian.common.http.HttpResult
import com.yidian.common.http.HttpResultSubscriber
import com.yidian.common.http.TransformUtil
import com.yidian.http.ServiceFactory
import com.yidian.shenghuoquan.commodity.dto.CategoryListDto
import com.yidian.shenghuoquan.commodity.dto.CommodityManagementListDto
import com.yidian.shenghuoquan.commodity.dto.CommodityStatus
import com.yidian.shenghuoquan.commodity.http.callback.ICategoryListCallback
import com.yidian.shenghuoquan.commodity.http.callback.ICommodityOperationCallback
import com.yidian.shenghuoquan.commodity.http.callback.IGetCommodityManagementListCallback
import com.yidian.shenghuoquan.commodity.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 onFailer(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 onFailer(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 onFailer(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 onFailer(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 onFailer(result: HttpResult<Any>?) {
          apiCallback.commodityOfflineFailure(result?.reason)
        }
      })
  }
}
