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

import android.content.Context
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 es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO
import retrofit2.Response


class PalletScanViewModel(val context: Context) : BaseViewModel(context) {


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

    private val _responsePalletGet by lazy { MutableLiveData<ItemExpeditionTruckVO>() }
    val responsePalletGet: LiveData<ItemExpeditionTruckVO>
        get() = _responsePalletGet


    fun expeditionState_addByPallet(
        vPalletId: Int,
        vStatePallet: String
    ) {
        silex.expeditionState_addByPallet(
            vPalletId,
            vStatePallet
        )
            .enqueue(object : SilexCallback<Unit>(context) {
                override fun onError(t: Throwable) {

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

                override fun onSuccess(response: Response<Unit>) {

                    _responseScan.value =
                        ResponseItemVO(isError = false, response = response.message()!!)
                }

            })
    }

    fun expeditionPallet_get(vPalletId: Int) {
        silex.expeditionPallet_get(vPalletId)
            .enqueue(object : SilexCallback<ItemExpeditionTruckVO>(context) {
                override fun onError(t: Throwable) {

                    _responsePalletGet.value = ItemExpeditionTruckVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<ItemExpeditionTruckVO>) {

                    _responsePalletGet.value =
                        response.body()!!

                }

            })
    }

}