package es.verdnatura.presentation.view.feature.ubicador.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.FragmentAutomaticAddItemBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnAutomaticItemClickListener
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.component.CustomDialogInput
import es.verdnatura.presentation.view.component.CustomDialogUbicador
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import es.verdnatura.presentation.view.feature.ubicador.adapter.AutomaticAdapter
import es.verdnatura.presentation.view.feature.ubicador.model.ItemEscanerVO


@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class AutomaticAddItemFragment(
    var shelvingFk : String = ""
) : BaseFragment<FragmentAutomaticAddItemBinding,AutomaticAddItemViewModel>(AutomaticAddItemViewModel::class) {

    private var adapter : AutomaticAdapter? = null
    private var listItems:ArrayList<ItemEscanerVO> = ArrayList()
    private var deep:Int = 1
    private var contador = 0

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

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

    override fun init() {

        ma.hideBottomNavigation(View.GONE)
        binding.mainToolbar.toolbarTitle.text = "itemShelvingMake"
        setEvents()
        setViews()
        setToolBar()
        super.init()
    }

    private fun setToolBar(){
        binding.mainToolbar.toolbarSubtitle.visibility = View.VISIBLE
    }

    private fun setSubtitle(){
        binding.mainToolbar.toolbarSubtitle.text = getString(R.string.labels)+contador
    }

    private fun setViews(){
        adapter = AutomaticAdapter(listItems,object: OnAutomaticItemClickListener{
            override fun onAutomaticItemClickListener(position: Int) {
                contador -= 1
                setSubtitle()
                if (listItems.size > position){
                    listItems.removeAt(position)
                }

                adapter!!.notifyDataSetChanged()
            }
        })
        binding.itemsRecyclerview.adapter = adapter
        binding.itemsRecyclerview.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
    }

    private fun setEvents(){
        binding.editMatricula.requestFocus()
        binding.editMatricula.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.editMatricula.text.toString().isNullOrEmpty()){
                    contador += 1
                    setSubtitle()
                    listItems.add(0,ItemEscanerVO(binding.editMatricula.text.toString()))
                    adapter!!.notifyDataSetChanged()
                }
                binding.editMatricula.setText("")
                ma.hideKeyboard(binding.editMatricula)
                return@setOnEditorActionListener true
            }

            return@setOnEditorActionListener false
        }

        binding.buttonFinalizar.setOnClickListener {
            val listString:ArrayList<String> = ArrayList()
            listItems.forEach {
                listString.add(it.item)
            }
            listItems = ArrayList()
            binding.splashProgress.visibility = View.VISIBLE
            viewModel.itemShelvingMake_multi( getData(USER),getData(PASSWORD),shelvingFk,listString,getData(WAREHOUSEFK))
        }

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

    override fun observeViewModel() {
        with(viewModel){
            response.observe(viewLifecycleOwner, Observer {
               binding.splashProgress.visibility = View.GONE
                if (it.isError){
                    ma.messageWithSound(it.errorMessage,true,false)
                }else{
                    requireActivity().onBackPressed()
                }
            })
        }
    }

}
