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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.google.gson.JsonObject
import es.verdnatura.MobileApplication
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
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.calidad.model.BuyerListVO
import es.verdnatura.presentation.view.feature.calidad.model.BuyerVO
import es.verdnatura.presentation.view.feature.inventario.model.InventaryListVO
import es.verdnatura.presentation.view.feature.inventario.model.InventoryParkingList
import es.verdnatura.presentation.view.feature.inventario.model.ItemInventaryVO
import es.verdnatura.presentation.view.feature.inventario.model.ItemInventoryParking
import es.verdnatura.presentation.view.feature.workermistake.model.DepartmentMistake
import es.verdnatura.presentation.view.feature.workermistake.model.ExpeditionMistakeSalix
import es.verdnatura.presentation.view.feature.workermistake.model.MistakeType
import es.verdnatura.presentation.view.feature.workermistake.model.WorkerFromMistake
import es.verdnatura.presentation.view.feature.workermistake.model.WorkerMistakeSalix
import es.verdnatura.presentation.view.feature.workermistake.model.departmentMistakeList
import es.verdnatura.presentation.view.feature.workermistake.model.workerFromMistakeList
import es.verdnatura.presentation.view.feature.workermistake.model.workerMistakeTypeList
import retrofit2.Response

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

    private val _inventaryList by lazy { MutableLiveData<InventaryListVO>() }
    val inventaryList: LiveData<InventaryListVO>
        get() = _inventaryList

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

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

    private val _buyersList by lazy { MutableLiveData<BuyerListVO>() }
    val buyersList: LiveData<BuyerListVO>
        get() = _buyersList

    private val _mistakeDepartmentList by lazy { MutableLiveData<departmentMistakeList>() }
    val mistakeDepartmentList: LiveData<departmentMistakeList>
        get() = _mistakeDepartmentList

    private val _workerFromMistakeList by lazy { MutableLiveData<workerFromMistakeList>() }
    val workerFromMistakeList: LiveData<workerFromMistakeList>
        get() = _workerFromMistakeList

    private val _mistakeWorkerList by lazy { MutableLiveData<workerMistakeTypeList>() }
    val mistakeWorkerList: LiveData<workerMistakeTypeList>
        get() = _mistakeWorkerList

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

    private val _inventoryListParking by lazy { MutableLiveData<InventoryParkingList>() }
    val inventoryListParking: LiveData<InventoryParkingList>
        get() = _inventoryListParking

    val loadInventaryParkingList = Transformations.map(_inventoryListParking) { Event(it) }

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

    val loadReponseCode = Transformations.map(_responseCode) { Event(it) }

    //sergio para obtener comprador
    fun itemShelvingBuyerGet() {
        salix.getBuyers("""{"order":"nickname"}""")
            .enqueue(object : SalixCallback<List<BuyerVO>>(context) {
                override fun onSuccess(response: Response<List<BuyerVO>>) {
                    _buyersList.value = response.body()?.let { BuyerListVO(it) }
                }

            })
    }

    val loadInventaryList = Transformations.map(_inventaryList) { Event(it) }

    fun getInventory(buyerFk: Int, warehouseFk: Int) {
        salix.itemShelvingFilterBuyer(params = arrayListOf(buyerFk, warehouseFk))
            .enqueue(object : SalixCallback<List<ItemInventaryVO>>(context) {
                override fun onSuccess(response: Response<List<ItemInventaryVO>>) {
                    _inventaryList.value = response.body()?.let { InventaryListVO(it) }
                }

            })
    }

    fun itemShelvingUpdate(
        itemShelvingFk: Long,
        params: Any,

        ) {

        salix.itemShelvingUpdate(itemShelvingFk, params)
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onSuccess(response: Response<Any>) {
                    _response.value = ResponseItemVO(isError = false, response = response.message())
                    super.onSuccess(response)
                }

            })
    }

    fun itemShelvingsReset(
        where: JsonObject,
        params: Any,

        ) {

        salix.itemShelvingsUpdate(where, params).enqueue(object : SalixCallback<Any>(context) {

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

        })
    }

    fun itemTrash(
        itemFk: Int, warehouseFk: Int, newValue: Int, isTrash: Boolean
    ) {

        salix.itemTrash(arrayListOf(itemFk, warehouseFk, newValue, isTrash).formatWithQuotes())
            .enqueue(object : SalixCallback<Any>(context) {
                override fun onSuccess(response: Response<Any>) {
                    _response.value = ResponseItemVO(isError = false, response = response.message())
                }
            })
    }

    fun departmentGetHasMistake() {
        salix.department_getHasMistake()
            .enqueue(object : SalixCallback<List<DepartmentMistake>>(context) {
                override fun onSuccess(response: Response<List<DepartmentMistake>>) {
                    _mistakeDepartmentList.value =
                        response.body()?.let { departmentMistakeList(it) }
                }
            })
    }

    fun workerGetFromHasMistake(departmentFk: Int) {
        salix.workerGetFromHasMistake(arrayListOf(departmentFk))
            .enqueue(object : SalixCallback<List<WorkerFromMistake>>(context) {
                override fun onSuccess(response: Response<List<WorkerFromMistake>>) {
                    _workerFromMistakeList.value =
                        response.body()?.let { workerFromMistakeList(it) }
                }

                override fun onError(t: Throwable) {
                    val listError: ArrayList<WorkerFromMistake> = ArrayList()
                    listError.add(
                        WorkerFromMistake(
                            isError = true, errorMessage = getMessageFromAllResponse(
                                nameofFunction(this), t.message!!
                            )
                        )
                    )
                    _workerFromMistakeList.value = workerFromMistakeList(listError)
                }

            })
    }

    fun workerMistakeTypeGet() {
        salix.workerMistakesTypes().enqueue(object : SalixCallback<List<MistakeType>>(context) {

            override fun onSuccess(response: Response<List<MistakeType>>) {
                _mistakeWorkerList.value = response.body()?.let { workerMistakeTypeList(it) }

            }

        })
    }

    fun workerMistakeAdd(
        vUserFk: Int, vTypeFk: String
    ) {
        salix.workerMistakesAdd(WorkerMistakeSalix(vUserFk, vTypeFk))
            .enqueue(object : SalixCallback<Any>(context) {})
    }

    fun expeditionMistakeAdd(
        vExpeditionFk: Int,
        vTypeFk: String,
    ) {
        salix.expeditionMistakesAdd(
            ExpeditionMistakeSalix(
                expeditionFk = vExpeditionFk,
                typeFk = vTypeFk,
                workerFk = (context as MobileApplication).userId!!
            )
        ).enqueue(object : SalixCallback<Any>(context) {
        })
    }

    fun getInventoryParking(
        vParkingFrom: String, vParkingTo: String
    ) {

        salix.getInventoryParking(vParkingFrom, vParkingTo)
            .enqueue(object : SalixCallback<List<ItemInventoryParking>>(context) {
                override fun onError(t: Throwable) {
                    val listError: ArrayList<ItemInventoryParking> = ArrayList()
                    listError.add(
                        ItemInventoryParking(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this), t.message!!
                            ),
                        )
                    )
                    _inventoryListParking.value = InventoryParkingList(listError)

                }

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

                    if (response.body() != null) {
                        _inventoryListParking.value =
                            response.body()?.let { InventoryParkingList(it) }

                    } else {
                        val listError: ArrayList<ItemInventoryParking> = ArrayList()
                        listError.add(
                            ItemInventoryParking(
                                isError = false,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this), response.message()
                                ),
                            )
                        )
                        _inventoryListParking.value = InventoryParkingList(listError)
                    }
                }

            })
    }

    fun ItemListInventoryParking_(
        vParkingFrom: String, vParkingTo: String
    ) {

        var myList = InventoryParkingList(
            listOf(
                ItemInventoryParking(
                    1,
                    31,
                    vParkingFrom,
                    vParkingTo,
                    19897,
                    "Dendrobium Galaxy",
                    510,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/dgalaxy",
                    0,
                    2
                ), ItemInventoryParking(
                    2,
                    31,
                    "F-01-1",
                    "OZR",
                    92400,
                    "Cymbidium Avalanche",
                    14,
                    37,
                    "Sergio de la Torre Nebot",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/92400",
                    1,
                    2
                ), ItemInventoryParking(
                    3,
                    31,
                    "F-01-1",
                    "OZR",
                    92400,
                    "Cymbidium Avalanche",
                    2,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/92400",
                    null,
                    2
                ), ItemInventoryParking(
                    4,
                    32,
                    "F-01-2",
                    "PLY",
                    19897,
                    "Dendrobium Galaxy",
                    520,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/dgalaxy",
                    0,
                    2
                ), ItemInventoryParking(
                    5,
                    33,
                    "F-01-3",
                    "ZLZ",
                    2912,
                    "HEL Tropical Red",
                    2,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/002912",
                    0,
                    0
                ), ItemInventoryParking(
                    6,
                    34,
                    "F-01-4",
                    "JXU",
                    25942,
                    "HEL BQ Amor (x bouquet)",
                    0,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/bqamor",
                    2,
                    0
                ), ItemInventoryParking(
                    7,
                    34,
                    "F-01-4",
                    "MUU",
                    25942,
                    "HEL BQ Amor (x bouquet)",
                    5,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/bqamor",
                    0,
                    0
                ), ItemInventoryParking(
                    8,
                    34,
                    "F-01-4",
                    "JXU",
                    50001,
                    "Monstera I",
                    130,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/50001",
                    0,
                    2
                ), ItemInventoryParking(
                    9,
                    34,
                    "F-01-4",
                    "JXU",
                    50004,
                    "Monstera IV",
                    240,
                    37,
                    "Jose Alarcan Gomex",
                    "http://verdnatura.es/vn-image-data/catalog/1600x900/50003",
                    0,
                    2
                )
            )
        )

    }

    fun expeditionMistakeTypeGet() {
        salix.getExpeditionMistakeTypes()
            .enqueue(object : SalixCallback<List<MistakeType>>(context) {

                override fun onSuccess(response: Response<List<MistakeType>>) {
                    _mistakeWorkerList.value =
                        response.body()?.let { workerMistakeTypeList(it) }
                }

            })
    }

    fun getIdFromCodeSalix(code: String) {
        salix.barcodes_toitem(code).enqueue(object : SalixCallback<String?>(context) {
            override fun onSuccess(response: Response<String?>) {
                _responseCode.value =
                    ResponseItemVO(isError = false, response = response.body()!!.toString())
            }
        })
    }
}
