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

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import es.verdnatura.domain.GetVehicleControlTimeUserCase
import es.verdnatura.presentation.base.BaseViewModel
import es.verdnatura.presentation.base.getMessageFromAllResponse
import es.verdnatura.presentation.base.nameofFunction
import es.verdnatura.presentation.common.ResponseItemMachineControl
import es.verdnatura.presentation.common.ResponseItemVO
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

    private val getVehicleControlTimeUserCase: GetVehicleControlTimeUserCase =
        GetVehicleControlTimeUserCase(context)

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

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

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

    fun machineWorker_add(
        usuario: String,
        password: String,
        PlateNumber: String
    ) {
        getVehicleControlTimeUserCase.machineWorker_add(usuario, password, PlateNumber)
            .enqueue(object : Callback<Boolean> {
                override fun onFailure(call: Call<Boolean>, t: Throwable) {
                    _responseinsert.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!)
                    )
                }

                override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                    //sergio_devuelve true o false
                   if (response.isSuccessful){
                       if (response.body().toString() == "false") {
                           _responseinsert.value = ResponseItemVO(
                               isError = true,
                               errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())
                           )
                       } else {

                           _responseinsert.value =
                               ResponseItemVO(isError = false, response = response.body()!!.toString())

                       }
                   }else{
                       _responseinsert.value = ResponseItemVO(
                           isError = true,
                           errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())
                       )
                   }

                }

            })
    }

    fun machineWorker_update(
        usuario: String,
        password: String,
        PlateNumber: String
    ) {
        getVehicleControlTimeUserCase.machineWorker_update(usuario, password, PlateNumber)
            .enqueue(object : Callback<Boolean> {
                override fun onFailure(call: Call<Boolean>, t: Throwable) {
                    _responseinsert.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!)
                    )
                }

                override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                    //sergio devuelve true o false
                    if (response.isSuccessful){

                    if (response.body().toString() == "false") {
                        _responseinsert.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())
                        )
                    } else {

                        _responseinsert.value =
                            ResponseItemVO(isError = false, response = response.body()!!.toString())

                    }

                }else{
                        _responseinsert.value = ResponseItemVO(
                            isError = true,
                            errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())
                        )
                }
                }

            })
    }

    fun workerMachinery_isRegistered(usuario: String, password: String) {
        getVehicleControlTimeUserCase.workerMachinery_isRegistered(
            usuario,
            password
        ).enqueue(object : Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                _responsecontrol.value = ResponseItemVO(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!)
                )
            }

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

                   if (!response.isSuccessful) {
                    _responsecontrol.value = ResponseItemVO(
                        isError = true,
                        errorMessage = getMessageFromAllResponse(nameofFunction(this),response.message())
                    )
                } else {
                    _responsecontrol.value =
                        ResponseItemVO(isError = false, response = response.body()!!)

            }
            }


        })
    }



    fun deviceProduction_getnameDevice(usuario: String, password: String, android_id: String) {
        getVehicleControlTimeUserCase.deviceProduction_getnameDevice(
            usuario,
            password,
            android_id
        ).enqueue(object : Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                _responsemachine.value = ResponseItemMachineControl(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!),
                    type = ""
                )
            }

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

                    _responsemachine.value =
                        ResponseItemMachineControl(
                            isError = false,
                            response = response.body()!!,
                            type = "getnameDevice"
                        )
                }

            }

        })
    }

    fun deviceProductionUser_getWorker(usuario: String, password: String, userFk: String) {
        getVehicleControlTimeUserCase.deviceProductionUser_getWorker(
            usuario,
            password,
            userFk
        ).enqueue(object : Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                _responsemachine.value = ResponseItemMachineControl(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!),
                    type = ""
                )
            }

            override fun onResponse(call: Call<String>, response: Response<String>) {
                if (response.body() != null) {
                    /*  _responsemachine.value = ResponseItemMachineControl(
                          isError = true,
                          errorMessage = "Error al comprobar usuario del dispositivo",
                          type = ""
                      )*/

                    _responsemachine.value =
                        ResponseItemMachineControl(
                            isError = false,
                            response = response.body()!!,
                            type = "getWorker"
                        )}

            }

        })
    }



    fun machine_getWorkerPlate(usuario: String, password: String) {
        getVehicleControlTimeUserCase.machine_getWorkerPlate(
            usuario,
            password
        ).enqueue(object : Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {


                _responsemachine.value = ResponseItemMachineControl(
                    isError = true,
                    errorMessage = getMessageFromAllResponse(nameofFunction(this),t.message!!),
                    type = ""
                )
            }

            override fun onResponse(call: Call<String>, response: Response<String>) {
                //sergio: devuelve en el body
                if (response.body() == null) {
                    ResponseItemMachineControl(
                        isError = false,
                        response = response.body()!!,
                        type = "getWorkerPlate"
                    )

                } else {
                    _responsemachine.value =
                        ResponseItemMachineControl(
                            isError = false,
                            response = response.body()!!,
                            type = "getWorkerPlate"
                        )
                }

            }

        })
    }

}