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

import android.view.View.INVISIBLE
import android.view.View.VISIBLE
import es.verdnatura.R
import es.verdnatura.databinding.FragmentVehiclecontrolUserBinding
import es.verdnatura.domain.ConstAndValues
import es.verdnatura.domain.ConstAndValues.ANDROID_ID
import es.verdnatura.domain.ConstAndValues.TOKEN
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.loadUrl

class ControlVehiculoUsuarioFragment(
    private var vUserFK: Int
) : BaseFragment<FragmentVehiclecontrolUserBinding, ControlVehiculoViewModel>(
    ControlVehiculoViewModel::class
) {

    private var userFk = 0
    private var goBack: Boolean = false
    override fun getLayoutId(): Int = R.layout.fragment_vehiclecontrol_user

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

    override fun init() {
        binding.mainToolbar.toolbarTitle.text = getString(R.string.vehiclecontroluser)
        goBack = false
        setEvents()
        setToolBar()
        setControlVehicleUser()
        super.init()
    }

    private fun setToolBar() {

        binding.mainToolbar.toolbarSubtitle.visibility = VISIBLE
        binding.mainToolbar.toolbarIcons.visibility = VISIBLE
        binding.mainToolbar.backButton.visibility = VISIBLE
    }

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

    private fun setControlVehicleUser() {

        try {
            userFk = mobileApplication.userId!!

            if (userFk == vUserFK) {
                val androidId: String = mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID)
                viewModel.machineGetWorkerPlate(mobileApplication.userId!!)
                viewModel.deviceProductionGetnameDevice(
                    androidId
                )
                viewModel.deviceProductionUserGetWorker(
                    androidId
                )
            } else {
                userFk = vUserFK
                binding.userInformation.visibility = INVISIBLE
                binding.nameVehiclecontrol.visibility = INVISIBLE
                binding.userImage.setOnClickListener { binding.mainToolbar.backButton.performClick() }
                viewModel.machineGetWorkerPlate(mobileApplication.userId!!)
                viewModel.getNameWorker(userFk)
            }

            binding.userImage.loadUrl(
                mobileApplication.dataStoreApp.readDataStoreKey<String>(
                    ConstAndValues.BASEURLSALIX
                ) + "/api/Images/user/160x160/$userFk/download?access_token=${
                    mobileApplication.dataStoreApp.readDataStoreKey<String>(
                        TOKEN
                    )
                }"
            )
        } catch (ex: Exception) {
            ma.messageWithSound(
                message = getString(R.string.errorUserId),
                isError = true,
                isToasted = true,
                isPlayed = true
            )
        }

    }

    private fun setEvents() {
        binding.mainToolbar.backButton.setOnClickListener {
            ma.onMyBackPressed()
        }

    }

    override fun observeViewModel() {

        with(viewModel) {

            responseMachine.observe(viewLifecycleOwner) {

                if (it.response == "false") {
                    if (it.type == "getWorker") {
                        binding.assignedDevicename.text = getString(R.string.noAssigned)
                    }
                } else {

                    when (it.type) {
                        "getnameDevice" -> binding.deviceName.text = it.response
                        "getWorker" -> binding.assignedDevicename.text = it.response

                        "getWorkerPlate" -> binding.registeredVehiclecontrol.text = it.response
                    }

                }

            }

            responseGetName.observe(viewLifecycleOwner) {
                binding.nameVehiclecontrol.text = getString(
                    R.string.txtName, it.firstName, it.lastName
                )
                binding.nameVehiclecontrol.visibility = VISIBLE

            }

        }

    }

}
