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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.domain.SalixCallback
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletListVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanList
import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanVO
import retrofit2.Call
import retrofit2.Response

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

    private val _expeditionPalletList by lazy { MutableLiveData<ItemPalletListVO>() }
    val loadExpeditionPalletList = Transformations.map(_expeditionPalletList) { Event(it) }

    private val _scanList by lazy { MutableLiveData<ItemScanList>() }
    val loadScanList = Transformations.map(_scanList) { Event(it) }

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

    //sergio: añadido para comprobar rutas escaneado desde PalletFragment
    private val _responseCheckexpeditionScanPut by lazy { MutableLiveData<ResponseItemVO>() }
    val responseCheckexpeditionScanPut: LiveData<ResponseItemVO>
        get() = _responseCheckexpeditionScanPut

    fun expeditionPalletList(vTruckFk: Int) {
        salix.expeditionPalletList(
            arrayListOf(vTruckFk)
        )
            .enqueue(object :
                SalixCallback<List<ItemPalletVO>>(context) {
                override fun onSuccess(response: Response<List<ItemPalletVO>>) {

                    _expeditionPalletList.value =
                        response.body()?.let { ItemPalletListVO(it) }

                }

            })
    }

    fun expeditionScanAdd(vPalletFk: Int, vTruckFk: Int) {
        salix.expeditionScanAdd(
            arrayListOf(vPalletFk, vTruckFk)
        )
            .enqueue(object :
                SalixCallback<List<ItemScanVO>>(context) {
                override fun onSuccess(response: Response<List<ItemScanVO>>) {
                        _scanList.value = response.body()?.let { ItemScanList(it) }
                }

            })
    }

    fun expeditionScanPut(vPalletFk: Int, vExpeditionFk: Int) {
        salix.expeditionScanPut(arrayListOf(vPalletFk, vExpeditionFk))
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onResponse(call: Call<Any>, 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!!)
                    )
                }
            })
    }

    fun expeditionCheckRoute(vPalletFk: Int, vExpeditionFk: Int) {
        salix.expeditionCheckRoute(arrayListOf(vPalletFk, vExpeditionFk))
            .enqueue(object : SalixCallback<String>(context) {
                override fun onSuccess(response: Response<String>) {

                        _responseCheckexpeditionScanPut.value =
                            ResponseItemVO(isError = false, response = response.body()!!)

                    }


            })
    }

}
