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

import android.view.View.*
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentExpeditionscanSorterBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.*
import es.verdnatura.presentation.view.component.CustomDialogList


class ExpeditionScanSorterFragment(
    var title: String = ""
) : BaseFragment<FragmentExpeditionscanSorterBinding, ExpeditionScanSorterViewModel>(
    ExpeditionScanSorterViewModel::class
) {

    override fun getLayoutId(): Int = R.layout.fragment_expeditionscan_sorter
    private lateinit var customDialogList: CustomDialogList
    private var listExpeditionScan: ArrayList<GeneralItem> = ArrayList()
    private var expeditionScanAdapter: GeneralAdapter? = null
    private var itemScaned: String = ""

    companion object {
        fun newInstance(title: String) = ExpeditionScanSorterFragment(title)
    }

    override fun init() {

        customDialogList = CustomDialogList(requireContext())
        ma.hideBottomNavigation(GONE)
        binding.splashProgress.visibility = GONE

        setEvents()
        setToolBar()
        showExpeditionScan()
        super.init()
    }

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

    private fun expeditionscan(ExpeditionScan: String) {

        binding.splashProgress.visibility = VISIBLE
        viewModel.expedition_scan(getData(USER),getData(PASSWORD), ExpeditionScan)
        itemScaned = ExpeditionScan

    }

    private fun setEvents() {

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

        }

    }

    override fun observeViewModel() {

        with(viewModel) {
            binding.splashProgress.visibility = GONE
            responsescan.observe(viewLifecycleOwner,  {

                if (it.isError) {

                    ma.messageWithSound(it.errorMessage,true,false)

                } else {

                    listExpeditionScan.add(GeneralItem(itemScaned, itemScaned))
                    customDialogList.setDescription(getString(R.string.total) + listExpeditionScan.size)
                    expeditionScanAdapter!!.notifyItemInserted(listExpeditionScan.size-1)
                    customDialogList.setValue("")
                    ma.messageWithSound(getString(R.string.added),false,true)

                }
            })

        }

    }

    private fun showExpeditionScan() {

        customDialogList.setTitle(getString(R.string.expeditionNoSorter))
            .setOkButton(getString(R.string.end)) {

                ma.hideKeyboard(customDialogList.getEditText())
                customDialogList.dismiss()
                binding.mainToolbar.backButton.performClick()


            }.setValue("").show()


        customDialogList.getEditText().requestFocus()
        ma.hideKeyboard(customDialogList.getEditText())

        customDialogList.getEditText().setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5 || actionId == 6) {
                if (customDialogList.getValue().isNotEmpty()) {
                    expeditionscan(customDialogList.getValue())
                }
                customDialogList.setValue("")
                ma.hideKeyboard(customDialogList.getEditText())
                return@setOnEditorActionListener true
            }
            false
        }

        expeditionScanAdapter =
            GeneralAdapter(listExpeditionScan, object : OnGeneralItemRowClickListener {
                override fun OnGeneralItemRowClickListener(item: GeneralItem) {
                }
            })

        customDialogList.getRecyclerView().adapter = expeditionScanAdapter
        customDialogList.getRecyclerView().layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)

    }

}

