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.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.Event
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletViewListVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletViewVO
import retrofit2.Response

class ExpeditionPalletDetailViewModel(var context: Context) : BaseViewModel(context) {

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

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

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

    fun expeditionPalletView(vPalletFk: Int) {
        salix.expeditionPalletView(arrayListOf(vPalletFk))
            .enqueue(object :
                SalixCallback<List<ItemPalletViewVO>>(context) {
                override fun onSuccess(response: Response<List<ItemPalletViewVO>>) {
                    if (response.body() != null) {
                        _expeditionPalletList.value =
                            response.body()?.let { ItemPalletViewListVO(it) }
                    } else {
                        val listError: ArrayList<ItemPalletViewVO> = ArrayList()
                        listError.add(
                            ItemPalletViewVO(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this),
                                    response.message()
                                )
                            )
                        )
                        _expeditionPalletList.value = ItemPalletViewListVO(listError)
                    }
                }

                override fun onError(t: Throwable) {
                    val listError: ArrayList<ItemPalletViewVO> = ArrayList()
                    listError.add(
                        ItemPalletViewVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!
                            )
                        )
                    )
                    _expeditionPalletList.value = ItemPalletViewListVO(listError)
                }
            })
    }

    fun expeditionPalletDel(vPalletFk: Int) {
        salix.expeditionPalletDel(vPalletFk).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,
                    errorMessage = getMessageFromAllResponse(
                        nameofFunction(this),
                        response.message()
                    )
                )
            }
        })
    }

    fun expeditionPalletPrintSet(
        vPalletFk: Int
    ) {
        salix.expeditionPalletPrintLabel(arrayListOf(vPalletFk))
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onSuccess(response: Response<Any>) {
                    _responsePrintPallet.value =
                        ResponseItemVO(isError = false, response = response.message())
                }

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

    fun expeditionStateAddByPallet(
        vPalletFk: Int,
        vStateCode: String,
    ) {
        salix.expeditionStateAddByPallet(
            arrayListOf(vPalletFk, vStateCode).formatWithQuotes()
        )
            .enqueue(object : SalixCallback<Unit>(context) {
                override fun onSuccess(response: Response<Unit>) {
                    _response.value = ResponseItemVO(isError = false, response = response.message())
                }

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

}