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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.view.feature.historicovehiculo.model.ItemHistoricoVehiculo
import es.verdnatura.presentation.view.feature.historicovehiculo.model.ItemHistoricoVehiculoList
import retrofit2.Response

class HistoricoVehiculoViewModel(var context: Context) : BaseViewModel(context) {

    private val _historicovehiculoList by lazy { MutableLiveData<ItemHistoricoVehiculoList>() }
    val loadHistoricoVehiculoList: LiveData<Event<ItemHistoricoVehiculoList>> = _historicovehiculoList.map { Event(it) }

    fun machineWorkeGetHistorical(plateNumber: String, workerId:Int) {
        salix.machineWorkerGetHistorical(arrayListOf(plateNumber,workerId).formatWithQuotes())
            .enqueue(object :
                SalixCallback<List<ItemHistoricoVehiculo>>(context) {
                override fun onSuccess(response: Response<List<ItemHistoricoVehiculo>>) {
                    _historicovehiculoList.value =
                        response.body()?.let { ItemHistoricoVehiculoList(it) }
                }

            })
    }
}
