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.bean.KeyboardTheme
import com.app.input.breeze.board.databinding.TrendingThemeItemBinding
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 TrendingThemeAdapter(
    private val context: android.content.Context,
    private val trendingList: List<KeyboardTheme>,
    private val onItemClick: (KeyboardTheme) -> Unit
) : RecyclerView.Adapter<TrendingThemeAdapter.TrendingViewHolder>() {

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

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

    override fun getItemCount(): Int = trendingList.size

    inner class TrendingViewHolder(
        private val binding: TrendingThemeItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(KeyboardTheme: KeyboardTheme) {
            val thumbGif = KeyboardTheme.thumbGif
            val thumb = KeyboardTheme.thumbUrl

            val options = RequestOptions()
                .transform(RoundedCorners(AppUtils.dpToPx(16f)))

            if (thumbGif != null && thumbGif.isNotEmpty()) {
                AppUtils.loadWepJif(context, thumbGif, binding.trendingImage)
            } else {
                Glide.with(context)
                    .load(thumb)
                    .apply(options)
                    .into(binding.trendingImage)
            }

            binding.root.setOnClickListener {
                onItemClick(KeyboardTheme)
            }
        }
    }
}

