package com.base.pdfviewerscannerwhite.ui.set

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

class LanguageAdapter : BaseQuickAdapter<LanguageBean, LanguageAdapter.LanguageViewHolder>() {

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

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindViewHolder(holder: LanguageViewHolder, position: Int, item: LanguageBean?) {
        item ?: return
        val binding = ItemLanguageSetBinding.bind(holder.itemView)
        binding.ivSelector.isSelected = item.isSelect
        binding.tvLanguage.text = item.key
        binding.root.setOnClickListener {
            items.find { it.isSelect }?.isSelect = false
            item.isSelect = true
            notifyDataSetChanged()
        }
    }

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