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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.google.gson.JsonObject
import es.verdnatura.domain.SalixCallback
import es.verdnatura.domain.formatWithQuotes
import es.verdnatura.domain.model.Filter
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.common.ResponseItemMachineControl
import es.verdnatura.presentation.view.feature.controlvehiculo.fragment.model.DeviceId
import es.verdnatura.presentation.view.feature.login.model.NameWorker
import retrofit2.Response

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

    private val _responseUpdate by lazy { MutableLiveData<Any>() }
    val responseUpdate: LiveData<Any>
        get() = _responseUpdate

    private val _responseControl by lazy { MutableLiveData<String>() }
    val responseControl: LiveData<String>
        get() = _responseControl

    private val _responseMachine by lazy { MutableLiveData<ResponseItemMachineControl>() }
    val responseMachine: LiveData<ResponseItemMachineControl>
        get() = _responseMachine

    private val _responseGetName by lazy { MutableLiveData<NameWorker>() }
    val responseGetName: LiveData<NameWorker> = _responseGetName
    fun machineWorkerUpdate(
        plate: String
    ) {
        salix.machineWorkerUpdateInTime(hashMapOf("plate" to plate))
            .enqueue(object : SalixCallback<Any>(context) {

                override fun onSuccess(response: Response<Any>) {

                    _responseUpdate.value = response.body()

                }

            })
    }

    fun workerMachineryIsRegistered(userId: Int) {
        salix.workerMachineryIsRegistered(arrayListOf(userId))
            .enqueue(object : SalixCallback<String>(context) {
                override fun onSuccess(response: Response<String>) {
                    _responseControl.value =
                        response.body()!!
                }
            })
    }

    fun deviceProductionGetnameDevice(androidId: String) {

        salix.deviceProductionsGetName(
            Gson().toJson(
                Filter(
                    mapOf("id" to true),
                    mapOf("android_id" to mapOf("eq" to androidId))
                )
            )
        )
            .enqueue(object : SalixCallback<List<DeviceId>>(context) {

                override fun onSuccess(response: Response<List<DeviceId>>) {

                    _responseMachine.value =
                        ResponseItemMachineControl(
                            response = if (response.body()!!.isEmpty()) {
                                ""
                            } else {
                                response.body()!![0].id.toString()
                            },
                            type = "getnameDevice"
                        )

                }

            })
    }

    fun deviceProductionUserGetWorker(androidId: String) {
        salix.deviceProductionUserGetWorker(
            arrayListOf(
                androidId
            ).formatWithQuotes()
        ).enqueue(object : SalixCallback<List<JsonObject>>(context) {
            override fun onSuccess(response: Response<List<JsonObject>>) {
                _responseMachine.value =
                    ResponseItemMachineControl(
                        response = if (response.body()!!.isEmpty()) {
                            ""
                        } else {
                            (response.body()!![0].entrySet().first().value.toString()).replace(
                                "\"",
                                ""
                            )
                        },
                        type = "getWorker"
                    )
            }

        })
    }

    fun machineGetWorkerPlate(userId: Int) {
        salix.machineGetWorkerPlate(
            arrayListOf(userId)
        ).enqueue(object : SalixCallback<List<JsonObject>>(context) {
            override fun onSuccess(response: Response<List<JsonObject>>) {
                _responseMachine.value =
                    ResponseItemMachineControl(
                        response = if (response.body()!!.isEmpty()) {
                            ""
                        } else {
                            response.body()!![0].entrySet().first().value.toString()
                                .replace("\"", "")
                        },
                        type = "getWorkerPlate"
                    )
            }

        })
    }

    fun getNameWorker(
        workerId: Int
    ) {
        salix.getNameWorker(
            """{"fields": ["firstName","lastName"],"where": {"id":$workerId}}"""
        ).enqueue(object : SalixCallback<NameWorker>(context) {

            override fun onSuccess(response: Response<NameWorker>) {
                _responseGetName.value = response.body()
            }

        })

    }

}