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.SilexCallback
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.Response

class ParkingViewModel(val context: Context) : BaseViewModel(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 fun showProgressLoading() {
        _isLoading.value = true
    }

    fun hideProgressLoading() {
        _isLoading.value = false
    }

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

    fun parking(scanItem: String, parking: String) {
        ProgressLoading(VISIBLE)
        silex.shelvingPark(scanItem, parking).enqueue(object :
            SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                ProgressLoading(GONE)
                _response.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onSuccess(response: Response<Any>) {
                ProgressLoading(GONE)
                _response.value = ResponseItemVO(isError = false, response = response.message())
            }
        })
    }


}
