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


import android.content.Context
import android.text.TextUtils
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.airbnb.lottie.parser.IntegerParser
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import es.verdnatura.domain.GetPaletizadoresUserCase
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.articulo.model.ItemCardVO
import es.verdnatura.presentation.view.feature.articulo.model.ItemPackingTypeList
import es.verdnatura.presentation.view.feature.paletizador.model.itemExpedetionState
//import es.verdnatura.presentation.view.feature.paletizador.model.itemExpeditionDynamicsList
import es.verdnatura.presentation.view.feature.paletizador.model.itemsExpeditionDynamics
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class ExpeditionStateViewModel(context: Context) : BaseViewModel() {
    private val GetPaletizadoresUserCase: GetPaletizadoresUserCase =
        GetPaletizadoresUserCase(context)
    val version: String = "5.0.0"

    private val _itemcard by lazy { MutableLiveData<ItemCardVO>() }
    val itemcard: LiveData<ItemCardVO>
        get() = _itemcard


    private val _itemexpeditionlist by lazy { MutableLiveData<itemExpedetionState>() }
    val itemexpeditionlist: LiveData<itemExpedetionState>
        get() = _itemexpeditionlist


    private val _itemexpedition by lazy { MutableLiveData<itemExpedetionState>() }
    val itemexpedition: LiveData<itemExpedetionState>
        get() = _itemexpedition


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

    private val _itemspackinglist by lazy { MutableLiveData<ItemPackingTypeList>() }
    val itemspackinglist: LiveData<ItemPackingTypeList>
        get() = _itemspackinglist

    private val _itemsExpeditionlist by lazy { MutableLiveData<ItemPackingTypeList>() }
    val itemsExpeditionlist: LiveData<ItemPackingTypeList>
        get() = _itemsExpeditionlist




   /* fun expedition_getState(expeditionFk: String, user: String, password: String) {
        GetPaletizadoresUserCase.expedition_getState(user, password, expeditionFk)
            .enqueue(object : Callback<itemExpedetionState> {




                override fun onFailure(call: Call<itemExpedetionState>, t: Throwable) {
                    _itemexpedition.value = itemExpedetionState(
                        isError = true,
                        errorMessage = "Error al obtener " + expeditionFk + " Respuesta:" + t.message!!
                    )
                }

                override fun onResponse(
                    call: Call<itemExpedetionState>,
                    response: Response<itemExpedetionState>
                ) {
                    if (response.body() != null) {
                        _itemexpedition.value = response.body()!!

                        Log.i(
                            "VERDNATURA:",
                            "el resultado sin JSOBJECT es ${response.body().toString()}"
                        )
                    } else {
                        _itemexpedition.value = itemExpedetionState(
                            isError = true,
                            errorMessage = "Error al obtener el estado de la expedición"
                        )
                    }
                }

            })
    }*/

    fun expedition_getStateJSON(expeditionFk: String, user: String, password: String){
        GetPaletizadoresUserCase.expedition_getStateJSON(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 = getMessageFromAllResponse(nameofFunction(this),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 = "",list = listProperties)

                        let {

                        }


                    } else {
                        _itemexpedition.value = itemExpedetionState(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message()),list = listProperties
                        )
                    }
                }

            })
    }

    /* private fun jsonToMap(json: JsonObject): Map<String, String> {
         val map: MutableMap<String, String> = linkedMapOf()
         for (key in js("Object").keys(json)) {
             map.put(key, json[key] as String)
         }
         return map
     }*/
    fun getListFromJSON(json: JsonObject): MutableList<itemsExpeditionDynamics> {
        val gson = Gson()
        var list = mutableListOf<itemsExpeditionDynamics>()

        //  val mapType = object : TypeToken<Map<String, Any>>() {}.type

        var expeditionState: Map<String, String?> =
            gson.fromJson(json, object : TypeToken<Map<String, String?>>() {}.type)
            expeditionState.forEach {

                list.add(itemsExpeditionDynamics(key=it.key, it.value?:""))

        }
        return list
    }


}








