package com.example.timert.data.adapter

import android.content.Intent
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.AddWorkout
import com.example.timert.CreatingTraining
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,
                       private val onWorkoutClick: (Workout) -> Unit,
                       private val onTrainingClick: (Training) -> 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, onWorkoutClick)
            TYPE_TRAINING_WITH_EXERCISES -> TrainingViewHolder(view, onDeleteTraining, onTrainingClick)
            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,
                            private val onClick: (Workout) -> 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)
            }

            itemView.setOnClickListener {
                val context = itemView.context
                val intent = Intent(context, AddWorkout::class.java)
                intent.putExtra("workoutId", workout.id)
                context.startActivity(intent)
            }
        }
    }

    class TrainingViewHolder(itemView: View, private val onDelete: (Int) -> Unit,
                             private val onClick: (Training) -> Unit) :
        RecyclerView.ViewHolder(itemView) {
        fun bind(trainingWithExercise: trainingWithExercise) {
            val training = trainingWithExercise.training
            itemView.findViewById<TextView>(R.id.training_text).text = trainingWithExercise.training.name
            itemView.findViewById<ImageButton>(R.id.deleteButtonTraining).setOnClickListener {
                onDelete(trainingWithExercise.training.id)
            }
            itemView.setOnClickListener {
                val context = itemView.context
                val intent = Intent(context, CreatingTraining::class.java)
                intent.putExtra("type", "trainingWithExercises")
                intent.putExtra("trainingId", trainingWithExercise.training.id)
                context.startActivity(intent)
            }
        }
    }

    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
        }
    }
}