package com.divya.animaltap.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.divya.animaltap.R
import com.divya.animaltap.model.Animal
import com.google.android.material.card.MaterialCardView

class AnimalAdapter(
    private val animals: List<Animal>,
    private val onAnimalClick: (Animal) -> Unit
) : RecyclerView.Adapter<AnimalAdapter.AnimalViewHolder>() {

    class AnimalViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val cardView: MaterialCardView = itemView.findViewById(R.id.card_view)
        val image: ImageView = itemView.findViewById(R.id.img_animal_icon)
        val name: TextView = itemView.findViewById(R.id.txt_animal_name)
    }

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

    override fun onBindViewHolder(holder: AnimalViewHolder, position: Int) {
        val animal = animals[position]
        
        holder.name.text = holder.itemView.context.getString(animal.nameRes)
        holder.image.setImageResource(animal.imageRes)
        holder.cardView.setCardBackgroundColor(ContextCompat.getColor(holder.itemView.context, animal.colorRes))
        
        holder.itemView.setOnClickListener {
            onAnimalClick(animal)
            // Add simple scale animation
            holder.itemView.animate().scaleX(0.9f).scaleY(0.9f).setDuration(100).withEndAction {
                holder.itemView.animate().scaleX(1f).scaleY(1f).setDuration(100).start()
            }.start()
        }
    }

    override fun getItemCount(): Int = animals.size
}
