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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
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 AutomaticAddItemViewModel(val context: Context) : BaseViewModel(context) {

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

    fun itemShelvingMakeMulti(
        shelvingFk: String,
        items: List<Long>,
        warehouseFk: Int
    ) {
        //Tarea 6276
        //salix.itemShelvingMakeMulti(MakeMultiSalix(shelvingFk,items,warehouseFk))
        silex.itemShelvingMake_multi(shelvingFk, items, warehouseFk)
            .enqueue(object :
                SalixCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true, errorMessage = getMessageFromAllResponse(
                            nameofFunction(this), t.message!!
                        )
                    )
                }

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

                    _response.value =
                        ResponseItemVO(isError = false, response = response.message()!!)

                }
            })
    }

    fun itemShelvingAddList(
        shelving: String,
        items: Any,
        isChecking: Boolean,
        warehouse: Int
    ) {
        salix.itemShelvingAddList(
            arrayListOf(shelving, items, isChecking, warehouse).formatWithQuotes()
        ).enqueue(object :
            SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _response.value =
                    ResponseItemVO(isError = false, response = response.message()!!)

            }

            override fun onError(t: Throwable) {
                _response.value = ResponseItemVO(
                    isError = true, errorMessage = getMessageFromAllResponse(
                        nameofFunction(this), t.message!!
                    )
                )
            }

        })
    }

}
