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.databinding.FragmentSearchBinding
import co.tiagoaguiar.course.instagram.search.Search

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

  override lateinit var presenter: Search.Presenter

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

  override fun setupPresenter() {
    // TODO:
  }

  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 {
          return false
        }
      })
    }

  }

  private class PostAdapter : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
      return PostViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.item_user_list, parent, false)
      )
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
      holder.bind(R.drawable.ic_insta_add)
    }

    override fun getItemCount(): Int {
      return 30
    }


    private class PostViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
      fun bind(image: Int) {
        itemView.findViewById<ImageView>(R.id.search_img_user).setImageResource(image)
      }
    }

  }

}