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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.domain.GetDayOfSaleUSerCase
import es.verdnatura.presentation.base.*
import es.verdnatura.presentation.common.Event
import es.verdnatura.presentation.common.ResponseItemVO
import es.verdnatura.presentation.view.feature.diadeventa.model.ItemShelvingSaleDate
import es.verdnatura.presentation.view.feature.diadeventa.model.ItemShelvingSaleDateList
import es.verdnatura.presentation.view.feature.paletizador.model.itemExpedetionState
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import timber.log.Timber.i

class DayOfSaleViewModel(context: Context) : BaseViewModel() {
    private val GetDayOfSaleUSerCase: GetDayOfSaleUSerCase =
        GetDayOfSaleUSerCase(context)


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


    private val _itemShelvingSaleDateList by lazy { MutableLiveData<ItemShelvingSaleDateList>() }
    val itemShelvingSaleDateList: LiveData<ItemShelvingSaleDateList>
        get() = _itemShelvingSaleDateList

    val loadItemShelvingSaleDateList = Transformations.map(_itemShelvingSaleDateList) { Event(it) }

    fun itemShelving_getSaleDate(usuario:String,password:String,vShelvingFK: String){
        GetDayOfSaleUSerCase.itemShelving_getSaleDate(usuario,password,vShelvingFK).enqueue(object :
            Callback<List<ItemShelvingSaleDate>> {
            override fun onFailure(call: Call<List<ItemShelvingSaleDate>>, t: Throwable) {
                val listError:ArrayList<ItemShelvingSaleDate> = ArrayList()
                listError.add(ItemShelvingSaleDate(isError = true,errorMessage =getMessageFromAllResponse(nameofFunction(this),t.message!!)))
                _itemShelvingSaleDateList.value = ItemShelvingSaleDateList(listError)
            }

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

                if (!response.isSuccessful){

                }
                if (response.body() != null){

                    _itemShelvingSaleDateList.value = response.body()?.let { ItemShelvingSaleDateList(it)
                    }
                    i("finalizada la lista ${response.body()}")

                }else{
                    if (!response.isSuccessful){


                    val listError:ArrayList<ItemShelvingSaleDate> = ArrayList()
                    listError.add(ItemShelvingSaleDate(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())))
                    _itemShelvingSaleDateList.value = ItemShelvingSaleDateList(listError) }
                }
            }

        })
    }



    /*fun itemShelving_getSaleDateOld(expeditionFk: String, user: String, password: String) {
        GetDayOfSaleUSerCase.itemShelving_getSaleDate(user, password, expeditionFk)
            .enqueue(object : Callback<JsonObject> {
                override fun onFailure(call: Call<JsonObject>, t: Throwable) {

                    var listProperties: MutableList<itemsExpeditionDynamics> = mutableListOf()

                    _itemexpedition.value = itemExpedetionState(
                        isError = true,
                        errorMessage = "Error en la llamada itemShelving_getSaleDate " + expeditionFk + " Respuesta:" + t.message!!, list= listProperties

                                                               )
                }

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


                    var listProperties: MutableList<itemsExpeditionDynamics> = mutableListOf()

                    if (response.body() != null) {

                        listProperties=getListFromJSON(response.body()!!.asJsonObject)

                        _itemexpedition.value = itemExpedetionState(
                            isError = false,
                            errorMessage = "Error al obtener el estado de la expedición",list = listProperties)


                    } else {
                        _itemexpedition.value = itemExpedetionState(
                            isError = true,
                            errorMessage = "Error en la llamada itemShelving_getSaleDate",list = listProperties
                                                                   )
                    }
                }

            })
    }*/


 /*   fun getListFromJSON(json: JsonObject): MutableList<itemsExpeditionDynamics> {
        val gson = Gson()
        var list = mutableListOf<itemsExpeditionDynamics>()
        var expeditionState: Map<String, Any> = gson.fromJson(json, object : TypeToken<Map<String, Any>>() {}.type)
        expeditionState.forEach {

            list.add(itemsExpeditionDynamics(key=it.key,value=it.value.toString()))

        }
        return list
    }*/

    fun parking(usuario: String,password: String,scanItem: String,parking:String){

        GetDayOfSaleUSerCase.shelvingPark(usuario,password,scanItem,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 == null){
                    _response.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))
                }else{*/
                    if (response.isSuccessful)
                        _response.value = ResponseItemVO(isError = false,response = response.message())
                    else
                        _response.value = ResponseItemVO(isError = true,errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()))

            }
        })
    }


}








