package es.verdnatura.presentation.view.feature.modelViewModel
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.SalixCallback
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.articulo.model.ItemProposal
import es.verdnatura.presentation.view.feature.articulo.model.ItemProposalList
import retrofit2.Response

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


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


    fun callBackFuncSalix(routine: String, params: ArrayList<Any>) {
        salix.executeFunc(
            routine = routine, params = params
        ).enqueue(object :
            SalixCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseCallBack.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse((routine), t.message!!),
                    routine = routine
                )
            }

            override fun onSuccess(response: Response<Any>) {

                _responseCallBack.value = ResponseItemVO(
                    isError = false,
                    response = response.body()!!.toString(),
                    errorMessage = "",
                    routine = routine

                )

            }

        })
    }
    fun callBackProcedSalix(routine: String, params: ArrayList<Any>) {
        salix.executeProc(
            routine = routine, params = params
        ).enqueue(object :
            SalixCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseCallBack.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse((routine), t.message!!),
                    routine = routine
                )
            }

            override fun onSuccess(response: Response<Any>) {

                _responseCallBack.value = ResponseItemVO(
                    isError = false,
                    response = if (response.body()!=null){response.body()!!.toString()}else{""},
                    errorMessage = "",
                    routine = routine,
                    data = response.body()?.let {
                        ItemProposalList(it as List<ItemProposal>)
                    }
                )

            }

        })
    }
}
