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


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

    private val GetPaletizadoresUserCase: GetPaletizadoresUserCase =
        GetPaletizadoresUserCase(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(
        usuario: String,
        password: String,
        vPalletId: String,
        vStatePallet: String
    ) {
        GetPaletizadoresUserCase.expeditionState_addByPallet(
            usuario,
            password,
            vPalletId,
            vStatePallet
        )
            .enqueue(object : Callback<Unit> {
                override fun onFailure(call: Call<Unit>, t: Throwable) {

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

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

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

                }

            })
    }

    fun expeditionPallet_get(usuario: String, password: String, vPalletId: String) {
        GetPaletizadoresUserCase.expeditionPallet_get(usuario, password, vPalletId)
            .enqueue(object : Callback<ItemExpeditionTruckVO> {
                override fun onFailure(call: Call<ItemExpeditionTruckVO>, t: Throwable) {

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

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

                    if (!response.isSuccessful) {
                        _responsePalletGet.value = ItemExpeditionTruckVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _responsePalletGet.value =
                            response.body()!!
                    }

                }

            })
    }

}