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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.R
import es.verdnatura.domain.GetLoginUserCase
import es.verdnatura.domain.GetSacadorControladorUserCase
import es.verdnatura.domain.GetUbicadorUserCase
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.collection.ItemVO
import es.verdnatura.presentation.view.feature.collection.SalesModifiedList
import es.verdnatura.presentation.view.feature.collection.mapper.map
import es.verdnatura.presentation.view.feature.sacador.model.*
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

   // 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 _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 _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 _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 _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


    fun collectionTicketGet(
        usuario: String,
        password: String,
        collectionFk: String,
        sectorFk: String,
        type: String
    ) {
        getSacadorControladorUserCase.collectionTicketGet(
            usuario,
            password,
            collectionFk,
            sectorFk,
            "0",
            type
        ).enqueue(object :
            Callback<CollectionVO> {
            override fun onFailure(call: Call<CollectionVO>, t: Throwable) {
                _collectionTicketList.value = CollectionVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onResponse(
                call: Call<CollectionVO>,
                response: Response<CollectionVO>
            ) {
                if (response.body() != null) {
                    _collectionTicketList.value = response.body()?.let { it.map(contextApp) }
                } else {
                    _collectionTicketList.value = CollectionVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                }
            }
        })
    }


    fun saleTrackingReplace(
        usuario: String,
        password: String,
        saleFk: String,
        vOriginalQuantity: String,
        vStateFk: String,
        vIsChecked: String,
        vBuyFk: String
    ) {
        getSacadorControladorUserCase.saleTrackingReplace(
            usuario,
            password,
            saleFk,
            vOriginalQuantity,
            vStateFk,
            vIsChecked,
            vBuyFk
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseSaleReplace.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onResponse(call: Call<Any>, response: Response<Any>) {
                if (!response.isSuccessful) {
                    _responseSaleReplace.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                } else {
                    _responseSaleReplace.value = ResponseItemVO(isError = false, response = "")
                }
            }
        })
    }
    fun saleTracking_mark(
        usuario: String,
        password: String,
        saleFk: String,
        vOriginalQuantity: String,
        vStateFk: String,
        vIsChecked: String,
        vBuyFk: String,
        vItemShelvingFk:String,
        vQuantity:String
    ) {
        getSacadorControladorUserCase.saleTracking_mark(
            usuario,
            password,
            saleFk,
            vOriginalQuantity,
            vStateFk,
            vIsChecked,
            vBuyFk,
            vItemShelvingFk,
            vQuantity
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseSaleTracking_mark.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onResponse(call: Call<Any>, 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 itemPlacementSupplyAiming(
        usuario: String,
        password: String,
        shelvingFk: String,
        quantity: String,
        itemFk: String
    ) {
        getSacadorControladorUserCase.itemPlacementSupplyAiming(
            usuario,
            password,
            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(
                        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) {
                    _placementSuppleyList.value = response.body()?.let { PlacementSupplyListVO(it) }
                } else {
                    val listError: ArrayList<PlacementSupplyVO> = ArrayList()
                    listError.add(
                        PlacementSupplyVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _placementSuppleyList.value = PlacementSupplyListVO(listError)
                }
            }
        })
    }

    fun itemShelvingSaleSupplyAdd(
        usuario: String,
        password: String,
        itemShelvingFk: String,
        saleFk: String,
        quantity: String
    ) {
        getSacadorControladorUserCase.itemShelvingSaleSupplyAdd(
            usuario,
            password,
            itemShelvingFk,
            saleFk,
            quantity
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseItemShelvingSaleSupplyAdd.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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


    fun collectionStickerPrint(
        usuario: String,
        password: String,
        collectionFk: String,
        sectorFk: String
    ) {
        getSacadorControladorUserCase.collectionStickerPrint(
            usuario,
            password,
            collectionFk,
            sectorFk
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responsePrint.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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


    fun ticketCollection_setUsedShelves(
        usuario: String,
        password: String,
        ticketFk: String,
        usedShelves: String
    ) {
        getSacadorControladorUserCase.ticketCollection_setUsedShelves(
            usuario,
            password,
            ticketFk,
            usedShelves
        ).enqueue(object : Callback<Void> {
            override fun onFailure(call: Call<Void>, t: Throwable) {
                _responseUsedShelves.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

    fun item_updatePackingShelve(
        usuario: String,
        password: String,
        vSelf: String,
        vPacking: String
    ) {
        getSacadorControladorUserCase.item_updatePackingShelve(
            usuario,
            password,
            vSelf,
            vPacking
        ).enqueue(object : Callback<Void> {
            override fun onFailure(call: Call<Void>, t: Throwable) {
                _responseItem_updatePackingShelve.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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


    fun saleTracking_add(
        usuario: String,
        password: String,
        saleGroupFk: String
    ) {
        getSacadorControladorUserCase.saleTracking_add(
            usuario,
            password,
            saleGroupFk
        ).enqueue(object : Callback<Void> {
            override fun onFailure(call: Call<Void>, t: Throwable) {
                _responseSaleGroup.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

    fun collectionAddItem(
        usuario: String,
        password: String,
        itemFk: String,
        quantityFk: String,
        ticketFk: String,
        warehouseFk: String
    ) {
        getSacadorControladorUserCase.collectionAddItem(
            usuario,
            password,
            itemFk,
            quantityFk,
            ticketFk,
            warehouseFk
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseNew.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

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


            }
        })
    }





    fun saleMove(
        usuario: String,
        password: String,
        saleFk: String,
        quantity: String,
        originalQuantity: String
    ) {
        getSacadorControladorUserCase.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(
        token:String,
        ticketFk:String,
        saleFk: String,
        quantity: String,

    ) {
        getSacadorControladorUserCase.transferSalesSalix(
            token,
            ticketFk,
            saleFk,
            quantity

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

            override fun onResponse(call: Call<Any>, 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 collectionMissingTrash(
        usuario: String,
        password: String,
        saleFk: String,
        quantity: String,
        type: String,
        warehouseFk: String,
        originalQuantity: String
    ) {
        getSacadorControladorUserCase.collectionMissingTrash(
            usuario,
            password,
            saleFk,
            quantity,
            type,
            warehouseFk,
            originalQuantity
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseMissingTrash.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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


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

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

    fun itemShelving_updateFromSale(
        usuario: String,
        password: String,
        itemShelvingFk: String,
        visible: String
    ) {
        getSacadorControladorUserCase.itemShelving_updateFromSale(
            usuario,
            password,
            itemShelvingFk,
            visible
        )
            .enqueue(object : Callback<Unit> {
                override fun onFailure(call: Call<Unit>, t: Throwable) {
                    _responseItemShelvingUpdate.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

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

    fun parking(
        usuario: String,
        password: String,
        ticketFk: String,
        parking: String
    ) {
        getUbicadorUserCase.shelvingPark(usuario, password, ticketFk, parking)
            .enqueue(object : Callback<Any> {
                override fun onFailure(call: Call<Any>, t: Throwable) {
                    _responseParking.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

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

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

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

    fun debug_add(
        usuario: String,
        password: String,
        buyFk: String,
        message: String
    ) {
        getSacadorControladorUserCase.debug_add(usuario, password, buyFk, message)
            .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 = "")
                    }
                }
            })
    }


    fun getIdFromCode(usuario: String, password: String, code: String) {
        getSacadorControladorUserCase.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(token: String, code: String) {
        getSacadorControladorUserCase.getIdFromCodeSalix(token, 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>) {
                    if (!response.isSuccessful) {
                        _responseCode.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    } else {
                        _responseCode.value =
                            ResponseItemVO(isError = false, response = if (response.body()==null) "" else {response.body()!!})
                    }
                }
            })
    }

/*    fun collectionIncreaseQuantity(
        usuario: String,
        password: String,
        saleFk: String,
        quantity: String,callFunction:String
                                  )
    {
        getSacadorControladorUserCase.collectionIncreaseQuantity(
            usuario,
            password,
            saleFk,
            quantity,

                                                                ).enqueue(object : Callback<String>
        {
            override fun onFailure(call: Call<String>, t: Throwable)
            {
                _responseIncQuantity.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!)
                                                           )
            }

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

                if (!response.isSuccessful) )
                {
                    _responseIncQuantity.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())
                                                               )
                } else
                {
                    _responseIncQuantity.value =
                        ResponseItemVO(isError = false, response = response.body()!!)
                }
            }
        })
    }*/

    fun collectionIncreaseQuantitySalix(

        token: String,
        saleFk: String,
        quantity: String
    ) {
        getSacadorControladorUserCase.collectionIncreaseQuantitySalix(
            token,
            saleFk,
            quantity
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseIncQuantity.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                if (!response.isSuccessful) {
                    _responseIncQuantity.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            response.message()
                        )
                    )
                } else {
                    _responseIncQuantity.value =
                        ResponseItemVO(isError = false, response = response.message().toString())
                }
            }
        })
    }

    fun sendChekingPresence(
        token: String,
        workerId: String,
        message: String

    ) {
        try {
            getLoginUserCase.sendChekingPresence(token, workerId.toInt(), 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(
        usuario: String,
        password: String,
        vSaleFk: String,
        vUserFk: String,
        vTypeFk: String
    ) {
        getSacadorControladorUserCase.saleMistakeAdd(usuario, password, vSaleFk, vUserFk, vTypeFk)
            .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 mistakeType(usuario: String, password: String) {
        getSacadorControladorUserCase.mistakeType(usuario, password)
            .enqueue(object : Callback<List<MistakeTypeVO>> {
                override fun onFailure(call: Call<List<MistakeTypeVO>>, t: Throwable) {
                    val listError: ArrayList<MistakeTypeVO> = ArrayList()
                    listError.add(MistakeTypeVO("0", ""))
                    _mistakeList.value = MistakeTypeListVO(listError)
                }

                override fun onResponse(
                    call: Call<List<MistakeTypeVO>>,
                    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 sip_getExtension(
        usuario: String,
        password: String,
        workerId: String
    ) {
        getSacadorControladorUserCase.sip_getExtension(
            usuario,
            password,
            workerId
        ).enqueue(object :
            Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                _responseGetExtensionFromUserId.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                if (response.body() != "false") {
                    _responseGetExtensionFromUserId.value = ResponseItemVO(
                        isError = false,
                        response = response.body()!!,
                        errorMessage = ""
                    )
                } else {
                    _responseGetExtensionFromUserId.value = ResponseItemVO(
                        isError = true,
                        response = response.body()!!,
                        errorMessage = ""
                    )
                }
            }
        })
    }

    fun sip_getExtensionSalix(
        token: String,
        workerId: String
    ) {
        getSacadorControladorUserCase.sip_getExtensionSalix(
            token,
            workerId
        ).enqueue(object :
            Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseGetExtensionFromUserId.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onResponse(
                call: Call<Any>,
                response: Response<Any?>
            ) {
                // No se ajusta ya que la response is Successful pero el valor retornado es un error y por lo tanto response.body nulo y tira error
                /*   if (response.isSuccessful ){
                       _responseGetExtensionFromUserId.value = ResponseItemVO(
                           isError = false,
                           response = JSONObject(response.body()!!.toString()).get("extension").toString(),
                           errorMessage = "")
                   }else{
                       _responseGetExtensionFromUserId.value = ResponseItemVO(
                           isError = true,
                           response = response.message()!!,
                           errorMessage = "No se puede realizar la llamada. El usuario no tiene la extensión configurada. Solicítelo a informática.")

                   }*/
                if (response.body() != null) {
                    _responseGetExtensionFromUserId.value = ResponseItemVO(
                        isError = false,
                        response = JSONObject(response.body()!!.toString()).get("extension")
                            .toString(),
                        errorMessage = ""
                    )
                    // Se prueba el call como JSONOBJECT pero no da resultado porque el sevidor puede devolver un null directamente y salta error MAlformed
                    // Se deja Any para obtener cualquier tipo de objeto y si no es nulo se parsea a Json

                    // Per al cas que el CAll siga Any enlloc de JsonObject:
                    //var answerError=(JSONObject(response.body().toString()).get("extension").toString())
                    //var messageFromError = answerError.get("extension").toString()
                    //Log.d("VERDNATURA::","Message ext"+messageFromError)
                } else {
                    _responseGetExtensionFromUserId.value = ResponseItemVO(
                        isError = true,
                        response = "",
                        errorMessage = response.message()!!
                    )
                }
            }
        })
    }

    fun ticket_checkFullyControlled(
        usuario: String,
        password: String,
        collectionFk: String
    ) {
        getSacadorControladorUserCase.ticket_checkFullyControlled(
            usuario,
            password,
            collectionFk
        ).enqueue(object :
            Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                _responsecheckfully.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

                if (response.body() != "false") {
                    //sergio:
                    _responsecheckfully.value = ResponseItemVO(
                        isError = false,
                        response = response.body()!!,
                        errorMessage = ""
                    )
                }
            }
        })
    }
    fun collection_getUncheckedTicket(
        usuario: String,
        password: String,
        collectionFk: String
    ) {
        getSacadorControladorUserCase.collection_getUncheckedTicket(
            usuario,
            password,
            collectionFk
        ).enqueue(object :
            Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                _responseCollectionUnchecked.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

            override fun onResponse(
                call: Call<String>,
                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()!!,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), response.message()))
                }
            }
        })
    }

}
