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

import android.content.Context
import android.view.View
import android.view.View.GONE
import android.view.inputmethod.EditorInfo
import es.verdnatura.R
import es.verdnatura.databinding.FragmentControladorBinding
import es.verdnatura.domain.notNull
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnCollectionSelectedListener
import es.verdnatura.presentation.view.feature.reubication.model.Reubication
import es.verdnatura.presentation.view.feature.ubicador.fragment.UbicadorViewModel

//Tarea #3754
class ReubicationFragment(var entrypoint: String) :
    BaseFragment<FragmentControladorBinding, UbicadorViewModel>(
        UbicadorViewModel::class
    ) {

    private var goBack: Boolean = false
    private var onCollectionSelectedListener: OnCollectionSelectedListener? = null
    override fun getLayoutId(): Int = R.layout.fragment_controlador
    private var shelvingScaned: String = ""

    companion object {
        fun newInstance(entrypoint: String) = ReubicationFragment(entrypoint = entrypoint)
    }

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

    override fun init() {

        binding.mainToolbar.toolbarTitle.text = entrypoint
        binding.splashProgress.visibility = GONE
        setEvents()

        super.init()
    }

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


    private fun setEvents() {

        binding.mainToolbar.backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        binding.scanInput.requestFocus()
        binding.scanInput.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                goBack = false
                if (!binding.scanInput.text.isNullOrEmpty()) {
                    binding.splashProgress.visibility = View.VISIBLE
                    shelvingScaned = binding.scanInput.text.toString()
                    viewModel.itemShelving_return(
                        usuario = getData(USER),
                        password = getData(PASSWORD),
                        shelvingFk = binding.scanInput.text.toString(),

                        )

                }

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

    override fun observeViewModel() {
        with(viewModel) {
            loadReubicationList.observe(viewLifecycleOwner) { event ->
                event.getContentIfNotHandled().notNull {
                    binding.splashProgress.visibility = GONE


                    if (it.list.isEmpty()) {
                        ma.messageWithSound(
                            getString(R.string.errorReubication), true, true, getString(
                                R.string.info
                            )
                        )
                    } else {
                        if (it.list[0].isError) {
                            ma.messageWithSound(
                                it.list[0].errorMessage, true, true, getString(
                                    R.string.info
                                )
                            )
                        } else {

                            createReubications(it.list)

                        }
                    }

                }
            }


        }


    }

    private fun createReubications(list: List<Reubication>) {

        ma.openFragmentReubications(list as MutableList<Reubication>, shelvingScaned)

    }


}


