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

import android.content.Context
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
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.Event
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.reubication.model.Reubication
import es.verdnatura.presentation.view.feature.reubication.model.ReubicationList
import es.verdnatura.presentation.view.feature.ubicador.model.ItemUbicadorListVO
import es.verdnatura.presentation.view.feature.ubicador.model.ItemUbicadorVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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


    private val getUbicadorUserCase: GetUbicadorUserCase = GetUbicadorUserCase(context)

    private val _shelvingList by lazy { MutableLiveData<ItemUbicadorListVO>() }

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

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


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


    val loadShelvingList = Transformations.map(_shelvingList) { Event(it) }


    private val _reubicationList by lazy { MutableLiveData<ReubicationList>() }
    val loadReubicationList = Transformations.map(_reubicationList) { Event(it) }



    fun itemShelvingList(
        usuario: String,
        password: String,
        vShelvingFk: String
    ) {
        getUbicadorUserCase.itemShelvingList(usuario, password, vShelvingFk.uppercase())
            .enqueue(object :
                Callback<List<ItemUbicadorVO>> {
                override fun onFailure(call: Call<List<ItemUbicadorVO>>, t: Throwable) {
                    val listError: ArrayList<ItemUbicadorVO> = ArrayList()
                    listError.add(
                        ItemUbicadorVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!
                            )
                        )
                    )
                    _shelvingList.value = ItemUbicadorListVO(listError)
                }

                override fun onResponse(
                    call: Call<List<ItemUbicadorVO>>,
                    response: Response<List<ItemUbicadorVO>>
                ) {
                    if (response.body() != null) {
                        _shelvingList.value = response.body()?.let { ItemUbicadorListVO(it) }
                    } else {
                        val listError: ArrayList<ItemUbicadorVO> = ArrayList()
                        listError.add(
                            ItemUbicadorVO(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this),
                                    response.message()
                                )
                            )
                        )
                        _shelvingList.value = ItemUbicadorListVO(listError)
                    }
                }

            })
    }

    fun shelvingPriorityUpdate(
        usuario: String,
        password: String,
        priority: String,
        shelving: String

    ) {
        getUbicadorUserCase.shelvingPriorityUpdate(usuario, password, priority, shelving)
            .enqueue(object : Callback<Any> {
                override fun onFailure(call: Call<Any>, t: Throwable) {
                    _responsepriority.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

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

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

                }

            })
    }


    fun itemShelvingMake(
        usuario: String,
        password: String,
        shelving: String,
        item: String,
        deep: String,
        quantity: String,
        packing: String,
        warehouse: String,
        level: String,
        grouping: String
    ) {
        getUbicadorUserCase.itemShelvingMake(
            usuario,
            password,
            shelving,
            item,
            quantity,
            packing,
            warehouse,
            grouping
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseMake.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

            }

        })
    }

    fun itemShelvingMakeEdit(
        usuario: String,
        password: String,
        shelving: String,
        item: String,
        deep: String,
        quantity: String,
        packing: String,
        warehouse: String,
        level: String,
        itemShelvingFk: String,
        grouping: String
    ) {
        getUbicadorUserCase.itemShelvingMakeEdit(
            usuario,
            password,
            shelving,
            item,
            deep,
            quantity,
            packing,
            warehouse,
            level,
            itemShelvingFk,
            grouping
        ).enqueue(object : Callback<Any> {
            override fun onFailure(call: Call<Any>, t: Throwable) {
                _responseMake.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                )
            }

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

            }

        })
    }

    fun shelvingPark(
        usuario: String,
        password: String,
        shelving: String,
        parking: String
    ) {
        getUbicadorUserCase.shelvingPark(usuario, password, shelving, parking)
            .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 shelvingChange(
        usuario: String,
        password: String,
        origen: String,
        destino: String
    ) {
        getUbicadorUserCase.shelvingChange(usuario, password, origen, destino)
            .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 itemShelving_merge(
        usuario: String,
        password: String,
        vShelf: String,
        vShelvingFk: String

    ) {
        getUbicadorUserCase.itemShelving_merge(usuario, password, vShelf, vShelvingFk)
            .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 itemShelving_return(
        usuario: String,
        password: String,
        shelvingFk: String,

        ) {
        getUbicadorUserCase.itemShelving_return(usuario, password, shelvingFk)
            .enqueue(object : Callback<ArrayList<Reubication>> {
                override fun onFailure(call: Call<ArrayList<Reubication>>, t: Throwable) {
                    val listError: ArrayList<Reubication> = ArrayList()
                    listError.add(
                        Reubication(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                t.message!!,

                            )
                        )
                    )
                    _reubicationList.value = ReubicationList(listError)
                }

                override fun onResponse(
                    call: Call<ArrayList<Reubication>>,
                    response: Response<ArrayList<Reubication>>
                ) {
                    if (response.body()==null) {
                        val listError: ArrayList<Reubication> = ArrayList()
                        listError.add(
                            Reubication(
                                isError = true,
                                errorMessage = getMessageFromAllResponse(
                                    nameofFunction(this),
                                    response.message()
                                )
                            )
                        )
                        _reubicationList.value = ReubicationList(listError)
                    } else {

                        _reubicationList.value = response.body()?.let { ReubicationList(it) }
                      /*  for (item in response.body()!!){
                            Log.d("VERDNATURA","El item es "+item.itemFk+":"+item.shelvingFk)
                        }*/
                    }
                }
            })
    }

    fun clearShelvingList(
        usuario: String,
        password: String,
        shelvingFk: String

    ) {
        getUbicadorUserCase.clearShelvingList(usuario, password, shelvingFk)
            .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 itemShelvingTransfer(
        usuario: String,
        password: String,
        itemFk: String,
        shelvingFk: String

    ) {
        getUbicadorUserCase.itemShelvingTransfer(usuario, password, itemFk, shelvingFk)
            .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 itemShelvingDelete(usuario: String, password: String, itemFk: String) {
        getUbicadorUserCase.itemShelvingDelete(usuario, password, itemFk)
            .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())
                    }
                }
            })
    }

}
