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

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ys.bdtp.adm.mvvm.model.FailureResponse
import com.ys.bdtp.adm.mvvm.model.SuccessResponse
import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
import com.ys.bdtp.adm.mvvm.model.vo.request.Account
import kotlinx.coroutines.launch

/**
 * FlashViewModel
 *
 * @author bai
 *
 * @param repo [FlashRepository]
 */
class FlashViewModel(private val repo: FlashRepository) : ViewModel() {

    /**
     * 获取当前登录用户
     */
    suspend fun getCurrentUser(): UserEntity? {
        return repo.getCurrentUser()
    }

    /**
     * 获取账号信息
     *  1,用户信息
     *  2,项目信息
     *
     * @param success 获取成功回调
     * @param failure 获取失败回调
     */
    suspend fun getAccount(success: (Account) -> Unit, failure: (String) -> Unit) {
        viewModelScope.launch {
            when (val response = repo.getAccount()) {
                is SuccessResponse -> {
                    success(response.data)
                }
                is FailureResponse -> {
                    failure(response.message)
                }
            }
        }
    }
}