package com.ys.bdtp.adm.mvvm.vm.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ys.bdtp.adm.mvvm.model.nt.Fail
import com.ys.bdtp.adm.mvvm.model.nt.FailureResponse
import com.ys.bdtp.adm.mvvm.model.nt.SuccessResponse
import com.ys.bdtp.adm.mvvm.model.vo.request.Login
import kotlinx.coroutines.launch

/**
 * LoginViewModel
 *
 * @author bai
 */
class LoginViewModel(private val repo: LoginRepository) : ViewModel() {
    /**
     * 登录
     */
    fun login(
        login: Login,
        success: (String) -> Unit,
        failure: (Fail) -> Unit,
    ) {
        viewModelScope.launch {
            when (val response = repo.login(login)) {
                is SuccessResponse -> {
                    success(response.data)
                }
                is FailureResponse -> {
                    failure(response.fail)
                }
            }
        }
    }


    /**
     * 获取账号信息
     */
    fun requestAccount(password: String, success: () -> Unit, failure: (Fail) -> Unit) {
        viewModelScope.launch {
            repo.requestAccount(password, success, failure)
        }
    }
}
