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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.MobileApplication
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.Event
import es.verdnatura.presentation.common.ItemShelvingSaleSalix
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.collection.ItemVO
import es.verdnatura.presentation.view.feature.collection.SalesModifiedList
import es.verdnatura.presentation.view.feature.collection.SalixSaleQuantity
import es.verdnatura.presentation.view.feature.collection.listSaleSalix
import es.verdnatura.presentation.view.feature.collection.mapper.map
import es.verdnatura.presentation.view.feature.collection.mapper.proposal
import es.verdnatura.presentation.view.feature.collection.saleSalix
import es.verdnatura.presentation.view.feature.sacador.model.CollectionTicket
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import es.verdnatura.presentation.view.feature.sacador.model.MistakeTypeListVO
import es.verdnatura.presentation.view.feature.sacador.model.MistakeTypeVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementSupplyListVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementSupplyVO
import es.verdnatura.presentation.view.feature.workermistake.model.SaleMistakeSalix
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class CollectionViewModel(val context: Context) : BaseViewModel(context) {

    // val emptyMessage = context.getString(R.string.collectionNoTickets)

    private val contextApp = context

    /*  private val getSacadorControladorUserCase: GetSacadorControladorUserCase =
          GetSacadorControladorUserCase(context)
      private val getUbicadorUserCase: GetUbicadorUserCase = GetUbicadorUserCase(context)
      private val getLoginUserCase: GetLoginUserCase = GetLoginUserCase(context)
  */
    private val _collectionTicketList by lazy { MutableLiveData<CollectionVO>() }
    val collectionTicketList: LiveData<CollectionVO>
        get() = _collectionTicketList

    private val _collectionTicketSalix by lazy { MutableLiveData<CollectionTicket>() }
    val collectionTicketSalix: LiveData<CollectionTicket>
        get() = _collectionTicketSalix


    private val _collectionTicketListLocal by lazy { MutableLiveData<CollectionTicket>() }
    val collectionTicketListLocal: LiveData<CollectionTicket>
        get() = _collectionTicketListLocal

    private val _placementSuppleyList by lazy { MutableLiveData<PlacementSupplyListVO>() }
    val placementSuppleyList: LiveData<PlacementSupplyListVO>
        get() = _placementSuppleyList

    private val _salesModifiedList by lazy { MutableLiveData<SalesModifiedList>() }
    val salesModifiedList: LiveData<SalesModifiedList>
        get() = _salesModifiedList


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

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

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

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

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

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


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




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

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


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

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

   /* private val _responseSaleMove by lazy { MutableLiveData<ResponseItemVO>() }
    val responseSaleMove: LiveData<ResponseItemVO>
        get() = _responseSaleMove*/

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

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

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

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

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

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


    /*  private val _responseMissingTrash by lazy { MutableLiveData<ResponseItemVO>() }
      val responseMissingTrash: LiveData<ResponseItemVO>
          get() = _responseMissingTrash*/

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

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

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

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

    private val _mistakeList by lazy { MutableLiveData<MistakeTypeListVO>() }
    val mistakeList: LiveData<MistakeTypeListVO>
        get() = _mistakeList

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

    private val _item by lazy { MutableLiveData<ItemVO>() }
    val item: LiveData<ItemVO>
        get() = _item

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


    val loadResponseDel = Transformations.map(_responseDel) { Event(it) }
    val loadResponseSaleTrackingMark=Transformations.map(_responseSaleTracking_mark) { Event(it) }
    val loadResponseItemShelvingUpdate=Transformations.map(_responseItemShelvingUpdate) { Event(it) }



    fun collectionTicketGet(
        collectionFk: Int,
        sectorFk: Int,
        print: String,
        type: String
    ) {
        silex.collection_getTickets(
            collectionFk,
            sectorFk,
            print,
            type
        ).enqueue(object :
            SilexCallback<CollectionVO>(context) {
            override fun onError(t: Throwable) {
                _collectionTicketList.value = CollectionVO(
                    0,
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                if (response.body() != null) {
                    _collectionTicketList.value = response.body()?.let { it.map(contextApp) }
                } else {
                    _collectionTicketList.value = CollectionVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                }
            }
        })
    }

    fun collectionTicketGetSalix(
        collectionFk: Int,
        print: Boolean,

    ) {
        salix.getCollectionTickets(
            collectionFk,
            hashMapOf("print" to print)
        ).enqueue(object :
            SalixCallback<CollectionTicket>(context) {
            override fun onError(t: Throwable) {
                _collectionTicketSalix.value = CollectionTicket(
                    0,
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                if (response.body() != null) {
                    _collectionTicketSalix.value = response.body()?.let { it.map(contextApp) }
                } else {
                    _collectionTicketSalix.value = CollectionTicket(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                }
            }
        })
    }
     /*   _collectionTicketListLocal.value = collectionLocal
    }*/


    fun collection_get(
        collectionFk: Int,
        sectorFk: Int,
        print: String,
        type: String
    ) {
        silex.collection_get(
            collectionFk,
            sectorFk,
            print,
            type
        ).enqueue(object :
            SilexCallback<CollectionVO>(context) {
            override fun onError(t: Throwable) {
                _collectionTicketList.value = CollectionVO(
                    0,
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                if (response.body() != null) {
                    _collectionTicketList.value = response.body()?.let { it.map(contextApp) }
                } else {
                    _collectionTicketList.value = CollectionVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                }
            }
        })
    }

    fun saleTrackingReplace(
        saleFk: Int,
        vOriginalQuantity: Int,
        vStateFk: String,
        vIsChecked: String,
        vBuyFk: Long,
        vIsScanned:Boolean?
    ) {
        silex.saleTrackingReplace(
            saleFk,
            vOriginalQuantity,
            vStateFk,
            vIsChecked != "0",
            vBuyFk,
            vIsScanned!!
        ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseSaleReplace.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

    fun saleTracking_mark(
        saleFk: Int,
        vOriginalQuantity: Int,
        vStateFk: String,
        vIsChecked: String,
        vBuyFk: Long,
        vItemShelvingFk: Int,
        vQuantity: Int,
        vIsScanned: Boolean?
    ) {
        silex.saleTracking_mark(
            saleFk,
            vOriginalQuantity,
            vStateFk,
            vIsChecked != "0",
            vBuyFk,
            vItemShelvingFk,
            vQuantity,
            vIsScanned
        ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseSaleTracking_mark.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

    fun confirmItemShelvingReserved(
        itemShelvingSaleFk: Int,
        quantity: Int,
        isItemShelvingSaleEmpty:Boolean?
    ) {
        salix.itemShelvingsConfirmRerserved(params = ItemShelvingSaleSalix(itemShelvingSaleFk,quantity,isItemShelvingSaleEmpty)
        ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseConfirmReservedItemShelvingSale.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

    fun confirmItemShelvingReservedUpdate(
        itemShelvingSaleFk: Int,
        isPicked: Boolean
    ) {
        salix.itemShelvingsReservedUpdate(id =itemShelvingSaleFk, params = hashMapOf("isPicked" to isPicked)
        ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseConfirmReservedUpdate.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

    fun itemPlacementSupplyAiming(
        shelvingFk: String,
        quantity: Int,
        itemFk: Int
    ) {
        silex.itemPlacementSupplyAiming(
            shelvingFk,
            quantity,
            itemFk
        ).enqueue(object :
            Callback<List<PlacementSupplyVO>> {
            override fun onFailure(call: Call<List<PlacementSupplyVO>>, t: Throwable) {
                val listError: ArrayList<PlacementSupplyVO> = ArrayList()
                listError.add(
                    PlacementSupplyVO(
                        itemShelvingFk = 0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _placementSuppleyList.value = PlacementSupplyListVO(listError)
            }

            override fun onResponse(
                call: Call<List<PlacementSupplyVO>>,
                response: Response<List<PlacementSupplyVO>>
            ) {
                if (response.body() != null) {
                    val mappedList = response.body()?.map { it.proposal(context) }
                    _placementSuppleyList.value = PlacementSupplyListVO(mappedList.orEmpty())


                   // _placementSuppleyList.value = response.body()?.let { PlacementSupplyListVO(it) }
                } else {
                    val listError: ArrayList<PlacementSupplyVO> = ArrayList()
                    listError.add(
                        PlacementSupplyVO(
                            itemShelvingFk = 0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _placementSuppleyList.value = PlacementSupplyListVO(listError)
                }
            }
        })
    }

    fun itemShelvingSaleSupplyAdd(
        usuario: String,
        password: String,
        itemShelvingFk: String,
        saleFk: String,
        quantity: String
    ) {
        silex.itemShelvingSaleSupplyAdd(
            usuario,
            password,
            itemShelvingFk,
            saleFk,
            quantity
        ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseItemShelvingSaleSupplyAdd.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

            }
        })
    }


    fun collectionStickerPrint(
        collectionFk: Int,
        labelCount: Int?
    ) {
        silex.collectionStickerPrint(
            collectionFk,
            labelCount
        ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responsePrint.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                _responsePrint.value =
                    ResponseItemVO(isError = false, response = response.message())

            }
        })
    }


    fun ticketCollection_setUsedShelves(
        ticketFk: Int,
        usedShelves: Int
    ) {
        salix.ticketCollectionUpdate("""where={"ticketFk":$ticketFk}""", hashMapOf("usedShelves" to usedShelves))
        .enqueue(object : SilexCallback<Unit>(context) {
            override fun onError(t: Throwable) {

                _responseUsedShelves.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

            }
        })
    }

    fun item_updatePackingShelve(
        vSelf: Int,
        vPacking: Int
    ) {
        silex.item_updatePackingShelve(
            vSelf,
            vPacking
        ).enqueue(object : SilexCallback<Unit>(context) {
            override fun onError(t: Throwable) {
                _responseItem_updatePackingShelve.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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


    fun saleTracking_add(
        saleGroupFk: Int
    ) {
        silex.saleTracking_add(
            saleGroupFk
        ).enqueue(object : SilexCallback<Unit>(context) {
            override fun onError(t: Throwable) {
                _responseSaleGroup.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

    fun collectionAddItem(
        itemFk: Int,
        quantityFk: Int,
        ticketFk: Int,
        warehouseFk: Int
    ) {
        silex.collectionAddItem(
            itemFk,
            quantityFk,
            ticketFk,
            warehouseFk
        ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {

                _responseNew.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

            }
        })
    }


/*    fun saleMove(
        usuario: String,
        password: String,
        saleFk: String,
        quantity: String,
        originalQuantity: String
    ) {
        silex.saleMove(
            usuario,
            password,
            saleFk,
            quantity,
            originalQuantity
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseSaleMove.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                } else {
                    _responseSaleMove.value =
                        ResponseItemVO(isError = false, response = response.message())
                }
            }
        })
    }*/

    fun transferSalesSalix(
        ticketFk: String,
        saleFk: Int,
        quantity: String,

        ) {
        var saleTransfer =
            listSaleSalix(arrayListOf(saleSalix(id = saleFk, quantity = quantity.toInt())))
        salix.transferSalesSalix(
            ticketFk, saleTransfer


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

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

                //sergio: salix devuelve id
                if (response.body() == null) {
                    _responseSplit.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                } else {
                    _responseSplit.value =
                        ResponseItemVO(isError = false, response = response.message())
                }
            }
        })
    }


    fun saleTrackingDel(saleFk: Int) {
        silex.saleTrackingDel(saleFk)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _responseDel.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

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

    fun itemShelving_updateFromSale(
        itemShelvingFk: Int
    ) {
        silex.itemShelving_updateFromSale(
            itemShelvingFk

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

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

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

    fun parking(
        ticketFk: Int,
        parking: String
    ) {
        silex.shelvingPark(ticketFk, parking)
            .enqueue(object : SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {

                    _responseParking.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

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

    fun ticket_setState(
        ticketFk: Int,
        state: String
    ) {
        silex.ticket_setState(ticketFk, state)
            .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 = "")
                }
            })
    }

    fun debug_add(

        buyFk: String,
        message: String
    ) {
        //TAREA 5134
        //Borrar
        silex.debug_add(buyFk, message)
            .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 = "")
                }
            })
    }


    /* fun getIdFromCode(usuario: String, password: String, code: String) {
         silex.getIdFromCode(usuario, password, code)
             .enqueue(object : Callback<String> {
                 override fun onFailure(call: Call<String>, t: Throwable) {
                     _responseCode.value = ResponseItemVO(
                         isError = true,
                         errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                     )
                 }

                 override fun onResponse(call: Call<String>, response: Response<String>) {
                     //sergio:devuelve item
                     if (response.body() == null) {
                         _responseCode.value = ResponseItemVO(
                             isError = true,
                             errorMessage = getMessageFromAllResponse(
                                 nameofFunction(this),
                                 response.message()
                             )
                         )
                     } else {
                         _responseCode.value =
                             ResponseItemVO(isError = false, response = response.body()!!)
                     }
                 }
             })
     }*/

    fun getIdFromCodeSalix(code: String) {
        salix.barcodes_toitem(code)
            .enqueue(object : SilexCallback<String?>(context) {
                override fun onError(t: Throwable) {
                    _responseCode.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

                override fun onSuccess(response: Response<String?>) {

                        _responseCode.value =
                            ResponseItemVO(
                                isError = false,
                                response = if (response.body() == null) "" else{response!!.body().toString()}
                            )

                }
            })
    }


    fun collectionIncreaseQuantitySalix(
        saleFk: Int,
        quantity: String
    ) {
             salix.collectionIncreaseQuantitySalix(
                 SalixSaleQuantity(saleId = saleFk, quantity = quantity.toInt())
             ).enqueue(object : SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseIncQuantity.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

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

  /*  fun sendChekingPresence(
        workerId: String,
        message: String

    ) {
        try {
            salix.sendChekingPresence(
                SalixMessageVO(
                    workerId = workerId.toInt(),
                    message = message
                )
            )
                .enqueue(object : Callback<Boolean> {
                    override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {

                        if (!response.isSuccessful || response.message() == "false") {
                            _responseSalixMessage.value = ResponseItemVO(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this), response.message()
                                )
                            )
                        }

                    }

                    override fun onFailure(call: Call<Boolean>, t: Throwable) {
                        _responseSalixMessage.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this), t.message!!
                            )
                        )

                        //Log.i("Salix Error",""+t.message)
                    }
                })
        } catch (e: Exception) {
        }

    }*/

    fun saleMistakeAdd(
        saleFk: Int,
        typeFk: Int
    ) {

        salix.saleMistakeAdd(SaleMistakeSalix(userFk=(context as MobileApplication).userId!!, saleFk = saleFk,typeFk = typeFk))
            .enqueue(object :
                SilexCallback<Any>(context) {
                override fun onError(t: Throwable) {
                    _responseSaleMistakeAdd.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

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

    fun mistakeType() {

        //Tarea 5134
        salix.getMistakeTypes()
        //silex.mistakeType()
            .enqueue(object : SilexCallback<List<MistakeTypeVO>>(context) {
                override fun onError(t: Throwable) {
                    val listError: ArrayList<MistakeTypeVO> = ArrayList()
                    listError.add(MistakeTypeVO(0, ""))
                    _mistakeList.value = MistakeTypeListVO(listError)
                }

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

                    if (response.body() != null) {
                        _mistakeList.value = response.body()?.let { MistakeTypeListVO(it) }
                    } else {
                        val listError: ArrayList<MistakeTypeVO> = ArrayList()
                        listError.add(MistakeTypeVO(0, ""))
                        _mistakeList.value = MistakeTypeListVO(listError)
                    }
                }

            })
    }


    fun ticket_isOutClosureZone(
        ticketFk: Int
    ) {
        silex.ticket_isOutClosureZone(
            ticketFk
        ).enqueue(object :
            SilexCallback<Any>(context) {
            override fun onError(t: Throwable) {
                _responseTicketClosure.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

            }
        })
    }

    fun collection_getUncheckedTicket(
        collectionFk: Int
    ) {
        silex.collection_getUncheckedTicket(
            collectionFk
        ).enqueue(object :
            SilexCallback<String>(context) {
            override fun onError(t: Throwable) {
                _responseCollectionUnchecked.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                    _responseCollectionUnchecked.value = ResponseItemVO(
                        isError = false,
                        response = response.body()!!,
                        errorMessage = ""
                    )
                } else {
                    _responseCollectionUnchecked.value = ResponseItemVO(
                        isError = true,
                        response = response.body()!!.toString(),
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                }
            }
        })
    }

}
