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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.R
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.ResponseItemVO
import es.verdnatura.presentation.view.feature.collection.SalixSaleQuantity
import es.verdnatura.presentation.view.feature.collection.listSaleSalix
import es.verdnatura.presentation.view.feature.collection.saleSalix
import es.verdnatura.presentation.view.feature.presacador.model.PreSacadorItemVO
import es.verdnatura.presentation.view.feature.sacador.mapper.toDateFormat
import es.verdnatura.presentation.view.feature.sacador.model.CollectionListVO
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementSupplyListVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementSupplyVO
import retrofit2.Call
import retrofit2.Response

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

    /*private val contextApp=context
    private val getPreSacadorUseCase: GetPreSacadorUseCase = GetPreSacadorUseCase(context)
    private val getSacadorControladorUserCase: GetSacadorControladorUserCase =
        GetSacadorControladorUserCase(context)
    private val getUbicadorUserCase: GetUbicadorUserCase = GetUbicadorUserCase(context)
    private val getLoginUserCase: GetLoginUserCase = GetLoginUserCase(context)*/

    private val _salesList by lazy { MutableLiveData<List<PreSacadorItemVO>>() }
    val salesList: LiveData<List<PreSacadorItemVO>>
        get() = _salesList

    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 _responseSaleTrackingUpdate by lazy { MutableLiveData<ResponseItemVO>() }
    val responseSaleTrackingUpdate: LiveData<ResponseItemVO>
        get() = _responseSaleTrackingUpdate

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


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

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

    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 _responseSalixMessage by lazy { MutableLiveData<ResponseItemVO>() }
    val responseSalixMessage: LiveData<ResponseItemVO>
        get() = _responseSalixMessage

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

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

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

    //sergio: previa nuevo
    private val _collectionList by lazy { MutableLiveData<CollectionListVO>() }
    val collectionList: LiveData<CollectionListVO>
        get() = _collectionList

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

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

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

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

    fun parking(
        ticketFk: String,
        parking: String
    ) {
        silex.shelvingPark(ticketFk, parking)
            .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 itemPlacementSupplyAiming(
        shelvingFk: String,
        quantity: Int,
        itemFk: Int
    ) {
        silex.itemPlacementSupplyAiming(
            shelvingFk,
            quantity,
            itemFk
        ).enqueue(object :
            SilexCallback<List<PlacementSupplyVO>>(context) {
            override fun onError(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 onSuccess(response: Response<List<PlacementSupplyVO>>) {
                if (response.body() != null) {
                    _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(
        itemShelvingFk: Int,
        saleFk: Int,
        quantity: Int
    ) {
        silex.itemShelvingSaleSupplyAdd(
            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 onResponse(call: Call<Any>, response: Response<Any>) {
                _responseItemShelvingSaleSupplyAdd.value =
                    ResponseItemVO(isError = false, response = response.message())
            }
        })
    }


    fun saleTrackingReplace(
        saleFk: String,
        vOriginalQuantity: String,
        vStateFk: String,
        vIsChecked: String,
        vBuyFk: String
    ) {

        silex.saleTrackingReplace(
            saleFk,
            vOriginalQuantity,
            vStateFk,
            vIsChecked,
            vBuyFk
        ).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(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?>) {

                    //sergio: espera item en el body
                    if (response.body() == null) {
                        _responseCode.value = ResponseItemVO(
                            isError = true,
                            errorMessage = context.getString(R.string.itemNotValid)
                        )

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

    fun saleTrackingDel(saleFk: String) {
        silex.saleTrackingDel(saleFk)
            .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 collectionIncreaseQuantity(
        saleFk: Int,
        quantity: Int
    ) {
        salix.collectionIncreaseQuantitySalix(
            SalixSaleQuantity(
                saleFk,
                quantity
            )
        ).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>) {
                _responseIncQuantity.value =
                    ResponseItemVO(isError = false, response = response.message())
            }
        })
    }



    fun ticket_printLabelPrevious(ticketFk: Int) {

        silex.ticket_printLabelPrevious(ticketFk).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 sectorCollection_get() {

        silex.sectorCollection_get().enqueue(object :
            SilexCallback<List<CollectionVO>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<CollectionVO> = ArrayList()
                listError.add(
                    CollectionVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(
                            nameofFunction(this),
                            t.message!!
                        )
                    )
                )
                _collectionList.value = CollectionListVO(listError)
            }


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

                if (response.body() != null) {
                    _collectionList.value =
                        response.body()?.let { CollectionListVO(it.toDateFormat(context)) }
                } else {
                    val listError: ArrayList<CollectionVO> = ArrayList()
                    listError.add(
                        CollectionVO(
                            0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _collectionList.value = CollectionListVO(listError)

                }
            }

        })
    }

    fun sectorCollection_new(
        sectorFk: Int
    ) {
        silex.sectorCollection_new(sectorFk).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 sectorCollection_getSale(
        sectorCollectionFK: Int,
        vSectorFk: Int
    ) {
        silex.sectorCollection_getSale(
            sectorCollectionFK,
            vSectorFk.toString()
        )
            .enqueue(object :
                SilexCallback<List<PreSacadorItemVO>>(context) {
                override fun onError(t: Throwable) {
                    _salesList.value = listOf()
                }

                override fun onSuccess(response: Response<List<PreSacadorItemVO>>) {
                    if (response.body() != null) {
                        _salesList.value = response.body()?.let { it }

                    } else {
                        _salesList.value = listOf()
                    }
                }
            })
    }

    fun sectorCollectionSaleGroup_add(

        vSaleGroupFk: Int,
        vSectorCollectionFk: Int
    ) {
        silex.sectorCollectionSaleGroup_add(
            vSaleGroupFk,
            vSectorCollectionFk
        ).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>) {
                    _responseSaleGroupAdd.value = ResponseItemVO(
                        isError = false,
                        errorMessage = "" + getMessageFromAllResponse(
                            nameofFunction(this), response.message()
                        )
                    )
                }
            })
    }

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

                }

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

    fun saleTracking_updateIsChecked(
        vSaleFk: Int,
        vIsChecked: Boolean,
        vIsScanned: Boolean?
    ) {
        silex.saleTracking_updateIsChecked(vSaleFk, vIsChecked, vIsScanned)
            .enqueue(
                object :
                    SilexCallback<Any>(context) {
                    override fun onError(t: Throwable) {
                        _responseSaleupdateIsChecked.value =
                            ResponseItemVO(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this),
                                    t.message!!
                                )
                            )


                    }

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


    fun itemShelving_updateFromSale(
        saleFk: Int
    ) {
        silex.itemShelving_updateFromSale(
            saleFk
        )
            .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 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 onResponse(call: Call<Any>, response: Response<Any>) {
                //segio: 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())
                }
            }
        })
    }

}
