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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import com.google.gson.JsonObject
import es.verdnatura.domain.SalixCallback
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.ShelvingLogSalix
import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.ShelvingLogSalixList
import retrofit2.Response

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

    private val _itemShelvingLogSalixList by lazy { MutableLiveData<ShelvingLogSalixList>() }
    val loadShelvingLogSalixList: LiveData<Event<ShelvingLogSalixList>> =
        _itemShelvingLogSalixList.map { Event(it) }

    private val _responseParkingId by lazy { MutableLiveData<Int?>() }
    val responseParkingId: LiveData<Int?> = _responseParkingId
    val loadResponseParking: LiveData<Event<Int?>> =_responseParkingId.map { Event(it) }

    fun shelvings(vShelvingFK: String) {
        salix.shelvingsGet("""{"where": {"code": "$vShelvingFK"}}""").enqueue(object :
            SalixCallback<List<ShelvingLogSalix>>(context) {
            override fun onSuccess(response: Response<List<ShelvingLogSalix>>) {
                _itemShelvingLogSalixList.value = response.body()?.let {
                    ShelvingLogSalixList(it)
                }

            }

        })

    }

    fun getParkingId(parkingCode: String) {
        salix.getParkings("""{"where": {"code": "$parkingCode"},"fields":"id"}""")
            .enqueue(object :
                SalixCallback<List<JsonObject>>(context) {
                override fun onSuccess(response: Response<List<JsonObject>>) {
                    _responseParkingId.value = response.body()?.firstOrNull()?.entrySet()?.firstOrNull()?.value?.toString()?.toIntOrNull()

                }

            })

    }
}








