package com.pan_american.android.data.model.common.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckedTextView
import androidx.recyclerview.widget.RecyclerView
import com.pan_american.android.R
import com.pan_american.android.data.model.common.entity.SiftListItem

class SiftListAdapter(private val listItem: List<SiftListItem>, private val layoutId: Int): RecyclerView.Adapter<SiftListAdapter.ViewHolder>() {

    lateinit var onRecyclerViewItemClick: OnRecyclerViewItemClick<SiftListItem>

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val checkedTextView: CheckedTextView = view.findViewById(R.id.customer_sift)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(layoutId, parent, false)

        return ViewHolder(view)
    }

    override fun getItemCount() = listItem.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = listItem[position]
        holder.checkedTextView.text = item.name
        holder.checkedTextView.isChecked = item.isChecked
        holder.checkedTextView.setOnClickListener {
            onRecyclerViewItemClick.onItemClick(position)
            if(item.isChecked) {
                holder.checkedTextView.isChecked = false
                item.isChecked = false
            } else {
                holder.checkedTextView.isChecked = true
                item.isChecked = true
            }
        }
    }

    interface OnRecyclerViewItemClick<T> {
        fun onItemClick(position: Int)
    }
}