package com.example.shawarma.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.shawarma.data.api.repos.RestUserRepository
import com.example.shawarma.data.models.UserModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class UserViewModel @Inject constructor(
    private val restUserRepository: RestUserRepository
) : ViewModel() {
    private val _token = MutableLiveData<String?>()
    val token: LiveData<String?>
        get() = _token

    private val _authorizationState = MutableLiveData<Boolean?>()
    val authorizationState: LiveData<Boolean?>
        get() = _authorizationState

    fun login(login: String, password: String){
        viewModelScope.launch {
            withContext(Dispatchers.Main) {
                val token_response = restUserRepository.getToken(UserModel(id = null, login = login, password = password, role = ""))
                if (token_response.access_token.isNotEmpty()) {
                    _token.postValue(token_response.access_token)
                    _authorizationState.postValue(true)
                    println(token_response.access_token)
                }
                else {
                    _token.postValue(null)
                    _authorizationState.postValue(false)
                }
            }
        }
    }
    fun calmAuthorizationState() {
        _authorizationState.postValue(null)
    }

    private val _registrationState = MutableLiveData<Boolean>()
    val registrationState: LiveData<Boolean>
        get() = _registrationState

    fun register(login: String, password: String, passwordRepeat: String) {
        _registrationState.postValue(null)
        if (password != passwordRepeat) {
            _registrationState.postValue(false)
            return
        }
        if (login.length < 5 || login.length > 20) {
            _registrationState.postValue(false)
            return
        }
        if (password.length < 5 || password.length > 20) {
            _registrationState.postValue(false)
            return
        }
        viewModelScope.launch {
            withContext(Dispatchers.Main) {
                val user_response = restUserRepository.checkLogin(UserModel(null, login, password, ""))
                if (user_response != null) {
                    if (user_response.login.isNullOrEmpty()) {
                        if (login == "admin" && password == "admin") {
                            restUserRepository.insert(UserModel(null, login, password, "ADMIN"))
                            _registrationState.postValue(true)
                        } else {
                            restUserRepository.insert(UserModel(null, login, password, "USER"))
                            _registrationState.postValue(true)
                        }
                    } else {
                        if (registrationState.value != true) {
                            _registrationState.postValue(false)
                        }
                    }
                }
            }
        }
    }
    fun calmRegistrationState() {
        _registrationState.postValue(null)
    }

}