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

    import android.view.View
    import android.view.inputmethod.EditorInfo
    import androidx.lifecycle.Observer
    import androidx.recyclerview.widget.LinearLayoutManager
    import es.verdnatura.R
    import es.verdnatura.databinding.FragmentItemshelvinglogBinding
    import es.verdnatura.domain.notNull
    import es.verdnatura.presentation.base.BaseFragment
    import es.verdnatura.presentation.view.component.CustomDialog
    import es.verdnatura.presentation.view.component.CustomDialogList
    import es.verdnatura.presentation.view.feature.historicoshelving.adapter.ItemShelvingLogAdapter
    import es.verdnatura.presentation.view.feature.main.activity.MainActivity


@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class itemShelvingLogFragment(
        var entryPoint: String = ""
                          // ) : BaseFragment<FragmentItemdayofsaleCardBinding, itemShelvingLogViewModel>(itemShelvingLogViewModel::class) {
    ) : BaseFragment<FragmentItemshelvinglogBinding, itemShelvingLogViewModel>(itemShelvingLogViewModel::class) {
        //private lateinit var customDialogList: CustomDialogList
        private var adapter: ItemShelvingLogAdapter? = null
        private var itemScan = ""

        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())
           // customDialogList = CustomDialogList(requireContext())

            setEvents()
            setToolBar(getString(R.string.itemShelvingLog))
            super.init()

        }

        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().isNullOrEmpty()) {
                        itemShelvingLog_get(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()
            }

        }


        private fun itemShelvingLog_get(vShelvingFK: String) {
            binding.splashProgress.visibility = View.VISIBLE
            viewModel.itemShelvingLog_get(getData(USER), getData(PASSWORD),vShelvingFK)

        }


        override fun observeViewModel() {
            with(viewModel) {

                loadItemShelvingLogList.observe(viewLifecycleOwner, Observer{ event ->
                    event.getContentIfNotHandled().notNull {
                        binding.splashProgress.visibility = View.GONE
                        adapter = ItemShelvingLogAdapter(it.list)
                        binding.itemshelvinglogRecyclerview.adapter = adapter
                        binding.itemshelvinglogRecyclerview.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
                        setToolBar(getString(R.string.itemShelvingLog)+":"+itemScan)

                        if  (it.list.isEmpty()){
                            customDialog.setTitle(getString(R.string.noResults))
                                .setDescription(getString(R.string.noDataLabelScanned))
                                .setOkButton(getString(R.string.close)) {
                                    customDialog.dismiss()
                                }.show()
                        }else{

                            if (it.list.get(0).isError){
                                customDialog.setTitle(getString(R.string.error))
                                    .setDescription(it.list.get(0).errorMessage)
                                    .setOkButton(getString(R.string.close)) {
                                        customDialog.dismiss()
                                    }.show()
                            }
                        }

                    }
                })


            }
        }



    }
