package com.rugvedsarika.easymath.ui.achievements

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.rugvedsarika.easymath.R
import com.rugvedsarika.easymath.data.db.AchievementEntity
import com.rugvedsarika.easymath.databinding.ItemAchievementBinding

class AchievementsAdapter : ListAdapter<AchievementEntity, AchievementsAdapter.AchievementViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AchievementViewHolder {
        val binding = ItemAchievementBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return AchievementViewHolder(binding)
    }

    override fun onBindViewHolder(holder: AchievementViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    class AchievementViewHolder(private val binding: ItemAchievementBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: AchievementEntity) {
            binding.textTitle.text = item.name
            binding.textDescription.text = item.description
            
            // Placeholder icon logic (using system drawables for now as defined in Manager)
            // binding.imgIcon.setImageResource(item.iconResId) 
            // Better: use a mapping or specific resource. unique code -> drawable
            
            if (item.unlocked) {
                binding.textStatus.text = "Unlocked"
                binding.textStatus.setTextColor(ContextCompat.getColor(binding.root.context, R.color.white))
                binding.textTitle.setTextColor(ContextCompat.getColor(binding.root.context, R.color.white))
                binding.textDescription.setTextColor(ContextCompat.getColor(binding.root.context, R.color.white))
                binding.imgIcon.setColorFilter(ContextCompat.getColor(binding.root.context, R.color.white))
                binding.layoutContainer.setBackgroundResource(R.drawable.bg_gradient_trophies)
                binding.cardAchievement.alpha = 1.0f
            } else {
                binding.textStatus.text = "Locked"
                binding.textStatus.setTextColor(ContextCompat.getColor(binding.root.context, R.color.wrong_red))
                binding.textTitle.setTextColor(ContextCompat.getColor(binding.root.context, R.color.black))
                binding.textDescription.setTextColor(ContextCompat.getColor(binding.root.context, R.color.text_secondary))
                binding.imgIcon.setColorFilter(ContextCompat.getColor(binding.root.context, android.R.color.darker_gray))
                binding.layoutContainer.setBackgroundResource(android.R.color.white)
                binding.cardAchievement.alpha = 0.7f
            }
        }
    }

    class DiffCallback : DiffUtil.ItemCallback<AchievementEntity>() {
        override fun areItemsTheSame(oldItem: AchievementEntity, newItem: AchievementEntity) = oldItem.code == newItem.code
        override fun areContentsTheSame(oldItem: AchievementEntity, newItem: AchievementEntity) = oldItem == newItem
    }
}
