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


import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.SilexCallback
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.paletizador.model.ExpeditionTruckSalix
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckList
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO
import retrofit2.Call
import retrofit2.Response
import java.time.LocalDate
import java.time.LocalTime

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

    private val _expeditionTruckList by lazy { MutableLiveData<ItemExpeditionTruckList>() }
    val loadExpeditionTruckList = Transformations.map(_expeditionTruckList) { Event(it) }

    private val _response by lazy { MutableLiveData<ResponseItemVO>() }
    val loadResponseExpeditionAdd = Transformations.map(_response) { Event(it) }


    @RequiresApi(Build.VERSION_CODES.O)
    fun expeditionTruckList() {
        // Tarea 5134 modificar fecha para que se vea correctamente y no con 0.00Z
      /*  salix.expeditionTruckGet(
            """{"where": { "eta": {
        "between": ["${LocalDate.now().atTime(LocalTime.MIN)}", "${LocalDate.now().atTime(LocalTime.MAX)}"]}}}"""
        ).enqueue(object : SalixCallback<List<ItemExpeditionTruckVO>>(context) {*/
            silex.expeditionTruckList().enqueue(object :
                SilexCallback<List<ItemExpeditionTruckVO>>(context) {
            override fun onError(t: Throwable) {
                val listError: ArrayList<ItemExpeditionTruckVO> = ArrayList()
                listError.add(
                    ItemExpeditionTruckVO(
                        0,
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!)
                    )
                )
                _expeditionTruckList.value = ItemExpeditionTruckList(listError)
            }

            override fun onSuccess(response: Response<List<ItemExpeditionTruckVO>>) {
                if (response.body() != null) {
                    _expeditionTruckList.value =
                        response.body()?.let { ItemExpeditionTruckList(it) }
                } else {
                    val listError: ArrayList<ItemExpeditionTruckVO> = ArrayList()
                    listError.add(
                        ItemExpeditionTruckVO(
                            0,
                            isError = true,
                            errorMessage = getMessageFromAllResponse(
                                nameofFunction(this),
                                response.message()
                            )
                        )
                    )
                    _expeditionTruckList.value = ItemExpeditionTruckList(listError)
                }
            }

        })
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun expeditionTruckAdd(vHour: String, vDescription: String) {
        salix.expeditionTrucksInsert(
            ExpeditionTruckSalix(
                eta = "${LocalDate.now()} $vHour",
                description = vDescription
            )
        ).enqueue(object : SalixCallback<Any>(context) {
            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())
                }

            }

        })
    }

}
