package com.example.timert.data.adapter

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.data.entity.Exercise


//class ExerciseAdapter internal constructor(context: Context):RecyclerView.Adapter<ExerciseAdapter.ExerciseViewHolder>(){
class ExerciseAdapter(
    private val onDeleteClick: (Int) -> Unit
) : RecyclerView.Adapter<ExerciseAdapter.ExerciseViewHolder>() {
    private var exerciseList: List<Exercise> = emptyList()




    inner class ExerciseViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        val textViewExercise: TextView = itemView.findViewById(R.id.textView13)
        val deleteButton: ImageButton = itemView.findViewById(R.id.deleteButton)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExerciseViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.resyclerview_item, parent, false)
        return ExerciseViewHolder(view)
    }

    override fun onBindViewHolder(holder: ExerciseViewHolder, position: Int) {

        val exercise = exerciseList[position]

        holder.textViewExercise.text =
            "${exercise.name_exercise} — работа: ${exercise.num_sets} сек, отдых: ${exercise.rest_of_sets} сек"
        holder.deleteButton.setOnClickListener {
            onDeleteClick(exercise.id)
        }
    }

    override fun getItemCount() = exerciseList.size

    fun setExercises(exercises: List<Exercise>) {
       this.exerciseList = exercises
        notifyDataSetChanged()
    }


}