package com.fastest.pass.account.presentation.viewmodels

import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fastest.pass.account.domain.usecase.AccountUseCase
import com.fastest.pass.account.utils.AccountRoute
import com.fastest.pass.app.Result
import com.fastest.pass.app.UIState
import com.fastest.pass.app.WebResponse
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import java.util.Objects
import javax.inject.Inject

@HiltViewModel
class AccountViewModel @Inject constructor(
    private val accountUseCase: AccountUseCase
) : ViewModel() {

    private val _router = MutableStateFlow<AccountRoute>(AccountRoute.OpenNoneScreen)
    val router: MutableStateFlow<AccountRoute> = _router

    private var _logoutResponse = mutableStateOf(UIState<WebResponse<Objects>>())
    val logoutResponse: State<UIState<WebResponse<Objects>>> = _logoutResponse

    fun logout() {
        viewModelScope.launch {
            Log.d("logout_api", "logout()")
            accountUseCase("") { result ->
                Log.d("logout_api", "result = $result")
                when (result) {
                    is Result.Loading -> {
                        _logoutResponse.value = UIState(true)
                    }
                    is Result.Success -> {
                        _logoutResponse.value = UIState(false, result.data)
                    }
                    is Result.Failure -> {
                        _logoutResponse.value = UIState(false)
                    }
                }
            }
        }
    }

    fun navigateTo(accountRoute: AccountRoute) {
        _router.value = accountRoute
    }
}