package xyz.apiote.bimba.czwek.dashboard.ui.home

import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.mancj.materialsearchbar.MaterialSearchBar
import com.mancj.materialsearchbar.MaterialSearchBar.BUTTON_NAVIGATION
import xyz.apiote.bimba.czwek.api.QueryableV1
import xyz.apiote.bimba.czwek.dashboard.MainActivity
import xyz.apiote.bimba.czwek.databinding.FragmentHomeBinding
import xyz.apiote.bimba.czwek.search.BimbaSuggestionsAdapter

// todo [3.1] search: https://github.com/material-components/material-components-android/blob/master/docs/components/Search.md

class HomeFragment : Fragment() {
	private var _binding: FragmentHomeBinding? = null
	private val binding get() = _binding!!

	private var lastSuggestions = listOf<QueryableV1>()

	override fun onCreateView(
		inflater: LayoutInflater,
		container: ViewGroup?,
		savedInstanceState: Bundle?
	): View {
		_binding = FragmentHomeBinding.inflate(inflater, container, false)

		val homeViewModel =
			ViewModelProvider(this)[HomeViewModel::class.java]
		homeViewModel.queryables.observe(viewLifecycleOwner) {
			binding.searchBar.updateLastSuggestions(it)
		}

		val root = binding.root
		ViewCompat.setOnApplyWindowInsetsListener(root) { view, windowInsets ->
			val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
			view.layoutParams = (view.layoutParams as FrameLayout.LayoutParams).apply {
				topMargin = insets.top
			}
			WindowInsetsCompat.CONSUMED
		}

		binding.searchBar.lastSuggestions = lastSuggestions
		val cm = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
		binding.searchBar.addTextChangeListener(
			homeViewModel.SearchBarWatcher(requireContext(), cm)
		)
		binding.searchBar.setOnSearchActionListener(object : MaterialSearchBar.OnSearchActionListener {
			override fun onButtonClicked(buttonCode: Int) {
				when (buttonCode) {
					BUTTON_NAVIGATION -> {
						(context as MainActivity).onNavigationClicked()
					}
				}
			}

			override fun onSearchStateChanged(enabled: Boolean) {
			}

			override fun onSearchConfirmed(text: CharSequence?) {
				binding.searchBar.clearSuggestions()
				(context as MainActivity).onSearchClicked(text)
			}
		})
		binding.searchBar.setCardViewElevation(0)
		binding.searchBar.setCustomSuggestionAdapter(BimbaSuggestionsAdapter(layoutInflater, context) {
			binding.searchBar.clearSuggestions()
			(context as MainActivity).onSuggestionClicked(it)
		})

		binding.floatingActionButton.setOnClickListener {
			binding.searchBar.clearSuggestions()
			(context as MainActivity).onGpsClicked(this)
		}
		// todo [3.1] (ux,low) on searchbar focus && if != '' -> populate suggestions

		return binding.root
	}

	override fun onDestroyView() {
		super.onDestroyView()
		_binding = null
	}
}