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

import android.content.Context
import android.view.View.VISIBLE
import android.view.inputmethod.EditorInfo
import es.verdnatura.R
import es.verdnatura.databinding.FragmentGeneralBlackBinding
import es.verdnatura.domain.notNull
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnCollectionSelectedListener
import es.verdnatura.presentation.view.feature.ubicador.fragment.UbicadorViewModel

class ReubicationFragment(var entrypoint: String) :
    BaseFragment<FragmentGeneralBlackBinding, UbicadorViewModel>(
        UbicadorViewModel::class
    ) {

    private var goBack: Boolean = false
    private var onCollectionSelectedListener: OnCollectionSelectedListener? = null
    override fun getLayoutId(): Int = R.layout.fragment_general_black
    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.scanInput.visibility = VISIBLE
        setEvents()

        super.init()
    }

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

    private fun setEvents() {

        binding.mainToolbar.backButton.setOnClickListener {
            ma.onMyBackPressed()
        }

        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()) {

                    shelvingScaned = binding.scanInput.text.toString()
                    viewModel.itemShelvingAlternative(
                        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 {

                    if (it.list.isEmpty()) {
                        ma.messageWithSound(
                            getString(R.string.errorReubication),
                            isError = true,
                            isPlayed = true,
                            getString(
                                R.string.info
                            )
                        )
                    } else {
                        ma.openFragmentReubications(it.list, shelvingScaned)

                    }

                }
            }

        }

    }

}


