package com.dumpster.cleaner.ui.recyclebin

import android.content.Context
import android.view.ViewGroup
import com.dumpster.cleaner.R
import com.dumpster.cleaner.databinding.ItemRecoveryBinBinding
import com.dumpster.cleaner.ui.adapter.CommonViewHolder
import com.dumpster.cleaner.ui.recyclebin.RecycleBinUtils.getImgRes
import com.dumpster.cleaner.ui.recyclebin.RecycleBinUtils.mImageStr
import com.dumpster.cleaner.ui.recyclebin.RecycleBinUtils.mVideoStr
import com.dumpster.cleaner.utils.KotlinExt.toFormatSize
import com.dumpster.cleaner.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseQuickAdapter
import java.io.File

class RecycleBinAdapter : BaseQuickAdapter<File, CommonViewHolder>() {

    var clickAction: ((file: File) -> Unit)? = null

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: File?) {
        item ?: return
        val binding = ItemRecoveryBinBinding.bind(holder.itemView)
        val context = holder.itemView.context

        if (mImageStr.contains("." + item.extension) || mVideoStr.contains("." + item.extension)) {
            Glide.with(context).load(item.absoluteFile).into(binding.icon)
        } else {
            binding.icon.setImageResource(item.getImgRes())
        }
        binding.tvName.text = item.name
        binding.tvSize.text = item.length().toFormatSize()
        binding.root.setOnClickListener {
            clickAction?.invoke(item)
        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): CommonViewHolder {
        return CommonViewHolder(R.layout.item_recovery_bin.inflate(parent))
    }
}