package com.base.datarecovery.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.annotation.Dimension
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.bean.RecoveryFilterBean
import com.base.datarecovery.databinding.ItemRecoveryFilterBinding
import com.base.datarecovery.view.XmlEx.inflate

class RecoveryFilterAdapter(
    val click: (bean: RecoveryFilterBean) -> Unit
) : RecyclerView.Adapter<RecoveryFilterAdapter.YYDS>() {

    private val beanList = arrayListOf<RecoveryFilterBean>()

    inner class YYDS(view: View) : ViewHolder(view)

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

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

    override fun onBindViewHolder(holder: YYDS, position: Int) {
        val binding = ItemRecoveryFilterBinding.bind(holder.itemView)
        val bean = beanList[position]
        val context = holder.itemView.context
        binding.tv.text = bean.tittle
        binding.iv.isVisible = bean.isSelect
        if (bean.isSelect) {
            binding.tv.setTextColor(ContextCompat.getColor(context, R.color.black))
            binding.tv.setTextSize(Dimension.SP, 18f)
        } else {
            binding.tv.setTextColor(ContextCompat.getColor(context, R.color.color_747474))
            binding.tv.setTextSize(Dimension.SP, 16f)
        }
        binding.root.setOnClickListener {
            click.invoke(bean)
        }
    }

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