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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetQualityUserCase
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.view.feature.calidad.model.BuyerListVO
import es.verdnatura.presentation.view.feature.calidad.model.BuyerVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class BuyersViewModel(context: Context) : BaseViewModel() {

    private val getQualityUserCase: GetQualityUserCase = GetQualityUserCase(context)

    private val _buyersList by lazy { MutableLiveData<BuyerListVO>() }
    val buyersList: LiveData<BuyerListVO>
        get() = _buyersList

    fun itemShelvingBuyerGet(user:String,password:String){
        getQualityUserCase.itemShelvingBuyerGet(user,password).enqueue(object :
            Callback<List<BuyerVO>> {
            override fun onFailure(call: Call<List<BuyerVO>>, t: Throwable) {
                val listError:ArrayList<BuyerVO> = ArrayList()
                listError.add(BuyerVO(isError = true,errorMessage =getMessageFromAllResponse(nameofFunction(this),t.message!!)))
                _buyersList.value = BuyerListVO(listError)
            }

            override fun onResponse(
                call: Call<List<BuyerVO>>,
                response: Response<List<BuyerVO>>
            ) {
                if (response.body() != null){
                    _buyersList.value = response.body()?.let { BuyerListVO(it) }
                }else{
                    val listError:ArrayList<BuyerVO> = ArrayList()
                    listError.add(BuyerVO(isError = true,errorMessage =getMessageFromAllResponse(nameofFunction(this),response.message())))
                    _buyersList.value = BuyerListVO(listError)
                }
            }

        })
    }

}
