package cz.muni.sutralog.ui.list

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cz.muni.sutralog.data.PhotoPresentableListItem
import cz.muni.sutralog.databinding.PhotoItemListBinding

class PhotoAdapter(private val onItemClick: (PhotoPresentableListItem) -> Unit,
                   private val onItemLongClick: (PhotoPresentableListItem) -> Boolean,
) : RecyclerView.Adapter<PhotoViewHolder>() {

    private var listItems: MutableList<PhotoPresentableListItem> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
        val binding = PhotoItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PhotoViewHolder(binding)
    }

    override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
        holder.bind(listItems[position], onItemClick, onItemLongClick)
    }

    fun submitList(newListItems: List<PhotoPresentableListItem>) {
        listItems = newListItems.toMutableList()
        notifyDataSetChanged()
    }

    fun addItem(newListItem: PhotoPresentableListItem) {
        listItems.add(newListItem)
        notifyItemInserted(itemCount - 1)
    }

    fun removeItem(position: Int) {
        listItems.removeAt(position)
        notifyItemRemoved(position)
    }


    override fun getItemCount(): Int = listItems.size
}
