package es.verdnatura.presentation.view.feature.parking.fragment

import android.content.Context
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetUbicadorUserCase
import es.verdnatura.presentation.base.BaseViewModel

import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.ResponseItemVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class ParkingSaleViewModel(context: Context) : BaseViewModel() {
    private val getUbicadorUserCase: GetUbicadorUserCase = GetUbicadorUserCase(context)

    private val _response by lazy { MutableLiveData<ResponseItemVO>() }
    val response: LiveData<ResponseItemVO>
        get() = _response

    private val _isLoading by lazy { MutableLiveData<Boolean>() }
    val isLoading: LiveData<Boolean> = _isLoading

    private val _isLoadingProgress by lazy { MutableLiveData<Int>() }
    val isLoadingProgress: LiveData<Int> = _isLoadingProgress


    private val _responseParkingAdd by lazy { MutableLiveData<ResponseItemVO>() }
    val responseParkingAdd: LiveData<ResponseItemVO>
        get() = _responseParkingAdd


    private fun showProgressLoading() {
        _isLoading.value = true
    }

    fun hideProgressLoading() {
        _isLoading.value = false
    }
    fun ProgressLoading(visibility:Int) {
        _isLoadingProgress.value = visibility
    }

    fun parking(usuario: String,password: String,scanItem: String,parking:String){
        ProgressLoading(VISIBLE)
        getUbicadorUserCase.shelvingPark(usuario,password,scanItem,parking).enqueue(object :
            Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _response.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!))
            }

            override fun onResponse(call: Call<Any>, response: Response<Any>) {
                ProgressLoading(GONE)
                if (!response.isSuccessful){
                    _response.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))
                }else{
                        _response.value = ResponseItemVO(isError = false,response = response.message())
                }
            }
        })
    }


}
