package com.base.pdfreaderallpdfreader.ui.language

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.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.bean.LanguageBean
import com.base.pdfreaderallpdfreader.databinding.ItemLanguageSetBinding
import com.base.pdfreaderallpdfreader.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))
    }
}