package es.verdnatura.presentation.view.feature.historicoarticulo.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.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.view.feature.historicoarticulo.model.ItemHistoricoListVO
import es.verdnatura.presentation.view.feature.historicoarticulo.model.ItemHistoricoVO
import retrofit2.Response

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

    private val _historicoList by lazy { MutableLiveData<ItemHistoricoListVO>() }
    val historicoList: LiveData<ItemHistoricoListVO>
        get() = _historicoList

    val loadHistoricoList: LiveData<Event<ItemHistoricoListVO>> = _historicoList.map { Event(it) }

    fun itemGetBalance(itemFk: Int, vWarehouseFk: Int) {

        salix.itemGetBalance(arrayListOf(itemFk, vWarehouseFk, null))
        .enqueue(object :
            SalixCallback<List<ItemHistoricoVO>>(context) {
            override fun onSuccess(response: Response<List<ItemHistoricoVO>>) {
                _historicoList.value = response.body()?.let { ItemHistoricoListVO(it) }
            }

        })
    }
}
