package es.verdnatura.presentation.view.feature.historicoshelvinglog.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.view.feature.historicoshelvinglog.model.ShelvingLog
import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.ShelvingLogList
import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.ShelvingLogSalix
import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.ShelvingLogSalixList
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


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

    private val _itemShelvingLogList by lazy { MutableLiveData<ShelvingLogList>() }
    val loadShelvingLogList = Transformations.map(_itemShelvingLogList) { Event(it) }

    private val _itemShelvingLogSalixList by lazy { MutableLiveData<ShelvingLogSalixList>() }
    val loadShelvingLogSalixList = Transformations.map(_itemShelvingLogSalixList) { Event(it) }

    private val _shelvingResponse by lazy { MutableLiveData<ShelvingLogSalix>() }
    val shelvingResponse: LiveData<ShelvingLogSalix>
        get() = _shelvingResponse

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

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

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

                    if (response.body() != null) {
                        _itemShelvingLogList.value = response.body()?.let {
                            ShelvingLogList(it)
                        }


                    }

                }

            }

        })
    }

    fun Shelvings(token: String, vShelvingFK: String) {
        itemShelvingLogUSerCase.Shelvings(token, vShelvingFK).enqueue(object :
            Callback<List<ShelvingLogSalix>> {
            override fun onFailure(call: Call<List<ShelvingLogSalix>>, t: Throwable) {
                val listError: ArrayList<ShelvingLogSalix> = ArrayList()
                listError.add(
                    ShelvingLogSalix(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _itemShelvingLogSalixList.value = ShelvingLogSalixList(listError)
            }

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

                if (!response.isSuccessful) {
                    val listError: ArrayList<ShelvingLogSalix> = ArrayList()
                    listError.add(
                        ShelvingLogSalix(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _itemShelvingLogSalixList.value = ShelvingLogSalixList(listError)

                } else {

                    if (response.body() != null) {
                        _itemShelvingLogSalixList.value = response.body()?.let {
                            ShelvingLogSalixList(it)
                        }


                    }

                }

            }

        })

    }
}








