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.ItemExpeditionScanList
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionScanVO
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 ExpeditionScanViewModel(context: Context) : BaseViewModel() {

    private val getPaletizadoresUserCase: GetPaletizadoresUserCase =
        GetPaletizadoresUserCase(context)

    private val _expeditionScanList by lazy { MutableLiveData<ItemExpeditionScanList>() }
    val loadExpeditionScanList = Transformations.map(_expeditionScanList) { 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

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

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


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

            override fun onResponse(
                call: Call<List<ItemExpeditionScanVO>>,
                response: Response<List<ItemExpeditionScanVO>>
            ) {
                if (response.body() != null) {
                    _expeditionScanList.value = response.body()?.let { ItemExpeditionScanList(it) }
                } else {
                    val listError: ArrayList<ItemExpeditionScanVO> = ArrayList()
                    listError.add(
                        ItemExpeditionScanVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message())
                        )
                    )
                    _expeditionScanList.value = ItemExpeditionScanList(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) {
                _responseCheckexpeditionScanPut.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onResponse(call: Call<String>, response: Response<String>) {
                //sergio:se devuelve 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()!!)

                }
            }
        })
    }


    fun expeditionScanDel(usuario: String, password: String, vScanFk: String) {
        getPaletizadoresUserCase.expeditionScanDel(usuario, password, vScanFk)
            .enqueue(object : Callback<Any> {
                override fun onFailure(call: Call<Any>, t: Throwable) {
                    _responseDeleteScan.value = ResponseItemVO(
                        isError = true, errorMessage = getMessageFromAllResponse(
                            nameofFunction(this), t.message!!
                        )
                    )
                }

                override fun onResponse(call: Call<Any>, response: Response<Any>) {
                    if (!response.isSuccessful) {
                        _responseDeleteScan.value = ResponseItemVO(
                            isError = true, errorMessage = getMessageFromAllResponse(
                                nameofFunction(this), response.message()!!
                            )
                        )
                    } else {
                        _responseDeleteScan.value =
                            ResponseItemVO(isError = false, response = response.message())
                    }
                }
            })
    }

}
