package com.led.scroller.barrage.banner.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import com.led.scroller.barrage.banner.R
import com.led.scroller.barrage.banner.bean.FontBean
import com.led.scroller.barrage.banner.databinding.AdapterFontItemBinding

class FontAdapter(
    private val context: Context,
    private val fontList: MutableList<FontBean>,
) :
    RecyclerView.Adapter<FontAdapter.FontViewHolder>() {

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

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: FontViewHolder, position: Int) {
        val bean = fontList[position]
        val typeface = ResourcesCompat.getFont(context, bean.id)
        holder.binding.textView.typeface = typeface
        if (bean.select) {
            holder.binding.selectView.background =
                context.getDrawable(R.drawable.drw_round_stroke_selected)
        } else {
            holder.binding.selectView.background =
                context.getDrawable(R.drawable.drw_round_stroke_unselected)
        }
        holder.itemView.setOnClickListener {
            if (itemClickListener != null) {
                itemClickListener?.onItemClick(position)
            }
        }
    }

    override fun getItemCount(): Int {
        return fontList.size
    }

    inner class FontViewHolder(val binding: AdapterFontItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    private var itemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        itemClickListener = listener
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }
}