package com.base.datarecovery.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.bean.FolderBean
import com.base.datarecovery.databinding.ItemFolderRecoveryBinding
import com.base.datarecovery.utils.ScreenUtil.dpToPx
import com.base.datarecovery.view.XmlEx.inflate
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class FileFolderAdapter(private val click: (bean: FolderBean) -> Unit) : RecyclerView.Adapter<FileFolderAdapter.FF>() {

    private val beanList = arrayListOf<FolderBean>()

    class FF(view: View) : ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FF {
        return FF(R.layout.item_folder_recovery.inflate(parent))
    }

    override fun getItemCount(): Int {
        return beanList.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: FF, position: Int) {
        val bean = beanList[position]
        val context = holder.itemView.context
        val binding = ItemFolderRecoveryBinding.bind(holder.itemView)
        runCatching {
            val options = RequestOptions().transform(CenterCrop(), RoundedCorners(context.dpToPx(10)))
            val image1 = bean.recoveryList[0].path
            Glide.with(context).load(image1).apply(options).into(binding.iv1)
            val image2 = bean.recoveryList[1].path
            Glide.with(context).load(image2).apply(options).into(binding.iv2)
            val image3 = bean.recoveryList[2].path
            Glide.with(context).load(image3).apply(options).into(binding.iv3)
        }

        binding.tvFolder.text = bean.folder
        binding.tvFolderNumber.text = "(${bean.recoveryList.size})"
        binding.root.setOnClickListener {
            click.invoke(bean)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: List<FolderBean>) {
        beanList.clear()
        beanList.addAll(list)
        notifyDataSetChanged()
    }

}