package com.yidian.bcommon.mvvm

import androidx.annotation.IdRes
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

abstract class BaseViewModel : ViewModel() {

  /**
   * 页面回退 Event
   */
  private val _goBackEvent = MutableLiveData<Unit>()
  val goBackEvent: LiveData<Unit> = _goBackEvent
  fun postBackEvent() {
    _goBackEvent.value = Unit
  }

  /**
   * 页面前进 Event
   */
  private val _goNextEvent = MutableLiveData<Any?>()
  val goNextEvent: LiveData<Any?> = _goNextEvent

  /**
   * 重置前进，每次页面跳转后应立即调用
   */
  fun resetNextEvent() {
    _goNextEvent.value = null
  }

  /**
   * 跳转到指定的导航目的地
   */
  fun postNextEvent() {
    _goNextEvent.value = Unit
  }

  /**
   * 跳转到指定的导航目的地
   */
  fun postNextEvent(@IdRes navId: Int) {
    _goNextEvent.value = navId
  }

  /**
   * 跳转到指定的路由
   */
  fun postNextEvent(routerName: String) {
    _goNextEvent.value = routerName
  }

  /**
   * 自定义跳转，可接收多个参数
   */
  fun postNextEvent(list: List<Any?>) {
    _goNextEvent.value = list
  }

  /**
   * 错误信息 Event
   */
  private val _errorEvent = MutableLiveData<ErrorInfo>()
  val errorEvent: LiveData<ErrorInfo> = _errorEvent
  fun postErrorEvent(errorInfo: ErrorInfo) {
    _errorEvent.value = errorInfo
  }

  /**
   * 加载动画 Event
   */
  private val _loadingEvent = MutableLiveData(false)
  val loadingEvent: LiveData<Boolean> = _loadingEvent
  fun postLoadingEvent(isLoading: Boolean = true) {
    _loadingEvent.value = isLoading
  }

}
