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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpedition
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckSalix
import retrofit2.Response

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

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

    fun expeditionStateAddByPallet(
        vPalletId: Int,
        vStatePallet: String
    ) {
        salix.expeditionStateAddByPallet(
            arrayListOf(
                vPalletId,
                vStatePallet
            ).formatWithQuotes()
        )
            .enqueue(object : SalixCallback<Unit>(context) {

            })
    }

    fun expeditionPalletGet(vPalletId: Int) {
        salix.expeditionPalletGet("""{"fields": ["truckFk"], "where": {"id": $vPalletId}, "include": [{"relation": "roadmapStop", "scope": {"fields": ["eta", "description"]}}]}""")
            .enqueue(object : SalixCallback<List<ItemExpeditionTruckSalix>>(context) {

                override fun onSuccess(response: Response<List<ItemExpeditionTruckSalix>>) {

                    if (response.body()!!.isNotEmpty()) {
                        _responsePalletGet.value =
                            response.body()?.get(0)
                    } else {
                        _responsePalletGet.value = ItemExpeditionTruckSalix(0, ItemExpedition(0))
                    }
                }

            })
    }

}