package com.base.scanqrclear.ui.language

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.LanguageBean
import com.base.scanqrclear.databinding.ItemLanguageBinding
import com.base.scanqrclear.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.ivSelector.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))
    }
}