package com.base.filerecoveryrecyclebin.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.bean.AppBean
import com.base.filerecoveryrecyclebin.databinding.ItemAppManagerBinding
import com.base.filerecoveryrecyclebin.view.XmlEx.inflate

class AppManagerAdapter(val click: (packageName: String) -> Unit) : RecyclerView.Adapter<AppManagerAdapter.AAA>() {

    private val beanList = arrayListOf<AppBean>()

    class AAA(view: View) : RecyclerView.ViewHolder(view)

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

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

    override fun onBindViewHolder(holder: AAA, position: Int) {
        val binding = ItemAppManagerBinding.bind(holder.itemView)
        val bean = beanList[position]
        binding.iv.setImageDrawable(bean.icon)
        binding.tvName.text = bean.name
        binding.tvStop.setOnClickListener {
            click.invoke(bean.packageName)
        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    fun removeBean(pkg: String) {
        beanList.removeIf { it.packageName == pkg }
        notifyDataSetChanged()
    }
}