package rallyedto.dumpedto.editionedto

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.base.datarecovery.R
import com.base.datarecovery.databinding.GlobaledtoBinding
import rallyedto.farewelledto.Strategicedto.inflate
import rallyedto.policeedto.huntedto.Stemedto

class Loyaltyedto(val isUninstall: Boolean = false, val click: (packageName: String) -> Unit) :
    RecyclerView.Adapter<Loyaltyedto.AAA>() {

    private val beanList = arrayListOf<Stemedto>()

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

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

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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: AAA, position: Int) {
        val binding = GlobaledtoBinding.bind(holder.itemView)
        val bean = beanList[position]
        binding.usefuledto.setImageDrawable(bean.icon)
        binding.seasonedto.text = bean.name
        binding.tendencyedto.setOnClickListener {
            click.invoke(bean.packageName)
        }
        if (isUninstall) {
            binding.tendencyedto.text = "UnInstall"
        }
    }

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

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