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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.domain.userCases.GetItemFromBarcodeUseCase
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.ResponseItemVO
import es.verdnatura.presentation.view.feature.collection.SalixSaleQuantity
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 getItemFromBarcodeUseCase = GetItemFromBarcodeUseCase(salix)

    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<Int?>() }
    val responseCode: LiveData<Int?>
        get() = _responseCode

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

    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 _responseHasSaleRerserved by lazy { MutableLiveData<Int>() }
    val responseHasSaleRerserved: LiveData<Int> = _responseHasSaleRerserved

    val loadResponseHasSaleReserved: LiveData<Event<Int>> =
        _responseHasSaleRerserved.map { Event(it) }

    private val _responseSaleGroupAdd by lazy { MutableLiveData<ResponseItemVO>() }
    val responseSaleGroupAdd: LiveData<ResponseItemVO>
        get() = _responseSaleGroupAdd
    val loadResponseSaleGroupAdd: LiveData<Event<ResponseItemVO>> =
        _responseSaleGroupAdd.map { Event(it) }

    private val _responseSaleGroupAdded by lazy { MutableLiveData<Int>() }
    val responseSaleGroupAdded: LiveData<Int>
        get() = _responseSaleGroupAdded
    val loadResponseSaleGropAdded: LiveData<Event<Int>> =
        _responseSaleGroupAdded.map { Event(it) }

    private val _responseReserveSectorCollection by lazy { MutableLiveData<ResponseItemVO>() }
    val responseReserveSectorCollection: LiveData<ResponseItemVO>
        get() = _responseReserveSectorCollection
    val loadResponseReserveSectorCollection: LiveData<Event<ResponseItemVO>> =
        _responseReserveSectorCollection.map { Event(it) }

    private val _responseReserveSaleGroup by lazy { MutableLiveData<ResponseItemVO>() }
    val responseReserveSaleGroup: LiveData<ResponseItemVO> = _responseReserveSaleGroup
    val loadResponseReserveSaleGroup: LiveData<Event<ResponseItemVO>> =
        _responseReserveSaleGroup.map { Event(it) }

    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
    ) {
        salix.setParking(arrayListOf(ticketFk, parking).formatWithQuotes())
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onSuccess(response: Response<Any>) {
                    _response.value = ResponseItemVO(isError = false, response = response.message())
                }

                override fun onError(t: Throwable) {
                    _response.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }
            })
    }

    fun itemPlacementSupplyAiming(
        shelvingFk: String, quantity: Int, itemFk: Int
    ) {
        salix.itemPlacementSupplyAiming(
            arrayListOf(
                shelvingFk, quantity, itemFk
            ).formatWithQuotes()
        ).enqueue(object : SalixCallback<List<PlacementSupplyVO>>(context) {
            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)
                }
            }

            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)
            }
        })
    }

    fun itemShelvingSaleAdd(
        itemShelvingFk: Int, saleFk: Int, quantity: Int
    ) {
        salix.itemShelvingSaleAdd(
            arrayListOf(
                itemShelvingFk, saleFk, quantity
            ).formatWithQuotes()
        ).enqueue(object : SalixCallback<Any>(context) {
            override fun onResponse(call: Call<Any>, response: Response<Any>) {
                _responseItemShelvingSaleSupplyAdd.value =
                    ResponseItemVO(isError = false, response = response.message())
            }

            override fun onError(t: Throwable) {
                _responseItemShelvingSaleSupplyAdd.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.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) {
        getItemFromBarcodeUseCase.execute(code)
            .enqueue(object : SalixCallback<Int?>(context) {

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

                    _responseCode.value = response.body()

                }
            })
    }

    fun collectionIncreaseQuantity(
        saleFk: Int, quantity: Int
    ) {
        salix.collectionIncreaseQuantitySalix(
            SalixSaleQuantity(
                saleFk, quantity
            )
        ).enqueue(object : SalixCallback<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 ticketPrintLabelPrevious(ticketFk: Int) {
        salix.ticketPrintLabelPrevious(arrayListOf(ticketFk))
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onSuccess(response: Response<Any>) {
                    _response.value =
                        ResponseItemVO(isError = false, response = response.message()!!)
                }

            })

    }

    fun sectorCollectionGet() {
        salix.sectorCollectionGet(params = null)
            .enqueue(object : SalixCallback<List<CollectionVO>>(context) {
                override fun onSuccess(response: Response<List<CollectionVO>>) {
                    _collectionList.value =
                        response.body()?.let { CollectionListVO(it.toDateFormat(context)) }

                }
            })
    }

    fun hasSalesReserved(sectorCollectionFk: Int) {
        salix.hasSalesReserved(sectorCollectionFk)
            .enqueue(object : SalixCallback<String>(context) {
                override fun onSuccess(response: Response<String>) {
                    _responseHasSaleRerserved.value = response.body()!!.toInt()
                }
            })
    }

    fun sectorCollection_new(
        sectorFk: Int
    ) {
        salix.sectorCollectionNew(sectorFk).enqueue(object : SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _response.value = ResponseItemVO(
                    isError = false, errorMessage = "" + getMessageFromAllResponse(
                        nameofFunction(this), response.message()
                    )
                )

            }
        })
    }

    fun sectorCollectionGetSale(
        sectorCollectionFK: Int, sectorFk: Int
    ) {

        salix.sectorCollectionGetSale(sectorFk = sectorFk, sectorCollectionFk = sectorCollectionFK)
            .enqueue(object : SalixCallback<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 sectorCollectionSaleGroupAdd(
        vSaleGroupFk: Int, vSectorCollectionFk: Int,
    ) {
        salix.sectorCollectionSaleGroupAdd(
            arrayListOf(
                vSaleGroupFk, vSectorCollectionFk
            ).formatWithQuotes()
        ).enqueue(object : SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _responseSaleGroupAdd.value = ResponseItemVO(
                    isError = false, errorMessage = "" + getMessageFromAllResponse(
                        nameofFunction(this), response.message()
                    )
                )
                _responseSaleGroupAdded.value = vSaleGroupFk
            }

            override fun onError(t: Throwable) {
                _response.value = ResponseItemVO(
                    isError = true, errorMessage = getMessageFromAllResponse(
                        nameofFunction(this), t.message!!
                    )
                )

            }
        })
    }

    fun getReserveBySectorCollection(
        vSectorCollectionFk: Int
    ) {
        salix.getReserveBySectorCollection(
            params = arrayListOf(
                vSectorCollectionFk
            ).formatWithQuotes()
        ).enqueue(object : SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _responseReserveSectorCollection.value = ResponseItemVO(
                    isError = false, errorMessage = "" + getMessageFromAllResponse(
                        nameofFunction(this), response.message()
                    )
                )
            }

            override fun onError(t: Throwable) {
                _responseReserveSectorCollection.value = ResponseItemVO(
                    isError = true, errorMessage = getMessageFromAllResponse(
                        nameofFunction(this), t.message!!
                    )
                )

            }
        })
    }

    fun getReserveBySaleGroup(
        saleGroup: Int
    ) {
        salix.getReserveBySaleGroup(
            params = arrayListOf(
                saleGroup
            ).formatWithQuotes()
        ).enqueue(object : SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _responseReserveSectorCollection.value = ResponseItemVO(
                    isError = false, errorMessage = "" + getMessageFromAllResponse(
                        nameofFunction(this), response.message()
                    )
                )
            }

            override fun onError(t: Throwable) {
                _responseReserveSectorCollection.value = ResponseItemVO(
                    isError = true, errorMessage = getMessageFromAllResponse(
                        nameofFunction(this), t.message!!
                    )
                )

            }
        })
    }

    fun saleTrackingAddPrevOK(
        sectorCollectionFk: Int
    ) {
        salix.saleTrackingAddPrevOK(arrayListOf(sectorCollectionFk))
            .enqueue(object : SalixCallback<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 saleTrackingUpdateIsChecked(
        vSaleFk: Int, vIsChecked: Boolean, vIsScanned: Boolean?
    ) {
        salix.saleTrackingUpdateIsChecked(
            arrayListOf(
                vSaleFk, vIsChecked, vIsScanned
            ).formatWithQuotes()
        ).enqueue(object : SalixCallback<Any>(context) {
            override fun onSuccess(response: Response<Any>) {
                _responseSaleupdateIsChecked.value = ResponseItemVO(
                    isError = false, errorMessage = "" + getMessageFromAllResponse(
                        nameofFunction(this), response.message()
                    )
                )
            }

            override fun onError(t: Throwable) {
                _responseSaleupdateIsChecked.value = ResponseItemVO(
                    isError = true, errorMessage = getMessageFromAllResponse(
                        nameofFunction(this), t.message!!
                    )
                )

            }
        })
    }

    fun itemShelving_updateFromSale(
        saleFk: Int
    ) {

        salix.itemShelvingUpdateFromSale(hashMapOf("saleFk" to saleFk))
            .enqueue(object : SalixCallback<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())
                    }
                }
            })
        }*/

}
