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

import android.content.Context
import androidx.lifecycle.LiveData
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.ItemPalletListVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanList
import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

    private val getPaletizadoresUserCase: GetPaletizadoresUserCase = GetPaletizadoresUserCase(context)

    private val _expeditionPalletList by lazy { MutableLiveData<ItemPalletListVO>() }
    val loadExpeditionPalletList = Transformations.map(_expeditionPalletList) { Event(it) }


    private val _scanList by lazy { MutableLiveData<ItemScanList>() }
    val loadScanList = Transformations.map(_scanList) { Event(it) }

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

//sergio: añadido para comprobar rutas escaneado desde PalletFragment
    private val _responseCheckexpeditionScanPut by lazy { MutableLiveData<ResponseItemVO>() }
    val responseCheckexpeditionScanPut: LiveData<ResponseItemVO>
        get() = _responseCheckexpeditionScanPut




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

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

        })
    }

    fun expeditionScanAdd(usuario:String,password:String,vPalletFk:String,vTruckFk:String){
        getPaletizadoresUserCase.expeditionScanAdd(usuario,password,vPalletFk,vTruckFk).enqueue(object :
            Callback<List<ItemScanVO>> {
            override fun onFailure(call: Call<List<ItemScanVO>>, t: Throwable) {
                val listError:ArrayList<ItemScanVO> = ArrayList()
                listError.add(ItemScanVO(isError = true,errorMessage =getMessageFromAllResponse(nameofFunction(this),t.message!!)))
                _scanList.value = ItemScanList(listError)
            }

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

        })
    }

    fun expeditionScanPut(usuario:String,password:String,vPalletFk:String,vExpeditionFk:String){
        getPaletizadoresUserCase.expeditionScanPut(usuario,password,vPalletFk,vExpeditionFk).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())
                }
            }
        })
    }

    fun checkRouteExpeditionScanPut(usuario:String,password:String,vPalletFk:String,vExpeditionFk:String){
        getPaletizadoresUserCase.checkRouteExpeditionScanPut(usuario,password,vPalletFk,vExpeditionFk).enqueue(object : Callback<String>{
            override fun onFailure(call: Call<String>, t: Throwable) {
                _response.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!))
            }
            override fun onResponse(call: Call<String>, response: Response<String>) {
                //sergio: espera repuesta en el body
                if (response.body() == null){
                    _responseCheckexpeditionScanPut.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))
                }else{
                    _responseCheckexpeditionScanPut.value = ResponseItemVO(isError = false,response = response.body()!!)

                }
            }
        })
    }


}
