package com.gallery.free.wallpaper

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target

class WpAdapter(
    private val wallpaperList: List<WpItem>,
    private val onItemClick: (Int, WpItem) -> Unit
) : RecyclerView.Adapter<WpAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
        val descriptionText: TextView = itemView.findViewById(R.id.descriptionText)
    }

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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val wallpaper = wallpaperList[position]

        // 设置描述文字
        val displayText = if (wallpaper.description.isNotEmpty()) {
            wallpaper.description
        } else {
            "Unnamed Wallpaper"
        }

        // 截断过长的描述文字
        val maxLength = 50
        val finalText = if (displayText.length > maxLength) {
            displayText.substring(0, maxLength) + "..."
        } else {
            displayText
        }

        holder.descriptionText.text = finalText

        // 加载图片
        if (wallpaper.smallUrl.isNotEmpty()) {
            Glide.with(holder.itemView.context)
                .load(wallpaper.smallUrl)
                .transition(DrawableTransitionOptions.withCrossFade())
                .addListener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable?>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        Log.d("WallpaperAdapter", "Smallnail load failed: ${e?.message}")
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable?>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        Log.d("WallpaperAdapter", "Thumbnail load successful")
                        return false
                    }
                })
                .placeholder(R.drawable.placeho_homepage)
                .error(R.drawable.placeho_homepage)
                .centerCrop()
                .into(holder.imageView)
        } else {
            // 如果 URL 为空，使用本地资源
            holder.imageView.setImageResource(wallpaper.imageRes)
        }

        holder.itemView.setOnClickListener {
            onItemClick(position, wallpaper)
        }
    }

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