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

import android.view.View.*
import androidx.lifecycle.Observer
import es.verdnatura.R
import es.verdnatura.databinding.FragmentVehiclecontrolUserBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.loadUrl
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.feature.main.activity.MainActivity


class ControlVehiculoUsuarioFragment(
    var vUserFK: String = ""
) : BaseFragment<FragmentVehiclecontrolUserBinding, ControlVehiculoViewModel>(
    ControlVehiculoViewModel::class
) {

    private var userFk = ""
    private var goBack: Boolean = false
    override fun getLayoutId(): Int = R.layout.fragment_vehiclecontrol_user
    //private lateinit var customDialog: CustomDialog

    companion object {
        fun newInstance(vUserFK: String) = ControlVehiculoUsuarioFragment(vUserFK)
    }

    override fun init() {
        binding.mainToolbar.toolbarTitle.text = getString(R.string.vehiclecontroluser)
       // customDialog = CustomDialog(requireContext())
        goBack = false
        binding.splashProgress.visibility = GONE
        setEvents()
        setControlVehicleUser()
        super.init()
    }

    override fun onPause() {
        goBack = true
        super.onPause()
    }

    private fun setControlVehicleUser() {

        userFk=getData(USERFK)

        if (userFk.equals(vUserFK)) {

            binding.splashProgress.visibility = VISIBLE
            viewModel.machine_getWorkerPlate(getData(USER),getData(PASSWORD))
            viewModel.deviceProduction_getnameDevice(getData(USER),getData(PASSWORD), getData(ANDROID_ID))
            viewModel.deviceProductionUser_getWorker(getData(USER),getData(PASSWORD), getData(ANDROID_ID))

        } else {
            userFk = vUserFK

            binding.userInformation.visibility = INVISIBLE
            binding.nameVehiclecontrol.visibility = INVISIBLE
            binding.userInformation.setOnClickListener({binding.mainToolbar.backButton.performClick()})

        }
        binding.userImage.loadUrl("https://salix.verdnatura.es/api/Images/user/160x160/$userFk/download?access_token=${getData(TOKEN)}")
        viewModel.machine_getWorkerPlate(getData(USER),getData(PASSWORD))
        binding.nameVehiclecontrol.text = getData(USER)
        binding.registeredVehiclecontrol.text = ""
        binding.deviceName.text = ""
        binding.assignedDevicename.text = ""

    }


    private fun setEvents() {
        binding.mainToolbar.backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }


    }


    override fun observeViewModel() {

        with(viewModel) {
            binding.splashProgress.visibility = GONE


            responsemachine.observe(viewLifecycleOwner, Observer {
                binding.splashProgress.visibility = GONE
                if (it.isError) {
                    ma.messageWithSound(it.errorMessage,true,false)

                } else {
                    //sergio: se modifica el response = null. En casi todas las llamadas se utilizaba para ver si había error.
                    // Ahora ya no.

                    if (it.response == "false") {

                    } else {


                        if (it.type.equals("getnameDevice")) {
                            binding.deviceName.text = it.response
                        }
                        if (it.type.equals("getWorker")) {
                            binding.assignedDevicename.text = it.response
                        }
                        if (it.type.equals("getWorkerPlate")) {
                            binding.registeredVehiclecontrol.text = it.response
                        }
                    }

                }

            })

        }

    }
}
