package com.base.filerecoveryrecyclebin.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.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import com.base.filerecoveryrecyclebin.databinding.ItemMediaFileBinding
import com.base.filerecoveryrecyclebin.databinding.ItemRecycleBinBeanBinding
import com.base.filerecoveryrecyclebin.databinding.ItemRecyclebinFileBinding
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatTime
import com.base.filerecoveryrecyclebin.view.XmlEx.inflate
import com.bumptech.glide.Glide
import java.io.File

class RecycleBinAdapter : RecyclerView.Adapter<RecycleBinAdapter.RBAV>() {

    private val beanList = arrayListOf<RecycleBinBean>()


    inner class RBAV(view: View) : ViewHolder(view) {

    }

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

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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RBAV, position: Int) {
        val binding = ItemRecyclebinFileBinding.bind(holder.itemView)
        val bean = beanList[position]
        val file = File(bean.path)
        val context = holder.itemView.context
        Glide.with(context).load(bean.path).centerCrop().into(binding.iv)
        binding.tvName.text = file.name
        binding.tvTime.text = "delete time:" + bean.deleteTime.toString()
        binding.tvSize.text = bean.size.toFormatTime()
    }

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