package es.verdnatura.presentation.view.feature.boxPicking

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.userCases.GetItemFromBarcodeUseCase
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.ExpeditionPrintOut
import retrofit2.Response

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

    private val getItemFromBarcodeUseCase = GetItemFromBarcodeUseCase(salix)

    /*private val _isBoxPickingInPrintOut by lazy { MutableLiveData<Boolean>() }
    val isBoxPickingInPrintOut: LiveData<Boolean> = _isBoxPickingInPrintOut*/
    private val _responseFindExpeditionId by lazy { MutableLiveData<Number>() }
    val responseFindExpeditionId: LiveData<Number> = _responseFindExpeditionId

    //Tarea 7751
    private val _isBoxPickingInPrintOut by lazy { MutableLiveData<ExpeditionPrintOut>() }
    val isBoxPickingInPrintOut: LiveData<ExpeditionPrintOut> = _isBoxPickingInPrintOut

    private val _responseCode by lazy { MutableLiveData<Boolean>() }
    val responseCode: LiveData<Boolean> = _responseCode
    fun isBoxPickingOk(
        expeditionFk: Long, itemFk: Long

    ) {

        salix.isBoxPickingInPrintOut(
            filter = """{"where":{"expeditionFk":$expeditionFk,"itemFk":$itemFk}}"""
            /* ).enqueue(object : SalixCallback<Any>(context) {

                 override fun onSuccess(response: Response<Any>) {
                     _isBoxPickingInPrintOut.value = true
                 }
                 override fun onError(t: Throwable) {
                     _isBoxPickingInPrintOut.value = false
                 }

             })*/
            //Tarea 7751
            //isChecked
        ).enqueue(object : SalixCallback<ExpeditionPrintOut>(context) {

            override fun onSuccess(response: Response<ExpeditionPrintOut>) {
                _isBoxPickingInPrintOut.value = response.body()
            }

            override fun onError(t: Throwable) {
                _isBoxPickingInPrintOut.value = ExpeditionPrintOut(0, 0, false)
            }

        })
    }

    fun isBoxPickingInPrintOut(expeditionFk: Long, barcode: String) {
        getItemFromBarcodeUseCase.execute(barcode)
            .enqueue(object : SalixCallback<Int?>(context) {

                override fun onSuccess(response: Response<Int?>) {

                    if (response.body() != null) {
                        isBoxPickingOk(
                            itemFk = response.body().toString().toLong(),
                            expeditionFk = expeditionFk
                        )
                    } else {
                        _responseCode.value = false
                    }

                }
            })
    }

    fun findExpedition(expedition: Number, expeditionStateId: Number) {
        salix.findExpedition(
            filter = """{"where":
            {
                "expeditionFk":$expedition,
                "typeFk":$expeditionStateId
                    }
             }""".trimMargin()
        ).enqueue(object : SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _responseFindExpeditionId.value = 0

            }

            override fun onError(t: Throwable) {
                _responseFindExpeditionId.value = expedition
            }

        })
    }

    fun updateExpeditionPrint(expeditionFk: Long, expeditionPrintOut: ExpeditionPrintOut) {
        salix.update(expeditionFk, expeditionPrintOut)
            .enqueue(object : SalixCallback<ExpeditionPrintOut>(context) {

            })
    }

}