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

import android.content.Context
import android.view.View
import android.view.View.GONE
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentBuscarItemBinding
import es.verdnatura.domain.notNull
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.feature.buscaritem.adapter.LocationAdapter


class BuscarItemFragment(
    var itemFk: String = ""
) : BaseFragment<FragmentBuscarItemBinding, BuscarItemViewModel>(BuscarItemViewModel::class) {

    private var adapter: LocationAdapter? = null
    private var pasillerosItemClickListener: OnPasillerosItemClickListener? = null

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

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

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

    override fun init() {

        ma.hideBottomNavigation(GONE)
        binding.mainToolbar.toolbarTitle.text = getString(R.string.getubicaition)

        setEvents()
        if (!itemFk.isNullOrEmpty()) {
            getLocations(itemFk)
        }
        super.init()
    }


    private fun setEvents() {
        binding.editItemFk.requestFocus()
        binding.editItemFk.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.editItemFk.text.toString().isNullOrEmpty())
                    getLocations(binding.editItemFk.text.toString())
                binding.editItemFk.setText("")
                ma.hideKeyboard(binding.editItemFk)
                return@setOnEditorActionListener true
            }
            false
        }


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

    }

    private fun getLocations(itemFk: String) {
        this.itemFk = itemFk
        binding.splashProgress.visibility = View.VISIBLE
        viewModel.getLocations(getData(USER), getData(PASSWORD), itemFk)
    }

    override fun observeViewModel() {
        with(viewModel) {
            loadLocationList.observe(viewLifecycleOwner, Observer { event ->

                event.getContentIfNotHandled().notNull {
                    binding.splashProgress.visibility = GONE
                    adapter = LocationAdapter(it.list, pasillerosItemClickListener!!)
                    binding.locationRecyclerview.adapter = adapter
                    binding.locationRecyclerview.layoutManager =
                        LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)

                    var totalVisible = 0
                    it.list.forEach {
                        if (!it.visible.isNullOrEmpty())
                            totalVisible += it.visible.toInt()
                    }
                    binding.mainToolbar.toolbarTitle.text =
                        getString(R.string.item) + itemFk + getString(R.string.visibleTotal) + totalVisible


                }
            })
        }
    }

}
