package com.easy.clean.ui.recovery

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.Dimension
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.easy.clean.R
import com.easy.clean.bean.recovery.RecoveryFilterBean
import com.easy.clean.databinding.ItemRecoveryFilterBinding
import androidx.core.graphics.toColorInt

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

    private val beanList = arrayListOf<RecoveryFilterBean>()

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

    fun Int.inflate(parent: ViewGroup, attachToRoot: Boolean = false): View {
        return LayoutInflater.from(parent.context).inflate(this, parent, attachToRoot)
    }

    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(Color.BLACK)
            binding.tv.setTextSize(Dimension.SP, 18f)
        } else {
            binding.tv.setTextColor("#747474".toColorInt())
            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()
    }
}