package es.verdnatura.presentation.view.feature.shelvingparking.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.FragmentShelvingParkingBinding
import es.verdnatura.domain.notNull
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import es.verdnatura.presentation.view.feature.shelvingparking.adapter.ShelvingParkingAdapter


@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class ShelvingParkingFragment : BaseFragment<FragmentShelvingParkingBinding, ShelvingParkingViewModel>(
    ShelvingParkingViewModel::class) {


    private var adapter : ShelvingParkingAdapter? = null
    companion object {
        fun newInstance() = ShelvingParkingFragment()
    }

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


    override fun init() {

        ma.hideBottomNavigation(View.GONE)
        
        binding.mainToolbar.toolbarTitle.text = getString(R.string.shelvingParking_get)
        setEvents()
        super.init()
    }


    private fun setEvents(){
        binding.mainToolbar.backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

       binding.shelvingText.requestFocus()
        binding.shelvingText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.shelvingText.text.toString().isNullOrEmpty()){
                    binding.mainToolbar.toolbarTitle.text = binding.shelvingText.text.toString()
                    binding.splashProgress.visibility = View.VISIBLE
                    viewModel.shelvingParking_get( getData(USER),getData(PASSWORD),binding.shelvingText.text.toString(),getData(WAREHOUSEFK),binding.dayrange.text.toString())
                }

                binding.shelvingText.setText("")
                ma.hideKeyboard(binding.shelvingText)
                return@setOnEditorActionListener true
            }

            return@setOnEditorActionListener false
        }

        binding.dayrange.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.shelvingText.text.toString().isNullOrEmpty())
                    binding.splashProgress.visibility = View.VISIBLE
                else
                    binding.shelvingText.requestFocus()

                binding.mainToolbar.toolbarTitle.text = binding.shelvingText.text.toString()
                viewModel.shelvingParking_get( getData(USER),getData(PASSWORD),binding.shelvingText.text.toString(),getData(WAREHOUSEFK),binding.dayrange.text.toString())
                binding.shelvingText.setText("")
                ma.hideKeyboard(binding.dayrange)
                return@setOnEditorActionListener true
            }

            return@setOnEditorActionListener false
        }
    }

    override fun observeViewModel() {
        with(viewModel){
            loadShelvingList.observe(viewLifecycleOwner, Observer { event ->
                event.getContentIfNotHandled().notNull {
                    binding.splashProgress.visibility = View.GONE
                    adapter = ShelvingParkingAdapter(it.list)
                    binding.locationRecyclerview.adapter = adapter
                    binding.locationRecyclerview.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)

                }
            })
        }
    }

}