package com.yidian.shenghuoquan.buserorder.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.yidian.bcommon.mvvm.BaseViewModel
import com.yidian.shenghuoquan.buserorder.dto.PickupPointListDto
import com.yidian.shenghuoquan.buserorder.http.OrderRemoteDataSource
import com.yidian.shenghuoquan.buserorder.http.callback.IGetPickupPointListCallback

/**
 * author: yinjiacheng
 * date: 2021/8/24 14:03
 * description: 自提点选择
 */
class SelectPickupPointViewModel : BaseViewModel() {

  private val _pickupPointListSearch = MutableLiveData<PickupPointListDto?>()
  val pickupPointListSearch: LiveData<PickupPointListDto?> = _pickupPointListSearch

  private val _pickupPointListRefresh = MutableLiveData<PickupPointListDto?>()
  val pickupPointListRefresh: LiveData<PickupPointListDto?> = _pickupPointListRefresh

  /**
   * 请求自提点列表
   */
  fun requestPickupPointList(
    marketingId: String,
    longitude: Double?,
    latitude: Double?,
    keyWords: String?,
    type: LoadType,
  ) {
    val requestParams = HashMap<String, String?>(6)
    requestParams["marketing_id"] = marketingId
    longitude?.let { requestParams["longitude"] = it.toString() }
    latitude?.let { requestParams["latitude"] = it.toString() }
    if (keyWords?.isNotEmpty() == true) requestParams["keywords"] = keyWords
    OrderRemoteDataSource.getPickupPointList(
      object : IGetPickupPointListCallback {
        override fun getPickupPointListSuccess(result: PickupPointListDto?) {
          when (type) {
            LoadType.SEARCH -> _pickupPointListSearch.value = result
            LoadType.REFRESH -> _pickupPointListRefresh.value = result
          }
        }

        override fun getPickupPointListFailure(message: String?) {
          when (type) {
            LoadType.SEARCH -> _pickupPointListSearch.value = null
            LoadType.REFRESH -> _pickupPointListRefresh.value = null
          }
        }

      }, requestParams
    )
  }

  /**
   * 列表加载类型
   */
  enum class LoadType {
    // 搜索
    SEARCH,

    // 下拉刷新
    REFRESH,
  }

}
