package co.tiagoaguiar.course.instagram.profile.view

import android.os.Bundle
import android.view.*
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import co.tiagoaguiar.course.instagram.R

class ProfileFragment : Fragment() {

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

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

    val rv = view.findViewById<RecyclerView>(R.id.profile_rv)
    rv.layoutManager = GridLayoutManager(requireContext(), 3)
    rv.adapter = PostAdapter()
  }

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

  override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.menu_profile, menu)
    super.onCreateOptionsMenu(menu, inflater)
  }

  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_profile_grid, 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.item_profile_img_grid).setImageResource(image)
      }
    }


  }

}