package com.app.input.breeze.board.listutils

import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.app.input.breeze.board.R
import com.app.input.breeze.board.databinding.QuickCategoryItemBinding

class QuickCategoryAdapter(
    private val context: android.content.Context,
    private val categories: List<Pair<String, String>>, // (名称, emoji)
    private val onCategoryClick: (String) -> Unit
) : RecyclerView.Adapter<QuickCategoryAdapter.CategoryViewHolder>() {

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

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        val (name, emoji) = categories[position]
        holder.bind(name, emoji)
    }

    override fun getItemCount(): Int = categories.size

    inner class CategoryViewHolder(
        private val binding: QuickCategoryItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(name: String, emoji: String) {
            binding.categoryEmoji.text = emoji
            binding.categoryName.text = name

            // 根据分类名称设置不同的渐变背景（更精致的多色渐变）
            val gradient = GradientDrawable().apply {
                orientation = GradientDrawable.Orientation.TL_BR
                cornerRadius = 22f * context.resources.displayMetrics.density
                colors = getCategoryColors(name)
            }
            binding.categoryBg.background = gradient

            binding.root.setOnClickListener {
                onCategoryClick(name)
            }
        }

        private fun getCategoryColors(categoryName: String): IntArray {
            // 降低饱和度10-20%，使用同色系不同明度，更成熟优雅
            return when (categoryName.lowercase()) {
                "cute" -> intArrayOf(
                    0xFFFFB3D9.toInt(),  // 降低饱和度：浅粉
                    0xFFFFC5E5.toInt(),  // 中粉
                    0xFFFFD9F0.toInt()  // 更浅粉
                )
                "neon" -> intArrayOf(
                    0xFFB794F6.toInt(),  // 降低饱和度：柔紫
                    0xFFA78BEA.toInt(),  // 中柔紫
                    0xFF9678E0.toInt()  // 深柔紫
                )
                "cool" -> intArrayOf(
                    0xFF6BB6FF.toInt(),  // 降低饱和度：柔蓝
                    0xFF5AA3F0.toInt(),  // 中柔蓝
                    0xFF4A90E0.toInt()  // 深柔蓝
                )
                "aesthetic" -> intArrayOf(
                    0xFFFFD699.toInt(),  // 降低饱和度：柔橙
                    0xFFFFE0B3.toInt(),  // 中柔橙
                    0xFFFFEACC.toInt()  // 浅柔橙
                )
                "love" -> intArrayOf(
                    0xFFF5A3C7.toInt(),  // 降低饱和度：柔玫红
                    0xFFFFB3D9.toInt(),  // 中柔粉
                    0xFFFFC5E5.toInt()  // 浅柔粉
                )
                "gravity" -> intArrayOf(
                    0xFF9D8FEB.toInt(),  // 降低饱和度：柔紫蓝
                    0xFF8B7DD8.toInt(),  // 中柔紫蓝
                    0xFF7A6BC5.toInt()  // 深柔紫蓝
                )
                else -> intArrayOf(
                    0xFFB0BEC5.toInt(),  // 降低饱和度：柔灰蓝
                    0xFFCFD8DC.toInt(),  // 中柔灰蓝
                    0xFFE1E8ED.toInt()  // 浅柔灰蓝
                )
            }
        }
    }
}

