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


import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.domain.GetPaletizadoresUserCase
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.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckList
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

    private val getPaletizadoresUserCase: GetPaletizadoresUserCase = GetPaletizadoresUserCase(context)

    private val _expeditionTruckList by lazy { MutableLiveData<ItemExpeditionTruckList>() }
    val loadExpeditionTruckList = Transformations.map(_expeditionTruckList) { Event(it) }

    private val _response by lazy { MutableLiveData<ResponseItemVO>() }
    val loadResponseExpeditionAdd = Transformations.map(_response) { Event(it) }




    fun expeditionTruckList(usuario:String,password:String){
        getPaletizadoresUserCase.expeditionTruckList(usuario,password).enqueue(object :
            Callback<List<ItemExpeditionTruckVO>> {
            override fun onFailure(call: Call<List<ItemExpeditionTruckVO>>, t: Throwable) {
                val listError:ArrayList<ItemExpeditionTruckVO> = ArrayList()
                listError.add(ItemExpeditionTruckVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!)))
                _expeditionTruckList.value = ItemExpeditionTruckList(listError)
            }

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

        })
    }

    fun expeditionTruckAdd(usuario:String,password:String,vHour:String,vDescription:String){
        getPaletizadoresUserCase.expeditionTruckAdd(usuario,password,vHour,vDescription).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())
                }

            }

        })
    }

}
