package com.base.appzxhy.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.base.appzxhy.R
import com.base.appzxhy.bean.LanguageBean
import com.base.appzxhy.databinding.ItemLanguageBinding
import com.base.appzxhy.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class LanguageAdapter(
    val click: (item: LanguageBean) -> Unit
) : BaseQuickAdapter<LanguageBean, LanguageAdapter.LanguageViewHolder>() {

    inner class LanguageViewHolder(view: View) : RecyclerView.ViewHolder(view)

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindViewHolder(holder: LanguageViewHolder, position: Int, item: LanguageBean?) {
        item ?: return

        val binding = ItemLanguageBinding.bind(holder.itemView)
        binding.tv.text = item.key
        binding.fl.isSelected = item.isSelect
        binding.root.setOnClickListener {
            items.map { it.isSelect = false }
            item.isSelect = true
            notifyDataSetChanged()

            click.invoke(item)
        }

    }

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