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

import android.content.Context
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentItemshelvinglogBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.feature.historicoshelving.adapter.ItemShelvingLogAdapter

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class ItemShelvingLogFragment(
    var entryPoint: String = ""
) : BaseFragment<FragmentItemshelvinglogBinding, ItemShelvingLogViewModel>(ItemShelvingLogViewModel::class) {

    private var adapter: ItemShelvingLogAdapter? = null
    private var itemScan = ""
    private var pasillerosItemClickListener: OnPasillerosItemClickListener? = null

    companion object {
        fun newInstance(entryPoint: String) = ItemShelvingLogFragment(entryPoint)
    }

    private lateinit var customDialog: CustomDialog

    override fun getLayoutId(): Int = R.layout.fragment_itemshelvinglog

    override fun init() {

        customDialog = CustomDialog(requireContext())
        setEvents()
        setToolBar(getString(R.string.itemShelvingLog))
        super.init()

    }

    override fun onAttach(context: Context) {
        if (context is OnPasillerosItemClickListener) pasillerosItemClickListener = context
        super.onAttach(context)
    }

    private fun setToolBar(title: String) {
        binding.mainToolbar.toolbarTitle.text = title
    }

    private fun setEvents() {
        binding.scanInput.requestFocus()
        binding.scanInput.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (binding.scanInput.text.toString().isNotEmpty()) {
                    viewModel.itemShelvingLogGet(binding.scanInput.text.toString())
                    itemScan = binding.scanInput.text.toString()
                }
                binding.scanInput.setText("")
                ma.hideKeyboard(binding.scanInput)
                return@setOnEditorActionListener true
            }
            false
        }
        binding.mainToolbar.backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

    }

    override fun observeViewModel() {
        with(viewModel) {
            itemShelvingLogList.observe(viewLifecycleOwner) {

                if (it.list.isEmpty()) {
                    customDialog.setTitle(getString(R.string.noResults))
                        .setDescription(getString(R.string.noDataLabelScanned))
                        .setOkButton(getString(R.string.close)) {
                            customDialog.dismiss()
                        }.show()
                } else {
                    adapter = ItemShelvingLogAdapter(
                        it.list,
                        onPasillerosItemClickListener = pasillerosItemClickListener!!
                    )
                    binding.itemshelvinglogRecyclerview.adapter = adapter
                    binding.itemshelvinglogRecyclerview.layoutManager =
                        LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
                    setToolBar(getString(R.string.itemShelvingLog) + ":" + itemScan)
                }
            }
        }
    }

}
