package com.example.defenseapplication.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.defenseapplication.databinding.ItemAddLinkedUserBinding

data class UserItem(val name: String, val phone: String)

class AddLinkedUserAdapter(
    private val data: List<UserItem>,
    private val selectedIds: MutableSet<String>,
    private val onItemClick: (String, Boolean) -> Unit
) : RecyclerView.Adapter<AddLinkedUserAdapter.ViewHolder>() {

    inner class ViewHolder(val binding: ItemAddLinkedUserBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: UserItem) {
            binding.tvUserName.text = "${item.name}-${item.phone}"
            binding.cbSelect.isChecked = selectedIds.contains(item.phone)
            binding.cbSelect.setOnClickListener {
                val isChecked = binding.cbSelect.isChecked
                onItemClick(item.phone, isChecked)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemAddLinkedUserBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(data[position])
    }

    override fun getItemCount(): Int = data.size
}