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


import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetItemCardUserCase
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.articulo.model.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class ItemCardViewModel(context: Context) : BaseViewModel() {
    private val getItemCardUserCase: GetItemCardUserCase = GetItemCardUserCase(context)
    val version: String = "5.0.0"

    private val _itemcard by lazy { MutableLiveData<ItemCardVO>() }
    val itemcard: LiveData<ItemCardVO>
        get() = _itemcard

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

    private val _itemspackinglist by lazy { MutableLiveData<ItemPackingTypeList>() }
    val itemspackinglist: LiveData<ItemPackingTypeList>
        get() = _itemspackinglist


    private val _itemProposallist by lazy { MutableLiveData<ItemProposalList>() }
    val itemProposallist: LiveData<ItemProposalList>
        get() = _itemProposallist


    fun getItemCard(
        itemFk: String,
        warehouseFk: String,
        user: String,
        password: String

    ) {
        getItemCardUserCase.getItemCard(user, password, itemFk, warehouseFk)
            .enqueue(object : Callback<ItemCardVO> {
                override fun onFailure(call: Call<ItemCardVO>, t: Throwable) {
                    _itemcard.value = ItemCardVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!),
                        stems = ""
                    , itemCost = "")

                }

                override fun onResponse(call: Call<ItemCardVO>, response: Response<ItemCardVO>) {


                    if (response.body() != null ) {

                        _itemcard.value = response.body()!!
                    } else {
                        _itemcard.value = ItemCardVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            ),
                            stems = "",
                            itemCost = ""
                        )
                    }
                }

            })
    }

    fun item_getSimilar(
        itemFk: String,
        warehouseFk: String,
        vShipped: String,
        vIsShowedByType: String,
        user: String,
        password: String
    ) {
        getItemCardUserCase.item_getSimilar(
            user,
            password,
            itemFk,
            warehouseFk,
            vShipped,
            vIsShowedByType
        ).enqueue(object : Callback<List<ItemProposal>> {
            override fun onFailure(call: Call<List<ItemProposal>>, t: Throwable) {
                val listError: ArrayList<ItemProposal> = ArrayList()
                listError.add(
                    ItemProposal(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _itemProposallist.value = ItemProposalList(listError)
            }

            override fun onResponse(
                call: Call<List<ItemProposal>>,
                response: Response<List<ItemProposal>>
            ) {
                if (response.body() != null) {
                    _itemProposallist.value = response.body()?.let {
                        ItemProposalList(it)
                    }

                } else {
                    val listError: ArrayList<ItemProposal> = ArrayList()
                    listError.add(
                        ItemProposal(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _itemProposallist.value = ItemProposalList(listError)

                }
            }

        })
    }


    fun getItemPackingType(user: String, password: String) {
        getItemCardUserCase.getItemPackingType(user, password).enqueue(object :
            Callback<List<ItemPackingType>> {
            override fun onFailure(call: Call<List<ItemPackingType>>, t: Throwable) {
                val listError: ArrayList<ItemPackingType> = ArrayList()
                listError.add(
                    ItemPackingType(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _itemspackinglist.value = ItemPackingTypeList(listError)
            }

            override fun onResponse(
                call: Call<List<ItemPackingType>>,
                response: Response<List<ItemPackingType>>
            ) {
                if (response.body() != null) {
                    _itemspackinglist.value = response.body()?.let { ItemPackingTypeList(it) }

                } else {
                    val listError: ArrayList<ItemPackingType> = ArrayList()
                    listError.add(
                        ItemPackingType(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _itemspackinglist.value = ItemPackingTypeList(listError)
                }
            }

        })
    }

    fun getItemPackingTypeSalix(token: String) {
        getItemCardUserCase.getItemPackingTypeSalix(token).enqueue(object :
            Callback<List<ItemPackingType>> {
            override fun onFailure(call: Call<List<ItemPackingType>>, t: Throwable) {
                val listError: ArrayList<ItemPackingType> = ArrayList()
                listError.add(
                    ItemPackingType(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _itemspackinglist.value = ItemPackingTypeList(listError)
            }

            override fun onResponse(
                call: Call<List<ItemPackingType>>,
                response: Response<List<ItemPackingType>>
            ) {
                if (response.body() != null) {
                    _itemspackinglist.value = response.body()?.let { ItemPackingTypeList(it) }

                } else {
                    val listError: ArrayList<ItemPackingType> = ArrayList()
                    listError.add(
                        ItemPackingType(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _itemspackinglist.value = ItemPackingTypeList(listError)
                }
            }

        })
    }


    fun itemTrash(
        itemFk: String,
        warehouseFk: String,
        user: String,
        password: String,
        newValue: String,
        isTrash: String
    ) {
        getItemCardUserCase.itemTrash(user, password, itemFk, warehouseFk, newValue, isTrash)
            .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 updateGrouping(
        itemFk: String,
        user: String,
        password: String,
        value: String,
        warehouseFk: String

    ) {
        getItemCardUserCase.updateGrouping(user, password, itemFk, value, warehouseFk)
            .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 updatePacking(
        itemFk: String,
        user: String,
        password: String,
        value: String,
        warehouseFk: String

    ) {
        getItemCardUserCase.updatePacking(user, password, itemFk, value, warehouseFk)
            .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())
                    }

                }

            })
    }

    //sergio ver como solucionar el tema del "Finish"
    fun updatePackingTypeSalix(token: String, itemFk: String, itemPackingType: String) {
        getItemCardUserCase.updatePackingTypeSalix(token, itemFk, itemPackingType)
            .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.body()!!.toString())
                    }

                }

            })
    }

    fun item_saveStems(
        itemFk: String,
        user: String,
        password: String,
        value: String

    ) {
        getItemCardUserCase.item_saveStems(user, password, itemFk, value)
            .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 item_saveReference(
        itemFk: String,
        user: String,
        password: String,
        value: String

    ) {
        getItemCardUserCase.item_saveReference(user, password, itemFk, value)
            .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)
                    }

                }

            })
    }

    fun barcodesEdit(
        itemFk: String,
        user: String,
        password: String,
        value: String,
        delete: String
    ) {
        getItemCardUserCase.barcodes_edit(user, password, itemFk, value, delete)
            .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 barcodesEditSalix(token: String, itemFk: String, barCodeValue: String) {
        getItemCardUserCase.barcodes_editSalix(token, itemFk, barCodeValue)
            .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()!!)
                    }

                }

            })
    }

}
