package com.base.superfilemanager.activity.photocompress.photo

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.base.superfilemanager.R

class AlbumListAdapter : RecyclerView.Adapter<AlbumListAdapter.ContentViewHolder> {


    private var mList: List<AlbumBean>

    private val mListener: OnItemClickListener<AlbumBean>

    constructor(mList: List<AlbumBean>, mListener: OnItemClickListener<AlbumBean>) : super() {
        this.mList = mList
        this.mListener = mListener
    }

    fun updateData(mList: List<AlbumBean>) {
        this.mList = mList
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentViewHolder {

        return ContentViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_album, parent, false)
        )
    }

    override fun onBindViewHolder(holder: ContentViewHolder, position: Int) {

        val entity = mList[position]

        holder.mName.text = entity.name
        holder.mNum.text = entity.num.toString()

        Glide.with(holder.itemView.context).load(entity.src).into(holder.mImage)
    }

    override fun getItemCount(): Int = mList.size


    inner class ContentViewHolder : ViewHolder {

        val mName: TextView
        val mNum: TextView
        val mImage: ImageView

        constructor(itemView: View) : super(itemView) {

            mName = itemView.findViewById(R.id.name)
            mNum = itemView.findViewById(R.id.num)
            mImage = itemView.findViewById(R.id.image)

            itemView.setOnClickListener {
                if(position>=0&&position<mList.size){
                    val entity = mList[position]
                    mListener.onItemClick(position, entity)
                }

            }
        }
    }
}