package com.fastest.pass.forgotpassword.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.app.Result
import com.fastest.pass.app.UIState
import com.fastest.pass.app.WebResponse
import com.fastest.pass.forgotpassword.data.model.ForgotPasswordRequest
import com.fastest.pass.forgotpassword.domain.usecase.ForgotPasswordUseCase
import com.fastest.pass.forgotpassword.utils.ForgotPasswordRoute
import com.fastest.pass.login.utils.LoginRoute
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 ForgotPasswordViewmodel @Inject constructor(
    private val forgotPasswordUseCase: ForgotPasswordUseCase
): ViewModel() {

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

    private var _forgotPasswordResponse = mutableStateOf(UIState<WebResponse<Objects>>())
    val forgotPasswordResponse: State<UIState<WebResponse<Objects>>> = _forgotPasswordResponse

    fun forgotPassword(email: String) {
        viewModelScope.launch {
            val request = ForgotPasswordRequest(email)
            forgotPasswordUseCase(request) { result ->
                Log.d("forgotPasswordResponse", "result = $result")
                when (result) {
                    is Result.Loading -> {
                        _forgotPasswordResponse.value = UIState(true)
                    }
                    is Result.Success -> {
                        _forgotPasswordResponse.value = UIState(false, result.data)
                    }
                    is Result.Failure -> {
                        _forgotPasswordResponse.value = UIState(false)
                    }
                }
            }
        }
    }

    fun navigateTo(forgotPasswordRoute: ForgotPasswordRoute) {
        _router.value = forgotPasswordRoute
    }
}