package co.tiagoaguiar.course.instagram.search.view

import android.app.SearchManager
import android.content.Context
import android.view.*
import android.widget.ImageView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import co.tiagoaguiar.course.instagram.R
import co.tiagoaguiar.course.instagram.common.base.BaseFragment
import co.tiagoaguiar.course.instagram.common.base.DependencyInjector
import co.tiagoaguiar.course.instagram.common.model.UserAuth
import co.tiagoaguiar.course.instagram.databinding.FragmentSearchBinding
import co.tiagoaguiar.course.instagram.profile.view.PostAdapter
import co.tiagoaguiar.course.instagram.search.Search
import co.tiagoaguiar.course.instagram.search.presenter.SearchPresenter

class SearchFragment : BaseFragment<FragmentSearchBinding, Search.Presenter>(
  R.layout.fragment_search,
  FragmentSearchBinding::bind
), Search.View {

  override lateinit var presenter: Search.Presenter

  private val adapter = SearchAdapter()

  override fun setupViews() {
    binding?.searchRv?.layoutManager = LinearLayoutManager(requireContext())
    binding?.searchRv?.adapter = adapter
  }

  override fun setupPresenter() {
    val repository = DependencyInjector.searchRepository()
    presenter = SearchPresenter(this, repository)
  }

  override fun getMenu() = R.menu.menu_search

  override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)

    val searchManager = requireActivity().getSystemService(Context.SEARCH_SERVICE) as SearchManager
    val searchView = (menu.findItem(R.id.menu_search).actionView as SearchView)
    searchView.apply {
      setSearchableInfo(searchManager.getSearchableInfo(requireActivity().componentName))
      setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
          return false
        }

        override fun onQueryTextChange(newText: String?): Boolean {
          if (newText?.isNotEmpty() == true) {
            presenter.fetchUsers(newText)
            return true
          }
          return false
        }
      })
    }

  }

  override fun showProgress(enabled: Boolean) {
    binding?.searchProgress?.visibility = if (enabled) View.VISIBLE else View.GONE
  }

  override fun displayFullUsers(users: List<UserAuth>) {
    binding?.searchTxtEmpty?.visibility = View.GONE
    binding?.searchRv?.visibility = View.VISIBLE
    adapter.items = users
    adapter.notifyDataSetChanged()
  }

  override fun displayEmptyUsers() {
    binding?.searchTxtEmpty?.visibility = View.VISIBLE
    binding?.searchRv?.visibility = View.GONE
  }

}