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.FragmentItemexpeditionstateCardBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.view.feature.paletizador.adapter.ExpeditionStateAdapter
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionStateRow
import es.verdnatura.presentation.view.feature.paletizador.model.itemExpedetionState

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class ExpeditionStateFragment(
    var entryPoint: String = ""
   ) : BaseFragment<FragmentItemexpeditionstateCardBinding, ExpeditionStateViewModel>(ExpeditionStateViewModel::class) {


    private var adapter: ExpeditionStateAdapter? = null
    private var listItemsRow: ArrayList<ItemExpeditionStateRow> = ArrayList()
    private var itemScan = ""

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

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

    override fun init() {
        setEvents()
        setToolBar(entryPoint)
        super.init()
    }


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

    private fun setEvents() {
        binding.editItemFk.requestFocus()
        binding.editItemFk.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (binding.editItemFk.text.toString().isNotEmpty()) {
                    getExpedtionState(binding.editItemFk.text.toString())
                    itemScan = binding.editItemFk.text.toString()
                }
                binding.editItemFk.setText("")
                ma.hideKeyboard(binding.editItemFk)
                return@setOnEditorActionListener true
            }
            false
        }


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

    }

    private fun getExpedtionState(expeditionFk: String) {

        binding.splashProgress.visibility = View.VISIBLE
        viewModel.expedition_getStateJSON(expeditionFk, getData(USER), getData(PASSWORD))

    }

    override fun observeViewModel() {
        with(viewModel) {
            itemexpedition.observe(viewLifecycleOwner,{
                binding.splashProgress.visibility = View.GONE
                if (it.isError) {
                    binding.itemcardLayout.visibility = View.GONE
                    binding.mainToolbar.toolbarTitle.text = entryPoint
                    ma.messageWithSound(getString(R.string.noDataLabelScanned),true,false,getString(R.string.noResults))

                } else {
                        setItemExpeditionState(it)
                }
            })

        }
    }


    private fun setItemExpeditionState(itemInfo: itemExpedetionState) {
        setToolBar(getString(R.string.expedition) + itemScan)
        binding.itemcardLayout.visibility = View.VISIBLE

        listItemsRow = ArrayList()
        for (i in itemInfo.list) {
            listItemsRow.add(
                ItemExpeditionStateRow(
                    title = i.key,
                    value = i.value?.replace(".0",""),
                    isEditable = false
                )
            )
        }
        adapter = ExpeditionStateAdapter(listItemsRow)
        binding.itemexpeditionstateRecyclerview.adapter = adapter
        binding.itemexpeditionstateRecyclerview.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)


    }


}

