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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetPaletizadoresUserCase
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 CmrExpeditionPalletViewModel(context: Context) : BaseViewModel() {

        private val GetPaletizadoresUserCase: GetPaletizadoresUserCase =
            GetPaletizadoresUserCase(context)

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


        fun cmrExpeditionPallet_add(usuario: String, password: String, vExpeditionPallet: String,vCmrId:String) {
            GetPaletizadoresUserCase.cmrExpeditionPallet_add(usuario, password,  vExpeditionPallet, vCmrId)
                .enqueue(object : Callback<Void> {
                    override fun onFailure(call: Call<Void>, t: Throwable) {

                        _responsescan.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                        )
                    }

                    override fun onResponse(call: Call<Void>, response: Response<Void>) {

                        if (!response.isSuccessful) {
                            _responsescan.value = ResponseItemVO(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this),
                                    response.message()
                                )
                            )
                        } else {
                            _responsescan.value =
                                ResponseItemVO(isError = false, response = response.message()!!)
                        }

                    }

                })
        }


    }