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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetInventaryUserCase
import es.verdnatura.domain.GetItemCardUserCase
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.faltas.model.ItemFaltasListVO
import es.verdnatura.presentation.view.feature.faltas.model.ItemFaltasVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class FaltasViewModel(context: Context) : BaseViewModel() {


    private val getInventaryUserCase: GetInventaryUserCase = GetInventaryUserCase(context)
    private val getItemCardUserCase: GetItemCardUserCase = GetItemCardUserCase(context)


    private val _faltasList by lazy { MutableLiveData<ItemFaltasListVO>() }
    val faltasList: LiveData<ItemFaltasListVO>
        get() = _faltasList

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


    //val loadFaltasList = Transformations.map(_faltasList) { Event(it) }


    fun getFaltas(user: String, password: String, warehouseFk: String) {
        getInventaryUserCase.faultsReview(user, password, warehouseFk).enqueue(object :
            Callback<List<ItemFaltasVO>> {
            override fun onFailure(call: Call<List<ItemFaltasVO>>, t: Throwable) {
                val listError: ArrayList<ItemFaltasVO> = ArrayList()
                listError.add(
                    ItemFaltasVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _faltasList.value = ItemFaltasListVO(listError)
            }

            override fun onResponse(
                call: Call<List<ItemFaltasVO>>,
                response: Response<List<ItemFaltasVO>>
            ) {
                if (response.body() != null) {
                    _faltasList.value = response.body()?.let { ItemFaltasListVO(it) }
                } else {
                    val listError: ArrayList<ItemFaltasVO> = ArrayList()
                    listError.add(
                        ItemFaltasVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _faltasList.value = ItemFaltasListVO(listError)
                }
            }

        })
    }

    fun itemStockUpdate(
        itemFk: String,
        warehouseFk: String,
        user: String,
        password: String,
        newValue: String,
        isTrash: String
    ) {
        getItemCardUserCase.itemStockUpdate(user, password, itemFk, warehouseFk, newValue, isTrash)
            .enqueue(object :
                Callback<Any> {
                override fun onFailure(call: Call<Any>, t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(call: Call<Any>, response: Response<Any>) {
                    if (!response.isSuccessful) {
                        _response.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _response.value =
                            ResponseItemVO(isError = false, response = response.message())
                    }

                }

            })
    }

    fun faultsReview_isChecked(
        user: String,
        password: String,
        itemFk: String,
        warehouseFk: String
    ) {
        getInventaryUserCase.faultsReview_isChecked(user, password, itemFk, warehouseFk)
            .enqueue(object :
                Callback<Any> {
                override fun onFailure(call: Call<Any>, t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onResponse(call: Call<Any>, response: Response<Any>) {
                    if (!response.isSuccessful) {
                        _response.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _response.value =
                            ResponseItemVO(isError = false, response = response.message())
                    }

                }
            })
    }

}
