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

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.app.input.breeze.board.R
import com.app.input.breeze.board.bean.KeyboardTheme
import com.app.input.breeze.board.databinding.FeaturedThemeItemBinding
import com.app.input.breeze.board.utils.AppUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class FeaturedThemeAdapter(
    private val context: android.content.Context,
    private val heroList: List<KeyboardTheme>,
    private val onItemClick: (KeyboardTheme) -> Unit
) : RecyclerView.Adapter<FeaturedThemeAdapter.HeroViewHolder>() {

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

    override fun onBindViewHolder(holder: HeroViewHolder, position: Int) {
        val KeyboardTheme = heroList[position]
        holder.bind(KeyboardTheme)
    }

    override fun getItemCount(): Int = heroList.size

    inner class HeroViewHolder(
        private val binding: FeaturedThemeItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(KeyboardTheme: KeyboardTheme) {
            // 先设置占位图，确保立即显示
            binding.heroImage.setImageResource(R.drawable.place_holder)
            
            // 设置标题（已简化，移除副标题）
            binding.heroTitle.text = KeyboardTheme.titleName

            // 加载图片 - 优先使用预览图，如果没有则使用缩略图
            val imgPath = KeyboardTheme.imgPath
            val imgGif = KeyboardTheme.imgGif
            val thumbGif = KeyboardTheme.thumbGif
            val thumb = KeyboardTheme.thumbUrl

            // 优先使用预览图（imgPath/imgGif），如果没有则使用缩略图（thumbUrl/thumbGif）
            when {
                imgGif != null && imgGif.isNotEmpty() -> {
                    AppUtils.loadWepJif(context, imgGif, binding.heroImage)
                }
                imgPath != null && imgPath.isNotEmpty() -> {
                    val options = RequestOptions()
                        .transform(RoundedCorners(AppUtils.dpToPx(20f)))
                        .centerCrop()
                        .placeholder(R.drawable.place_holder)
                        .error(R.drawable.place_holder)
                    
                    Glide.with(context)
                        .load(imgPath)
                        .apply(options)
                        .into(binding.heroImage)
                }
                thumbGif != null && thumbGif.isNotEmpty() -> {
                    AppUtils.loadWepJif(context, thumbGif, binding.heroImage)
                }
                thumb != null && thumb.isNotEmpty() -> {
                    val options = RequestOptions()
                        .transform(RoundedCorners(AppUtils.dpToPx(20f)))
                        .centerCrop()
                        .placeholder(R.drawable.place_holder)
                        .error(R.drawable.place_holder)
                    
                    Glide.with(context)
                        .load(thumb)
                        .apply(options)
                        .into(binding.heroImage)
                }
                // else 情况已经在开头设置了占位图
            }

            // 点击事件
            binding.root.setOnClickListener {
                onItemClick(KeyboardTheme)
            }
        }
    }
}

