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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetSacadorControladorUserCase
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.collection.mapper.map
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

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

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

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


    fun collectionTicketGet(usuario:String,password:String,collectionFk:String,sectorFk:String,type:String){
        getSacadorControladorUserCase.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)
                    }
                  /*  response.body()?.let {
                      Log.d("VERDNTURA::","el itempacking es "+it.tickets.get(0).itemPackingTypeFk)
                    }.also {  }*/



                }else{
                    _collectionTicketList.value = CollectionVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))
                }
            }
        })
    }


}
