package com.rugvedsarika.docscan.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.rugvedsarika.docscan.data.entity.Page
import com.rugvedsarika.docscan.databinding.ItemPageBinding

class PageAdapter(private val pages: List<Page>) : RecyclerView.Adapter<PageAdapter.PageViewHolder>() {

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

    override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
        holder.bind(pages[position])
    }

    override fun getItemCount(): Int = pages.size

    fun getPageAt(position: Int): Page? {
        return if (position in pages.indices) pages[position] else null
    }

    class PageViewHolder(private val binding: ItemPageBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(page: Page) {
            Glide.with(binding.ivPage.context)
                .load(page.imagePath)
                .into(binding.ivPage)
        }
    }
}
