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.ItemPalletViewListVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletViewVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

    private val getPaletizadoresUserCase: GetPaletizadoresUserCase = GetPaletizadoresUserCase(context)

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

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

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



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

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

        })
    }

    fun expeditionPalletDel(usuario:String,password:String,vPalletFk:String){
        getPaletizadoresUserCase.expeditionPalletDel(usuario,password,vPalletFk).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 expeditionPalletPrintSet(
        usuario: String,
        password: String,
        vPalletFk: String,
        vSectorFk: String){

        getPaletizadoresUserCase.expeditionPalletPrintSet(usuario,password,vPalletFk,vSectorFk).enqueue(object : Callback<Any>{
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responsePrintPallet.value = ResponseItemVO(isError = true,errorMessage =getMessageFromAllResponse(nameofFunction(this),t.message!!))
            }
            override fun onResponse(call: Call<Any>, response: Response<Any>) {
                if (!response.isSuccessful){
                    _responsePrintPallet.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))
                }else{
                    _responsePrintPallet.value = ResponseItemVO(isError = false,response = response.message())
                }
            }
        })
    }

    fun expeditionState_addByPallet(
        usuario: String,
        password: String,
        vPalletFk: String,
        vStateCode:String,
        ){

        getPaletizadoresUserCase.expeditionState_addByPallet(usuario,password,vPalletFk,vStateCode).enqueue(object : Callback<Unit>{
            override fun onFailure(call: Call<Unit>, t: Throwable) {
                _response.value = ResponseItemVO(isError = true,errorMessage =getMessageFromAllResponse(nameofFunction(this),t.message!!))
            }
            override fun onResponse(call: Call<Unit>, response: Response<Unit>) {
                if (!response.isSuccessful){
                    _response.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))
                }else{
                    _response.value = ResponseItemVO(isError = false,response = response.message())
                }
            }
        })
    }

}