package es.verdnatura.presentation.view.feature.category

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.view.feature.articulo.model.ItemDetails
import retrofit2.Response

class ChangeCategoryViewModel(val context: Context) : BaseViewModel(context) {
    private val _itemDetails by lazy { MutableLiveData<ItemDetails>() }
    val itemDetails: LiveData<ItemDetails> = _itemDetails

    private val _responseDevalue by lazy { MutableLiveData<Boolean>() }
    var responseDevalue: LiveData<Boolean> = _responseDevalue
    
    fun itemDevalueA2(
        itemFk: Long, shelvingFk: String, buyingValue: Double, quantity: Int
    ) {
        salix.itemDevalueA2(
            params = arrayListOf(
                itemFk,
                shelvingFk,
                buyingValue,
                quantity
            ).formatWithQuotes()
        )
            .enqueue(object : SalixCallback<Unit>(context) {
                override fun onSuccess(response: Response<Unit>) {
                    _responseDevalue.value = true
                    super.onSuccess(response)
                }

                override fun onError(t: Throwable) {
                    super.onError(t)
                    _responseDevalue.value = false
                }
            })
    }

    fun itemShelvingGetItemDetails(
        barcode: Long, shelvingFk: String
    ) {
        salix.itemShelvingGetItemDetails(
            params = arrayListOf(
                barcode,
                shelvingFk
            ).formatWithQuotes()
        )
            .enqueue(object : SalixCallback<List<ItemDetails>>(context) {
                override fun onSuccess(response: Response<List<ItemDetails>>) {
                    response.body()?.get(0).let {
                        _itemDetails.value = it
                    }

                }

            })
    }
}