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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.domain.GetShelvingParkingUserCase
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.shelvingparking.model.ItemShelvingParkingListVO
import es.verdnatura.presentation.view.feature.shelvingparking.model.ItemShelvingParkingVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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


    private val getShelvingParkingUserCase: GetShelvingParkingUserCase = GetShelvingParkingUserCase(context)



    private val _shelvingList by lazy { MutableLiveData<ItemShelvingParkingListVO>() }
    val shelvingList: LiveData<ItemShelvingParkingListVO>
        get() = _shelvingList


    val loadShelvingList = Transformations.map(_shelvingList) { Event(it) }


    fun shelvingParking_get(usuario:String,password:String,vShelvingFk:String,vWarehouseFk:String,vDayRange:String){
        getShelvingParkingUserCase.shelvingParking_get(usuario,password,vShelvingFk.uppercase(),vWarehouseFk,vDayRange).enqueue(object :
            Callback<List<ItemShelvingParkingVO>> {
            override fun onFailure(call: Call<List<ItemShelvingParkingVO>>, t: Throwable) {
                val listError:ArrayList<ItemShelvingParkingVO> = ArrayList()
                listError.add(ItemShelvingParkingVO(isError = true,errorMessage =  getMessageFromAllResponse(nameofFunction(this),t.message!!)))
                _shelvingList.value = ItemShelvingParkingListVO(listError)
            }

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

        })
    }




}
