package com.example.timert.com.example.timert.data.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.timert.R
import com.example.timert.com.example.timert.data.entity.Workout



class WorkoutAdapter internal constructor(private val onDeleteClick: (Int) -> Unit
) : RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder>() {
    private var workouts: List<Workout> = emptyList()

    inner class WorkoutViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textViewName: TextView = itemView.findViewById(R.id.training_text)
        val deleteButton: ImageButton = itemView.findViewById(R.id.deleteButtonTraining)
    }

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

    override fun onBindViewHolder(holder: WorkoutViewHolder, position: Int) {
        val currentItem = workouts[position]
        holder.textViewName.text =
            "${currentItem.name}"



        holder.deleteButton.setOnClickListener {
            onDeleteClick(currentItem.id)
        }
    }

    override fun getItemCount() = workouts.size

    fun setWorkouts(workouts: List<Workout>) {
        this.workouts = workouts
        notifyDataSetChanged()
    }
}