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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.Event
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: LiveData<Event<ItemPalletViewListVO>> =
        _expeditionPalletList.map { Event(it) }

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

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

    fun expeditionPalletView(vPalletFk: Int) {
        salix.expeditionPalletView(arrayListOf(vPalletFk))
            .enqueue(object : SalixCallback<List<ItemPalletViewVO>>(context) {
                override fun onSuccess(response: Response<List<ItemPalletViewVO>>) {
                    _expeditionPalletList.value = response.body()?.let { ItemPalletViewListVO(it) }
                }
            })
    }

    fun expeditionPalletDel(vPalletFk: Int) {
        salix.expeditionPalletDel(vPalletFk).enqueue(object : SalixCallback<Any>(context) {

            override fun onSuccess(response: Response<Any>) {
                _response.value = true
            }
        })
    }

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

            })
    }

    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 = true
            }

        })
    }

}