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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetPaletizadoresUserCase
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.Call
import retrofit2.Callback
import retrofit2.Response

class BufferFragmentViewModel(context: Context) : BaseViewModel() {
    private val GetPaletizadoresUserCase: GetPaletizadoresUserCase =
        GetPaletizadoresUserCase(context)


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


    /*private val _bufferresponse by lazy { MutableLiveData<Boolean>() }
    val bufferresponse: LiveData<Boolean>
        get() = _bufferresponse*/

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



    fun buffer_setTypeByName(user: String, password: String, vBufferFk: String, vType: String){
        GetPaletizadoresUserCase.buffer_setTypeByName(user, password, vBufferFk, vType)
            .enqueue(object : Callback<Boolean> {
                override fun onFailure(call: Call<Boolean>, t: Throwable) {


                    _itembuffertype.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!))

                }

                override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                //sergio: espera respuesta en el body
                    if (response.body() == true) {

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

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

                    }

                }


            })
    }

    fun expeditionLoading_add(usuario:String,password:String,vExpeditionFk:String, vBufferFk:String){
        GetPaletizadoresUserCase.expeditionLoading_add(usuario,password,vExpeditionFk, vBufferFk).enqueue(object : Callback<String>{
            override fun onFailure(call: Call<String>, t: Throwable) {
                _bufferloadresponse.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!))
            }

            override fun onResponse(call: Call<String>, response: Response<String>) {

                if (!response.isSuccessful){
                    _bufferloadresponse.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()!!))
                }else {

                            if (response.body()!!.isNullOrEmpty()) {
                                _bufferloadresponse.value = ResponseItemVO(
                                    isError = true,
                                    errorMessage =
                                    getMessageFromAllResponse(nameofFunction(this),response.message()!!)
                                )
                            } else {
                                _bufferloadresponse.value = ResponseItemVO(
                                    isError = false,
                                    response = response.body()!!
                                )

                            }
                    }
            }


        })
    }
}



