package co.tiagoaguiar.tutorial.jokerappdev.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import co.tiagoaguiar.tutorial.jokerappdev.R
import co.tiagoaguiar.tutorial.jokerappdev.model.Category
import co.tiagoaguiar.tutorial.jokerappdev.presentation.HomePresenter
import com.xwray.groupie.GroupieAdapter

class HomeFragment : Fragment() {

  private lateinit var progressBar: ProgressBar

  private lateinit var presenter: HomePresenter

  private val adapter = GroupieAdapter()

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    presenter = HomePresenter(this)
  }

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_home, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    progressBar = view.findViewById(R.id.progress_bar)

    val recyclerView = view.findViewById<RecyclerView>(R.id.rv_main)
    recyclerView.layoutManager = LinearLayoutManager(requireContext())

    presenter.findAllCategories()

    recyclerView.adapter = adapter
  }

  fun showCategories(response: List<Category>) {
    val categories = response.map { CategoryItem(it) }
    adapter.addAll(categories)
    adapter.notifyDataSetChanged()
  }

  fun showFailure(message: String) {
    Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
  }

  fun showProgress() {
    progressBar.visibility = View.VISIBLE
  }

  fun hideProgress() {
    progressBar.visibility = View.GONE
  }

}