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

import android.content.Context
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 AutomaticAddItemViewModel (context: Context): BaseViewModel() {


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


    fun itemShelvingMake_multi(usuario:String,password:String,shelving:String,items:List<String>,warehouse:String){
        getUbicadorUserCase.itemShelvingMake_multi(usuario,password,shelving,items,warehouse).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>) {
                if (!response.isSuccessful){
                    _response.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()!!))
                }else{
                    _response.value = ResponseItemVO(isError = false,response = response.message()!!)
                }

            }
        })
    }

}
