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.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.Event
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.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.Response

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

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

    private val _expeditionScanList by lazy { MutableLiveData<ItemExpeditionScanList>() }
    val loadExpeditionScanList: LiveData<Event<ItemExpeditionScanList>> =
        _expeditionScanList.map { Event(it) }

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

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

    private val _responseScanPut by lazy { MutableLiveData<Int>() }
    val responseScanPut: LiveData<Int> = _responseScanPut

    val loadResponseScanPut: LiveData<Event<Int>> =
        _responseScanPut.map { Event(it) }
    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 expeditionScanList(vPalletFk: Int) {
        salix.expeditionScanList(arrayListOf(vPalletFk))
            .enqueue(object : SalixCallback<List<ItemExpeditionScanVO>>(context) {
                override fun onSuccess(response: Response<List<ItemExpeditionScanVO>>) {
                    _expeditionScanList.value = response.body()?.let { ItemExpeditionScanList(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 onSuccess(response: Response<Any>) {
                _responseScanPut.value = vExpeditionFk
            }

        })
    }

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

                _responseExpeditionAddHasNotSameRoute.value = response.body()!! == 1
            }

        })
    }

    fun expeditionScanDel(expeditionFk: Int, palletId: Int) {
        salix.expeditionScanDel(expeditionFk).enqueue(object : SalixCallback<Any>(context) {

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

}
