package com.rgt.superr.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.rgt.superr.R
import com.rgt.superr.bean.LanguageBean
import com.rgt.superr.databinding.ItemLanguageBinding
import com.rgt.superr.utils.Utils.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class LanguageAdapter(

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

    var click: ((item: LanguageBean) -> Unit)? = null

    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)
        val contxt = holder.itemView.context
        binding.tv.text = item.key
        binding.fl.isSelected = item.isSelect
        binding.ivSelector.isSelected = item.isSelect
        if (item.isSelect) {
            binding.tv.setTextColor(contxt.getColor(R.color.black))
            binding.ivSelector.setImageResource(R.drawable.bg_select_yuan_s_2)
        } else {
            binding.tv.setTextColor(contxt.getColor(R.color.black))
            binding.ivSelector.setImageResource(R.drawable.bg_select_yuan_n)
        }

        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))
    }
}