package es.verdnatura.presentation.view.feature.precontrol

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.domain.GetPreControladorUserCase
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.collection.mapper.map
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class ShowTicketViewModel(context: Context) : BaseViewModel() {
    private val contextApp=context

    private val getPreControladorUserCase: GetPreControladorUserCase = GetPreControladorUserCase(context)

    private val _collectionTicketList by lazy { MutableLiveData<CollectionVO>() }
    val collectionTicketList: LiveData<CollectionVO>
        get() = _collectionTicketList

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

    fun collectionTicketGet(usuario:String,password:String,collectionFk:String,sectorFk:String,type:String){
        getPreControladorUserCase.collectionTicketGet(usuario,password,collectionFk,sectorFk,"0",type).enqueue(object :
            Callback<CollectionVO> {
            override fun onFailure(call: Call<CollectionVO>, t: Throwable) {
                _collectionTicketList.value = CollectionVO(isError = true,errorMessage =getMessageFromAllResponse(nameofFunction(this),t.message!!))
            }
            override fun onResponse(
                call: Call<CollectionVO>,
                response: Response<CollectionVO>
            ) {
                if (response.body() != null){
                    _collectionTicketList.value = response.body()?.let { it.map(contextApp) }
                }else{
                    _collectionTicketList.value = CollectionVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))
                }
            }
        })
    }

}