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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.JsonObject
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.presentation.base.BaseViewModel
import retrofit2.Response

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

    private val _itemBufferType by lazy { MutableLiveData<Boolean>() }
    val itemBufferType: LiveData<Boolean>
        get() = _itemBufferType

    private val _bufferLoadResponse by lazy { MutableLiveData<String>() }
    val bufferLoadResponse: LiveData<String>
        get() = _bufferLoadResponse

    fun bufferSetTypeByName(vBufferFk: Number, vType: String) {
        salix.bufferSetTypeByName(params = listOf(vBufferFk, vType).formatWithQuotes())
            .enqueue(object : SalixCallback<List<JsonObject>>(context) {
                override fun onSuccess(response: Response<List<JsonObject>>) {

                    _itemBufferType.value =
                        response.body()?.get(0)?.entrySet()?.first()?.value.toString() == "1"
                    if (_itemBufferType.value == true) {
                        super.onSuccess(response)
                    }

                }
            })
    }

    fun expeditionLoadingAdd(vExpeditionFk: Int, vBufferFk: Int) {
        salix.expeditionLoadingAdd(
            arrayListOf(vExpeditionFk, vBufferFk),
            schema = "srt"
        )
            .enqueue(object : SalixCallback<List<Map<String, Any>>>(context) {
                override fun onSuccess(response: Response<List<Map<String, Any>>>) {
                    _bufferLoadResponse.value = response.body()!![0]["vMessage"].toString()
                }

            })
    }
}



