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.R
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.ResponseItemVO
import retrofit2.Response

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

    private val _itembuffertype by lazy { MutableLiveData<ResponseItemVO>() }
    val itembuffertype: LiveData<ResponseItemVO>
        get() = _itembuffertype

    private val _bufferloadresponse by lazy { MutableLiveData<ResponseItemVO>() }
    val bufferloadresponse: LiveData<ResponseItemVO>
        get() = _bufferloadresponse

    fun bufferSetTypeByName(vBufferFk: Int, vType: String) {
        salix.buffer_setTypeByName(params = listOf(vBufferFk, vType).formatWithQuotes())
            .enqueue(object : SalixCallback<List<JsonObject>>(context) {
                override fun onSuccess(response: Response<List<JsonObject>>) {
                    var responseCall = response.body()!![0].entrySet().first().value.toString()
                    _itembuffertype.value = ResponseItemVO(
                        isError = responseCall != "1",
                        errorMessage = if (responseCall == "0") {
                            context.getString(R.string.errorOperation)
                        } else {
                            ""
                        }

                    )
                    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 =
                        ResponseItemVO(
                            isError = false,
                            response = response.body()!![0]["vMessage"].toString()
                        )
                }

            })
    }
}



