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.ItemExpeditionScanList
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionScanVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanList
import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanVO
import retrofit2.Response

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


    private val _expeditionScanList by lazy { MutableLiveData<ItemExpeditionScanList>() }
    val loadExpeditionScanList = Transformations.map(_expeditionScanList) { 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

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

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


    fun expeditionScanList(vPalletFk: Int) {
        silex.expeditionScanList(vPalletFk).enqueue(object :
            SilexCallback<List<ItemExpeditionScanVO>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<ItemExpeditionScanVO> = ArrayList()
                listError.add(
                    ItemExpeditionScanVO(0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _expeditionScanList.value = ItemExpeditionScanList(listError)
            }

            override fun onSuccess(response: Response<List<ItemExpeditionScanVO>>) {

                if (response.body() != null) {
                    _expeditionScanList.value = response.body()?.let { ItemExpeditionScanList(it) }
                } else {
                    val listError: ArrayList<ItemExpeditionScanVO> = ArrayList()
                    listError.add(
                        ItemExpeditionScanVO(0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _expeditionScanList.value = ItemExpeditionScanList(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 onSuccess(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) {

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

            override fun onSuccess(response: Response<String>) {
                //sergio:se devuelve 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()!!)

                }
            }
        })
    }


    fun expeditionScanDel( vScanFk: Int) {
        //Tarea 5134 // no se cambia hasta que se suba test→master 11-17-23
        //salix.expeditionScanDel(vScanFk).enqueue(object : SalixCallback<Any>(context){
        silex.expeditionScanDel(vScanFk)
            .enqueue(object : SilexCallback<Any> (context){
                override fun onError(t: Throwable) {
                    _responseDeleteScan.value = ResponseItemVO(
                        isError = true, errorMessage = getMessageFromAllResponse(
                            nameofFunction(this), t.message!!
                        )
                    )
                }

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

                        _responseDeleteScan.value =
                            ResponseItemVO(isError = false, response = response.message())
                    }
            })
    }

}
