package com.webtoonviewer.ui.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.recyclerview.widget.RecyclerView
import com.webtoonviewer.R
import com.webtoonviewer.ui.data.CheckboxItem
import kotlin.math.log

class TagAdapter(
    private val items: List<CheckboxItem>,
    private val onCheckedChangeListener: (position: Int, isChecked: Boolean) -> Unit
) : RecyclerView.Adapter<TagAdapter.MenuViewHolder>() {

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

    override fun onBindViewHolder(holder: MenuViewHolder, position: Int) {
        val item = items[position]
        Log.d("ithi", "onBindViewHolder: $item")
        holder.checkBox.isChecked = item.isChecked
        holder.checkBox.text = item.text

        holder.checkBox.setOnCheckedChangeListener { _, isChecked ->
            onCheckedChangeListener(position, isChecked)
        }
    }

    override fun getItemCount() = items.size

    class MenuViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val checkBox: CheckBox = itemView.findViewById(R.id.checkBoxDialogMainSettings)
    }

    fun setItemsChecked() {
        items.forEach {
            it.isChecked = true
        }
        notifyDataSetChanged()
    }
}