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

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.yidian.common.mvvm.BaseViewModel
import com.yidian.shenghuoquan.commodity.dto.CategoryListDto
import com.yidian.shenghuoquan.commodity.dto.SelectCategoryDto
import com.yidian.shenghuoquan.commodity.dto.SubCategory
import com.yidian.shenghuoquan.commodity.http.CouponDataSource
import com.yidian.shenghuoquan.commodity.http.callback.ICategoryListCallback

/**
 * 选择类目ViewModel
 */
class ChooseCategoryViewModel : BaseViewModel() {

  // 根据上次选中的类目匹配成功，获取到一级类目及其子类目
  private lateinit var matchSuccessFirstCategory: CategoryListDto

  // 是否显示顶部的一级类目名称
  var showSelectFirstCategory = MutableLiveData(false)

  // 选择的一级类目名称
  var selectFirstCategoryName = MutableLiveData<String>()
  var selectFirstCategoryId = MutableLiveData<Long>()

  // 选择的二级类目名称
  var selectSecondCategoryName = MutableLiveData<String>()
  var selectSecondCategoryId = MutableLiveData<Long>()
  var selectedCategory = MutableLiveData<SelectCategoryDto?>(null)

  /**
   * 类目列表数据 Event
   */
  private val _addListEvent = MutableLiveData<MutableList<CategoryListDto>?>()
  val addListEvent: LiveData<MutableList<CategoryListDto>?> = _addListEvent

  /**
   * 在类目列表中匹配上次选择的类目 Event
   */
  private val _matchListEvent = MutableLiveData<MutableList<CategoryListDto>?>()
  val matchListEvent: LiveData<MutableList<CategoryListDto>?> = _matchListEvent

  /**
   * 切换显示一级分类 Event
   */
  private val _showFirstLevelListEvent = MutableLiveData<Unit>()
  val showFirstLevelListEvent: LiveData<Unit> = _showFirstLevelListEvent

  /**
   * 请求类目列表
   */
  fun requestCategoryList() {
    val paramsMap = HashMap<String, String?>()
    paramsMap["category_name"] = ""
    CouponDataSource.getCategoryList(requestCategoryListCallBack, paramsMap)
  }

  private val requestCategoryListCallBack = object : ICategoryListCallback {
    override fun getCategoryListSuccess(dto: MutableList<CategoryListDto>?) {
      if (selectedCategory.value == null) {
        // 没有上次选中数据
        _addListEvent.value = dto
      } else {
        // 在接口返回数据中匹配上次选中数据是否成功
        var matchSuccess = false
        dto?.forEachIndexed { _, categoryListDto ->
          if (categoryListDto.category_id == selectedCategory.value?.firstCategoryId) {
            categoryListDto.sub_list.forEach { sub ->
              if (sub.category_id == selectSecondCategoryId.value) {
                sub.isSelect = true
                // 匹配成功的一级类目
                matchSuccessFirstCategory = categoryListDto
                matchSuccess = true
              } else {
                sub.isSelect = false
              }
            }
          }
        }
        if (matchSuccess) {
          // 有上次选中数据且在接口中匹配成功
          _matchListEvent.value = dto
        } else {
          // 没有匹配成功上次选中数据
          _addListEvent.value = dto
        }
      }
    }
  }

  fun gotoSearch() {
    postNextEvent()
  }

  /**
   * 显示选择的一级类目
   */
  fun showSelectFirstLevelName(dto: CategoryListDto? = null) {
    if (dto == null) {
      showSelectFirstCategory.value = false
    } else {
      showSelectFirstCategory.value = true
      selectFirstCategoryName.value = dto.name
      selectFirstCategoryId.value = dto.category_id
    }
  }

  /**
   * 显示选择的二级类目
   */
  fun showSelectSecondLevelName(secondCategory: SubCategory): SelectCategoryDto {
    selectSecondCategoryId.value = secondCategory.category_id
    selectSecondCategoryName.value = secondCategory.name
    return SelectCategoryDto(
      selectFirstCategoryId.value!!,
      selectFirstCategoryName.value!!,
      selectSecondCategoryId.value!!,
      selectSecondCategoryName.value!!
    )
  }

  fun showFirstLevelList() {
    _showFirstLevelListEvent.value = Unit
  }

  /**
   * 设置上次选中的类目
   */
  fun setLastSelected(selectCategoryDto: SelectCategoryDto) {
    selectedCategory.value = selectCategoryDto
    selectSecondCategoryId.value = selectCategoryDto.secondCategoryId
    selectSecondCategoryName.value = selectCategoryDto.secondCategoryName
  }

  /**
   * 获取上次选中的二级列表，用于页面显示
   */
  fun getLastSelectSecondLevelList(): CategoryListDto? {
    if (::matchSuccessFirstCategory.isInitialized) {
      return matchSuccessFirstCategory
    }
    return null
  }
}
