package com.yidian.shenghuoquan.commodity.ui.coupon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.orhanobut.hawk.Hawk
import com.yidian.common.HawkConfig
import com.yidian.common.XBirdPageConstants
import com.yidian.common.XRouterPathConstants
import com.yidian.common.mvvm.BaseViewModel
import com.yidian.shenghuoquan.commodity.dto.Goods
import com.yidian.shenghuoquan.commodity.http.CouponDataSource
import com.yidian.shenghuoquan.commodity.http.callback.ICommodityOperationCallback
import com.yidian.xpage.XPageManager

/**
 * author: yinjiacheng
 * date: 7/3/21 2:18 PM
 * description: 商品
 */
class CommodityListItemViewModel : BaseViewModel(), ICommodityOperationCallback {

  companion object {
    // 商品spu_id
    private const val PARAMS_COMMODITY_ID = "goods_spu_id"

    // 生活号id
    private const val PARAMS_LIFE_ACCOUNT_ID = "life_account_id"

    // 传入H5商品id
    private const val EXTRA_COMMODITY_ID = "coupon_id"
  }

  // 商品上架操作事件
  val commodityOnlineOperationEvent by lazy { MutableLiveData<Goods>() }

  // 商品上架成功事件
  val commodityOnlineSuccessEvent by lazy { MutableLiveData<Unit>() }

  // 商品下架操作事件
  val commodityOfflineOperationEvent by lazy { MutableLiveData<Goods>() }

  // 商品下架成功事件
  val commodityOfflineSuccessEvent by lazy { MutableLiveData<Unit>() }

  // 商品编辑操作事件
  val commodityEditOperationEvent by lazy { MutableLiveData<Goods>() }

  /**
   * 商品上架
   */
  fun commodityOnline(item: Goods) {
    requestCommodityOnline(item.goodsSpuId)
  }

  /**
   * 商品下架
   */
  fun commodityOffline(item: Goods) {
    // 发送数据
    commodityOfflineOperationEvent.value = item
  }

  /**
   * 商品编辑
   */
  fun commodityEdit(item: Goods) {
    // 跳转商品发布页面 传入商品id
    val pageUrl = XBirdPageConstants.COMMODITY_PUBLISH_COMMODITY +
      "?life_account_id=${Hawk.get(HawkConfig.LifeAccountId, "")}" +
      "&coupon_id=${item.goodsSpuId}"
    XPageManager.push(XRouterPathConstants.COMMON_WEB_VIEW_ACTIVITY, hashMapOf(Pair(XBirdPageConstants.PAGE_URL, pageUrl)))
  }

  /**
   * 请求商品上架
   */
  fun requestCommodityOnline(commodityId: String) {
    CouponDataSource.commodityOnline(
      this,
      hashMapOf(Pair(PARAMS_COMMODITY_ID, commodityId), Pair(PARAMS_LIFE_ACCOUNT_ID, Hawk.get(HawkConfig.LifeAccountId, "")))
    )
  }

  /**
   * 请求商品下架
   */
  fun requestCommodityOffline(commodityId: String) {
    CouponDataSource.commodityOffline(
      this,
      hashMapOf(Pair(PARAMS_COMMODITY_ID, commodityId), Pair(PARAMS_LIFE_ACCOUNT_ID, Hawk.get(HawkConfig.LifeAccountId, "")))
    )
  }

  override fun commodityOnlineSuccess() {
    // 商品上架成功 发送事件
    commodityOnlineSuccessEvent.value = Unit
  }

  override fun commodityOnlineFailure(message: String?) {

  }

  override fun commodityOfflineSuccess() {
    // 商品下架成功 发送事件
    commodityOfflineSuccessEvent.value = Unit
  }

  override fun commodityOfflineFailure(message: String?) {

  }

}
