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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
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.ResponseItemVO
import retrofit2.Response

class BufferFragmentViewModel(val context: Context) : BaseViewModel(context) {
    /* private val GetPaletizadoresUserCase: GetPaletizadoresUserCase =
         GetPaletizadoresUserCase(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 buffer_setTypeByName(vBufferFk: Int, vType: String) {
        silex.buffer_setTypeByName(vBufferFk, vType)
            .enqueue(object : SilexCallback<Boolean>(context) {
                override fun onError(t: Throwable) {
                    _itembuffertype.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Boolean>) {
                    if (response.body() == true ) {

                        _itembuffertype.value = ResponseItemVO(
                            isError = false,
                            errorMessage = ""
                        )

                    } else {
                        _itembuffertype.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    }
                }

            })
    }

    fun expeditionLoading_add(vExpeditionFk: Int, vBufferFk: Int) {
        silex.expeditionLoading_add(vExpeditionFk, vBufferFk)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _bufferloadresponse.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _bufferloadresponse.value = ResponseItemVO(
                        isError = false,
                        response = response.body().toString()!!
                    )

                }


            })
    }
}



