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


import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
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.Event
import es.verdnatura.presentation.view.feature.historicoshelving.model.ItemShelvingLog
import es.verdnatura.presentation.view.feature.historicoshelving.model.ItemShelvingLogList
import retrofit2.Response

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

    private val _itemShelvingLogList by lazy { MutableLiveData<ItemShelvingLogList>() }
    val itemShelvingLogList: LiveData<ItemShelvingLogList>
    get() = _itemShelvingLogList


    val loadItemShelvingLogList = Transformations.map(_itemShelvingLogList) { Event(it) }


    fun itemShelvingLog_get(vShelvingFK: String) {
        silex.itemShelvingLog_get(vShelvingFK).enqueue(object :
            SilexCallback<List<ItemShelvingLog>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<ItemShelvingLog> = ArrayList()
                listError.add(
                    ItemShelvingLog(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _itemShelvingLogList.value = ItemShelvingLogList(listError)
            }

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

                if (response.body() != null) {
                    _itemShelvingLogList.value = response.body()?.let {
                        ItemShelvingLogList(it)
                    }

                }

            }

        })
    }


}








