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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetPreSacadorUseCase
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.presacador.model.PreSacadorItemVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementSupplyListVO
import es.verdnatura.presentation.view.feature.sacador.model.PlacementSupplyVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class ReposicionViewModel(context: Context) : BaseViewModel() {
    private val getPreSacadorUseCase: GetPreSacadorUseCase = GetPreSacadorUseCase(context)
    private val getUbicadorUserCase: GetUbicadorUserCase = GetUbicadorUserCase(context)
    private val getSacadorControladorUserCase: GetSacadorControladorUserCase =
        GetSacadorControladorUserCase(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 _responseClose by lazy { MutableLiveData<ResponseItemVO>() }
    val responseClose: LiveData<ResponseItemVO>
        get() = _responseClose

    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

    fun itemPlacementSupplyGetOrder(
        usuario: String,
        password: String,
        sectorFk: String

    ) {
        getPreSacadorUseCase.itemPlacementSupplyGetOrder(usuario, password, sectorFk)
            .enqueue(object :
                Callback<List<PreSacadorItemVO>> {
                override fun onFailure(call: Call<List<PreSacadorItemVO>>, t: Throwable) {
                    _salesList.value = listOf()
                }

                override fun onResponse(
                    call: Call<List<PreSacadorItemVO>>,
                    response: Response<List<PreSacadorItemVO>>
                ) {
                    if (response.body() != null) {
                        _salesList.value = response.body()?.let { it }
                    } else {
                        _salesList.value = listOf()
                    }
                }
            })
    }

    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) {
                    _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 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 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:espera item en el body
                    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 itemShelvingPlacementSupplyAdd(
        usuario: String,
        password: String,
        itemShelvingFk: String,
        itemPlacementSupplyFk: String,
        quantity: String

    ) {
        getSacadorControladorUserCase.itemShelvingPlacementSupplyAdd(
            usuario,
            password,
            itemShelvingFk,
            itemPlacementSupplyFk,
            quantity
        ).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 itemPlacementSupplyCloseOrder(
        usuario: String,
        password: String,
        id: String,
        quantity: String
    ) {
        getPreSacadorUseCase.itemPlacementSupplyCloseOrder(usuario, password, id, quantity)
            .enqueue(object : Callback<Any> {
                override fun onFailure(call: Call<Any>, t: Throwable) {
                    _responseClose.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                }

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

}