package com.example.timert.data.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.timert.R
import com.example.timert.com.example.timert.data.entity.Workout
import com.example.timert.data.TrainingItem
import com.example.timert.data.entity.Training
import com.example.timert.data.trainingWithExercise

class TrainingAdapter( private val onDeleteWorkout: (Int) -> Unit,
                       private val onDeleteTraining: (Int) -> Unit
) : ListAdapter<TrainingItem, RecyclerView.ViewHolder>(DiffCallback())  {

    companion object {
        private const val TYPE_WORKOUT = 0
        private const val TYPE_TRAINING_WITH_EXERCISES = 1
    }

    override fun getItemViewType(position: Int): Int {
        return when (getItem(position)) {
            is TrainingItem.WorkoutItem -> TYPE_WORKOUT
            is TrainingItem.TrainingWithExercisesItem -> TYPE_TRAINING_WITH_EXERCISES
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val view = inflater.inflate(R.layout.training_item_layout, parent, false) // один layout
        return when (viewType) {
            TYPE_WORKOUT -> WorkoutViewHolder(view, onDeleteWorkout)
            TYPE_TRAINING_WITH_EXERCISES -> TrainingViewHolder(view, onDeleteTraining)
            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (val item = getItem(position)) {
            is TrainingItem.WorkoutItem -> (holder as WorkoutViewHolder).bind(item.workout)
            is TrainingItem.TrainingWithExercisesItem -> (holder as TrainingViewHolder).bind(item.trainingWithExercise)
        }
    }

    class WorkoutViewHolder(itemView: View, private val onDelete: (Int) -> Unit) :
        RecyclerView.ViewHolder(itemView) {
        fun bind(workout: Workout) {
            itemView.findViewById<TextView>(R.id.training_text).text = workout.name
            itemView.findViewById<ImageButton>(R.id.deleteButtonTraining).setOnClickListener {
                onDelete(workout.id)
            }
        }
    }

    class TrainingViewHolder(itemView: View, private val onDelete: (Int) -> Unit) :
        RecyclerView.ViewHolder(itemView) {
        fun bind(trainingWithExercise: trainingWithExercise) {
            itemView.findViewById<TextView>(R.id.training_text).text = trainingWithExercise.training.name
            itemView.findViewById<ImageButton>(R.id.deleteButtonTraining).setOnClickListener {
                onDelete(trainingWithExercise.training.id)
            }
        }
    }

    class DiffCallback : DiffUtil.ItemCallback<TrainingItem>() {
        override fun areItemsTheSame(oldItem: TrainingItem, newItem: TrainingItem): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: TrainingItem, newItem: TrainingItem): Boolean {
            return oldItem == newItem
        }
    }
}