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.SilexCallback
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 expeditionPallet_List(vTruckFk: Int) {
        silex.expeditionPallet_List(vTruckFk).enqueue(object :
            SilexCallback<List<ItemPalletVO>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<ItemPalletVO> = ArrayList()
                listError.add(
                    ItemPalletVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _expeditionPalletList.value = ItemPalletListVO(listError)
            }

            override fun onSuccess(response: Response<List<ItemPalletVO>>) {
                if (response.body() != null) {
                    _expeditionPalletList.value = response.body()?.let { ItemPalletListVO(it) }
                } else {
                    val listError: ArrayList<ItemPalletVO> = ArrayList()
                    listError.add(
                        ItemPalletVO(
                            0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _expeditionPalletList.value = ItemPalletListVO(listError)
                }
            }

        })
    }

    fun expeditionScanAdd(vPalletFk: Int, vTruckFk: Int) {
        silex.expeditionScanAdd(vPalletFk, vTruckFk).enqueue(object :
            SilexCallback<List<ItemScanVO>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<ItemScanVO> = ArrayList()
                listError.add(
                    ItemScanVO(
                        "",
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _scanList.value = ItemScanList(listError)
            }

            override fun onSuccess(response: Response<List<ItemScanVO>>) {
                if (response.body() != null) {
                    _scanList.value = response.body()?.let { ItemScanList(it) }
                } else {
                    val listError: ArrayList<ItemScanVO> = ArrayList()
                    listError.add(
                        ItemScanVO(
                            "",
                            0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _scanList.value = ItemScanList(listError)
                }
            }

        })
    }

    fun expeditionScanPut(vPalletFk: Int, vExpeditionFk: Int) {
        silex.expeditionScanPut(vPalletFk, vExpeditionFk)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(call: Call<Any>, response: Response<Any>) {
                    _response.value = ResponseItemVO(isError = false, response = response.message())
                }

            })
    }

    fun checkRouteExpeditionScanPut(vPalletFk: Int, vExpeditionFk: Int) {
        silex.checkRouteExpeditionScanPut(vPalletFk, vExpeditionFk)
            .enqueue(object : SilexCallback<String>(context) {
                override fun onError(t: Throwable) {

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

                override fun onSuccess(response: Response<String>) {
                    //sergio: espera repuesta en el body
                    if (response.body() == null) {
                        _responseCheckexpeditionScanPut.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _responseCheckexpeditionScanPut.value =
                            ResponseItemVO(isError = false, response = response.body()!!)

                    }
                }
            })
    }


}
