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

import android.content.Context
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Observer
import es.verdnatura.R
import es.verdnatura.databinding.FragmentControladorBinding
import es.verdnatura.domain.ConstAndValues
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnCollectionSelectedListener
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class ControladorFragment :
    BaseFragment<FragmentControladorBinding, ControladorViewModel>(ControladorViewModel::class) {

    private var goBack: Boolean = false
    private var onCollectionSelectedListener: OnCollectionSelectedListener? = null
    override fun getLayoutId(): Int = R.layout.fragment_controlador
    //private lateinit var customDialog: CustomDialog

    companion object {
        fun newInstance() = ControladorFragment()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnCollectionSelectedListener) onCollectionSelectedListener = context
    }

    override fun init() {

        binding.splashProgress.visibility = View.GONE
      //  customDialog = CustomDialog(requireContext())
        binding.mainToolbar.toolbarTitle.text = getString(R.string.controlticket)
        setEvents()
        ma.hideBottomNavigation(View.VISIBLE)
        super.init()
    }

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


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

        binding.scanInput.requestFocus()
        binding.scanInput.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                // d("Action id "+actionId)
                goBack = false
                if (!binding.scanInput.text.isNullOrEmpty()) {
                    binding.splashProgress.visibility = View.VISIBLE


                    //sergio: para ver si ha marcado dos veces el mismo ticket. El laser falla
                    binding.scanInput.setText(textScanned_filterDouble(binding.scanInput.text!!.toString()))

                    viewModel.collectionTicketGet(
                        usuario = getData(USER),
                        password = getData(PASSWORD),
                        sectorFk = getData(SECTORFK),
                        collectionFk = binding.scanInput.text.toString(),
                        type = ConstAndValues.CONTROLADOR
                    )

                }
                binding.scanInput.setText("")
                ma.hideKeyboard(binding.scanInput)
                return@setOnEditorActionListener true
            }
            false
        }
    }

    override fun observeViewModel() {
        with(viewModel) {
            collectionTicketList.observe(viewLifecycleOwner, Observer {
                binding.splashProgress.visibility = View.GONE
                if (it.isError) {
                    ma.messageWithSound(it.errorMessage,isError = true, isPlayed = false)
                    /*customDialog.setTitle(getString(R.string.error)).setDescription(it.errorMessage)
                        .setOkButton(getString(R.string.accept)) {
                            customDialog.dismiss()
                        }.show()*/
                } else {

                    if (!goBack) navigateToCollectionList(it)
                    goBack = false
                }

            })

        }
    }

    private fun navigateToCollectionList(collection: CollectionVO) {
        onCollectionSelectedListener?.onCollectionSelected(collection, ConstAndValues.CONTROLADOR)
    }


}
