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.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.calidad.model.ItemBuyerListVO
import es.verdnatura.presentation.view.feature.calidad.model.ItemBuyerVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

    private val getQualityUserCase: GetQualityUserCase = GetQualityUserCase(context)

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


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


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

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

        })
    }

    fun itemShelvingStarsUpdate(usuario:String,password:String, vId: String,vStars: String) {
        getQualityUserCase.itemShelvingStarsUpdate(usuario,password,vId,vStars).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())
                }
            }
        })
    }
}