package es.verdnatura.presentation.view.feature.smarttag.sacador.fragment

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetSmarttagsUserCase
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 es.verdnatura.presentation.view.feature.smarttag.sacador.model.SmartTagRegister
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SmartTagsViewModel(context: Context) : BaseViewModel() {

    private val GetSmarttagsUserCase: GetSmarttagsUserCase = GetSmarttagsUserCase(context)

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

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

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

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


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

    private val _registerSmarttagNode by lazy { MutableLiveData<SmartTagRegister>() }
    val registerSmarttagNode: LiveData<SmartTagRegister>
        get() = _registerSmarttagNode


    fun workerShelving_add(usuario: String, password: String, shelvingFK: String) {
        GetSmarttagsUserCase.workerShelving_add(usuario, password, shelvingFK)
            .enqueue(object : Callback<Void> {
                override fun onFailure(call: Call<Void>, t: Throwable) {

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

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

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

                }


            })
    }

   /* fun messageError(message: String, call: String): ResponseItemVO? {
        return ResponseItemVO(
            isError = true,
            errorMessage = "Error al llamar a " + call + " . Descripción del error:" + message
        )
    }*/


    fun workerShelving_delete(usuario: String, password: String, vBarcode: String) {
        GetSmarttagsUserCase.workerShelving_delete(usuario, password, vBarcode)
            .enqueue(object : Callback<Void> {
                override fun onFailure(call: Call<Void>, t: Throwable) {
                    //_responseremove.value = ResponseItemVO(isError = true,errorMessage = "Error al llamar a workerShelving_remove")
                    _responseremove.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            t.message!!
                        ))
                }

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

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


                }

            })
    }

    fun smartTagRegister(smarttag: String, wagon: String, shelving: String) {
        GetSmarttagsUserCase.smartTagRegister(smarttag, wagon, shelving)
            .enqueue(object : Callback<SmartTagRegister> {
                override fun onResponse(
                    call: Call<SmartTagRegister>,
                    response: Response<SmartTagRegister>
                ) {

                    val responseSmart: SmartTagRegister

                    if (response.isSuccessful) {
                        responseSmart = SmartTagRegister(
                            message = response.message(),
                            isError = false,
                            errorMessage = ""
                        )

                    } else {
                        // sergio: buscar JSON obtindre el "message" Smarttag
                        var text = JSONObject(response.errorBody()!!.string()).get("error")
                        var message = JSONObject(text.toString()).get("message")

                        responseSmart = SmartTagRegister(
                            isError = true,
                            errorMessage = message.toString()
                        )

                    }

                    _registerSmarttagNode.value = responseSmart
                }

                override fun onFailure(call: Call<SmartTagRegister>, t: Throwable) {
                    val responseSmart =
                        SmartTagRegister(isError = true, errorMessage = t.message!!)

                    //Log.d("VERDNATURA::","el error nodejs es "+t.message+"--"+t.cause.toString()+"--"+t.stackTrace.toString())

                    _registerSmarttagNode.value = responseSmart
                }

            })
    }

}