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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.SilexCallback
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.ItemDiscardSalix
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.common.itemBarCodeSalix
import es.verdnatura.presentation.view.feature.articulo.model.AddressLoses
import es.verdnatura.presentation.view.feature.articulo.model.AddressLosesList
import es.verdnatura.presentation.view.feature.articulo.model.ItemCardVO
import es.verdnatura.presentation.view.feature.articulo.model.ItemPackingTypeList
import es.verdnatura.presentation.view.feature.articulo.model.ItemProposal
import es.verdnatura.presentation.view.feature.articulo.model.ItemProposalList
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response
import java.io.File

class ItemCardViewModel(var context: Context) : BaseViewModel(context) {

    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

    private val _addressLosesList by lazy { MutableLiveData<AddressLosesList>() }
    val addressLosesList: LiveData<AddressLosesList>
        get() = _addressLosesList


    fun getItemCard(

        itemFk: Number,
        warehouseFk: Int,

        ) {
        println("La sale el item es $itemFk")
        silex.getItemCard(itemFk, warehouseFk)
            .enqueue(object : SilexCallback<ItemCardVO>(context) {
                override fun onError(t: Throwable) {
                    _itemcard.value = ItemCardVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!),
                        stems = "", itemCost = ""
                    )

                }

                override fun onSuccess(response: Response<ItemCardVO>) {
                    if (response.body() != null) {

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

            })
    }

    fun item_getSimilar(
        itemFk: Int,
        warehouseFk: Int,
        vShipped: String,
        vIsShowedByType: Boolean,

        ) {
        silex.item_getSimilar(
            itemFk,
            warehouseFk,
            vShipped,
            vIsShowedByType
        ).enqueue(object : SilexCallback<List<ItemProposal>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<ItemProposal> = ArrayList()
                listError.add(
                    ItemProposal(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _itemProposallist.value = ItemProposalList(listError)
            }

            override fun onSuccess(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 itemTrash(
        itemFk: Int,
        warehouseFk: Int,
        newValue: Int,
        isTrash: Boolean
    ) {
        silex.itemTrash(itemFk, warehouseFk, newValue, isTrash)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.message())
                }
            })
    }

    fun setVisibleDiscard(
        itemFk: Int,
        warehouseFk: Int,
        quantity: Int,
        addressFk: Int,

        ) {
        salix.setVisibleDiscard(ItemDiscardSalix(itemFk, warehouseFk, quantity, addressFk))
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.message())
                }
            })
    }

    fun updateGrouping(
        itemFk: Int,
        value: Int,
        warehouseFk: Int

    ) {
        silex.updateGrouping(itemFk, value, warehouseFk)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    super.onError(t)
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.message())
                }
            })
    }

    fun updatePacking(
        itemFk: Int,
        value: Int,
        warehouseFk: Int

    ) {
        silex.updatePacking(itemFk, value, warehouseFk)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {

                    _response.value =
                        ResponseItemVO(isError = false, response = response.message())
                }

            })
    }

    fun updatePackingTypeSalix(itemFk: Int, itemPackingTypeFk: String) {
        salix.updateItem(itemFk, hashMapOf("itemPackingTypeFk" to itemPackingTypeFk))
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.body()!!.toString())

                }

            })
    }

    fun item_saveStems(
        itemFk: Int,
        value: Int?

    ) {
        salix.updateItem(itemFk, hashMapOf("stems" to value))
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.message())
                }
            })
    }

    fun item_saveReference(
        itemFk: Int,
        value: Int

    ) {
        salix.updateItem(itemFk, hashMapOf("comment" to value))
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value = ResponseItemVO(isError = false)
                }

            })
    }

    fun barcodesEdit(
        itemFk: Int,
        value: String,
        delete: Boolean
    ) {//Tarea 5134 cambiar Silex para obtener el id y así poder hacer un DELETE y un PATCH
        // salix.barcodes_edit(idBarcode:Int, )
        silex.barcodes_edit(value, itemFk, delete)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value = ResponseItemVO(
                        isError = false,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                }

            })
    }


    fun barcodesEditSalix(itemFk: Int, barCodeValue: String) {
        salix.barcodes_edit(
            itemBarCodeSalix(itemFk, barCodeValue)
        )
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.message()!!)
                }

            })
    }


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


    fun uploadPhoto(
        urlImage: String,
        itemFK: Int,
        file: File = File("$urlImage"),
        fileRequestBody: RequestBody = RequestBody.create(MediaType.parse("image/jpeg"), file),
        filePart: MultipartBody.Part = MultipartBody.Part.createFormData(
            "file",
            file.name,
            fileRequestBody
        ),
    ) {
        salix.uploadImage("catalog", itemFK, filePart)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _responseinsert.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<Any>) {
                    _responseinsert.value = ResponseItemVO(
                        isError = false

                    )
                }

            })
    }


    fun getAddress() {
        salix.getAddressSalix().enqueue(object : SilexCallback<List<AddressLoses>>(context) {
            override fun onError(t: Throwable) {

                val listError: ArrayList<AddressLoses> = ArrayList()
                listError.add(AddressLoses(isError = true, errorMessage = t.message!!))
                _addressLosesList.value = AddressLosesList(listError)
            }

            override fun onSuccess(response: Response<List<AddressLoses>>) {

                if (response.body() != null) {
                    _addressLosesList.value = response.body()?.let { AddressLosesList(it) }
                } else {
                    val listError: ArrayList<AddressLoses> = ArrayList()
                    listError.add(
                        AddressLoses(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _addressLosesList.value = AddressLosesList(listError)
                }
            }

        })
    }

}
