package es.verdnatura.presentation.view.feature.diadeventa.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.diadeventa.model.ItemShelvingSaleDate
import es.verdnatura.presentation.view.feature.diadeventa.model.ItemShelvingSaleDateList
import retrofit2.Response

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

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

    private val _itemShelvingSaleDateList by lazy { MutableLiveData<ItemShelvingSaleDateList>() }
    val itemShelvingSaleDateList: LiveData<ItemShelvingSaleDateList>
        get() = _itemShelvingSaleDateList

    val loadItemShelvingSaleDateList = Transformations.map(_itemShelvingSaleDateList) { Event(it) }

    fun itemShelvingGetSaleDate(vShelvingFK: String) {
        //salix falla fecha
       // silex.itemShelving_getSaleDate(vShelvingFK)
        salix.itemShelvingGetSaleDate(params = listOf(vShelvingFK).formatWithQuotes())
            .enqueue(object :
                SalixCallback<List<ItemShelvingSaleDate>>(context) {
                override fun onError(t: Throwable) {
                    val listError: ArrayList<ItemShelvingSaleDate> = ArrayList()
                    listError.add(
                        ItemShelvingSaleDate(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!
                            )
                        )
                    )
                    _itemShelvingSaleDateList.value = ItemShelvingSaleDateList(listError)
                }

                override fun onSuccess(response: Response<List<ItemShelvingSaleDate>>) {

                    if (response.body() != null) {

                        _itemShelvingSaleDateList.value = response.body()?.let {
                            ItemShelvingSaleDateList(it)
                        }

                    } else {
                        if (!response.isSuccessful) {

                            val listError: ArrayList<ItemShelvingSaleDate> = ArrayList()
                            listError.add(
                                ItemShelvingSaleDate(
                                    isError = true,
                                    errorMessage = getMessageFromAllResponse(
                                        nameofFunction(this),
                                        response.message()
                                    )
                                )
                            )
                            _itemShelvingSaleDateList.value = ItemShelvingSaleDateList(listError)
                        }
                    }
                }

            })
    }

    fun setParking(scanItem: String, parking: String) {
        salix.setParking(arrayListOf(scanItem, parking).formatWithQuotes())
            .enqueue(object :
                SalixCallback<Any>(context) {
            })
    }

}








