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.view.feature.collection.mapper.map
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

    private val contextApp=context
    private val _collectionTicketList by lazy { MutableLiveData<CollectionVO>() }
    private val getPreControladorUserCase: GetPreControladorUserCase = GetPreControladorUserCase(context)

    val collectionTicketList: LiveData<CollectionVO>
        get() = _collectionTicketList

    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()))
                }
            }
        })
    }

    /* sergio: para el caso que íbamos a diferenciar pero ahora se hará desde Collection_getTickets pasánsole parámetro

    fun collection_getTickets(usuario:String,password:String,collectionFk:String,sectorFk:String){
        //getPreControladorUserCase.collection_getTickets(usuario,password,collectionFk,sectorFk,"0").enqueue(object :
        getPreControladorUserCase.collection_getTickets(usuario,password,collectionFk,sectorFk,"0").enqueue(object :
            Callback<CollectionVO> {
            override fun onFailure(call: Call<CollectionVO>, t: Throwable) {
                _collectionTicketList.value = CollectionVO(isError = true,errorMessage = t.message!!)
            }
            override fun onResponse(
                call: Call<CollectionVO>,
                response: Response<CollectionVO>
            ) {
                if (response.body() != null){
                    _collectionTicketList.value = response.body()?.let { it.map() }
                }else{
                    _collectionTicketList.value = CollectionVO(isError = true,errorMessage = "No hay tickets para sacar")
                }
            }
        })
    }

*/


}