package com.example.smartlab.Adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.smartlab.Model.CatalogItem
import com.example.smartlab.R
import com.example.smartlab.databinding.PartCategoryBinding

class AdapterCategory(val listener: Listener) :
    RecyclerView.Adapter<AdapterCategory.CategoryHolder>() {
    var categoryList = ArrayList<String>()
    var selectedRBPosition: Int = 0

    class CategoryHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val binding = PartCategoryBinding.bind(itemView)
        val objectCategory = binding.objectCategoryName
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.part_category, parent, false)
        return CategoryHolder(itemView)
    }

    override fun getItemCount(): Int {
        return categoryList.size
    }

    override fun onBindViewHolder(holder: CategoryHolder, position: Int) {
        val item = holder.objectCategory
        item.text = (categoryList[position])
        item.isChecked = selectedRBPosition == position
        item.setOnClickListener {
            selectedRBPosition = holder.adapterPosition
            listener.onClick(position, item)
            notifyDataSetChanged()
        }
    }
    fun addCategory(name: String) {
        categoryList.add(name)
        notifyDataSetChanged()
    }

    fun newList()
    {
        categoryList = ArrayList<String>()
        notifyDataSetChanged()
    }

    interface Listener {
        fun onClick(position: Int, item: RadioButton)
    }

}
