package cpm.com.gskmtorange.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cpm.com.gskmtorange.GetterSetter.CategoryPictureGetterSetter
import cpm.com.gskmtorange.R
import cpm.com.gskmtorange.adapter.data.ImageUri
import cpm.com.gskmtorange.databinding.ItemRealtimePdBinding

class RealtimeAdapter(
    val category: String?,
    var context: Context,
    private var subcates: ArrayList<CategoryPictureGetterSetter>?,
    private val btnlistener: BtnClickListener,
) : RecyclerView.Adapter<RealtimeAdapter.CustomViewHolder>() {
    class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = CustomViewHolder(
        ItemRealtimePdBinding.inflate(LayoutInflater.from(context), parent, false).root
    )

    @SuppressLint(
        "NotifyDataSetChanged", "ClickableViewAccessibility",
        "UseCompatLoadingForDrawables", "SetTextI18n"
    )
    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        ItemRealtimePdBinding.bind(holder.itemView).apply {
            val subcat = subcates!![position]
            tvSubCategory.text = "$category-${subcat.suB_CATEGORY}"
            analyticBT.text = context.getString(R.string.view_report_pd)
            analyticBT.setOnClickListener {
                btnlistener.onStartSessionClick(position, subcat)
            }
        }
    }

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

    interface BtnClickListener {
        fun onStartSessionClick(_pos: Int, data: CategoryPictureGetterSetter)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addsubCatItems(langList: ArrayList<CategoryPictureGetterSetter>?) {
        subcates = langList
        notifyDataSetChanged()
    }

    fun updateItem(position: Int, uris: List<ImageUri>) {
        subcates!![position].imageUris = (subcates!![position].imageUris
            ?: ArrayList()).apply { addAll(uris as ArrayList<ImageUri>) }
        notifyItemChanged(position)
    }
}