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


import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.domain.itemShelvingLogUSerCase
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.ItemShelvingSaleDateList
import es.verdnatura.presentation.view.feature.historicoshelving.model.ItemShelvingLogList
import es.verdnatura.presentation.view.feature.historicoshelving.model.ItemShelvingLog
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import timber.log.Timber.i

class itemShelvingLogViewModel(context: Context) : BaseViewModel() {
    private val itemShelvingLogUSerCase: itemShelvingLogUSerCase =
        itemShelvingLogUSerCase(context)



    private val _itemShelvingLogList by lazy { MutableLiveData<ItemShelvingLogList>() }


    val loadItemShelvingLogList = Transformations.map(_itemShelvingLogList) { Event(it) }



    fun itemShelvingLog_get(usuario:String,password:String,vShelvingFK: String){
        itemShelvingLogUSerCase.itemShelvingLog_get(usuario,password,vShelvingFK).enqueue(object :
            Callback<List<ItemShelvingLog>> {
            override fun onFailure(call: Call<List<ItemShelvingLog>>, t: Throwable) {
                val listError:ArrayList<ItemShelvingLog> = ArrayList()
                listError.add(ItemShelvingLog(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!)))
                _itemShelvingLogList.value = ItemShelvingLogList(listError)
            }

            override fun onResponse(
                call: Call<List<ItemShelvingLog>>,
                response: Response<List<ItemShelvingLog>>
                                   ) {

                if (!response.isSuccessful){
                    val listError:ArrayList<ItemShelvingLog> = ArrayList()
                    listError.add(ItemShelvingLog(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())))
                    _itemShelvingLogList.value = ItemShelvingLogList(listError)
                }else{

                    if (response.body() != null)
                    {
                        i("crea la lista %s", response.body())
                        _itemShelvingLogList.value = response.body()?.let {
                            ItemShelvingLogList(it)
                        }


                    }

                }

            }

        })
    }



}








