package com.base.filerecoveryrecyclebin.adapter

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.findRecycleBinFile
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import com.base.filerecoveryrecyclebin.databinding.ItemRecyclebinFileBinding
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatTime
import com.base.filerecoveryrecyclebin.utils.LogEx
import com.base.filerecoveryrecyclebin.view.XmlEx.inflate
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import java.io.File

class RecycleBinAdapter(val click: (bean: RecycleBinBean) -> Unit) : RecyclerView.Adapter<RecycleBinAdapter.RBAV>() {

    private val TAG = "RecycleBinAdapter"

    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.backupPath)
        val context = holder.itemView.context
        LogEx.logDebug("glc","bean.backupPath   "+bean.backupPath)
        LogEx.logDebug("glc","bean.absoluteFile   "+bean.backupPath)

        Glide.with(context).load(file?.absoluteFile).listener(object : RequestListener<Drawable> {
            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>, isFirstResource: Boolean): Boolean {
                LogEx.logDebug(TAG, "Failed to load image: $model")
                return false
            }

            override fun onResourceReady(
                resource: Drawable,
                model: Any,
                target: Target<Drawable>?,
                dataSource: DataSource,
                isFirstResource: Boolean
            ): Boolean {

                return false
            }
        }).centerCrop().into(binding.iv)
        binding.tvName.text = bean.name
        binding.tvTime.text = "delete time:" + bean.deleteTime.toFormatTime()
        binding.tvSize.text = bean.size.toFormatSize()
        binding.root.setOnClickListener {
            click.invoke(bean)
        }
    }

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