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

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.led.scroller.barrage.banner.R
import com.led.scroller.barrage.banner.bean.FontColorBean
import com.led.scroller.barrage.banner.databinding.AdapterColorItemBinding

class FontColorAdapter(
    private val context: Context,
    private val colorList: MutableList<FontColorBean>,
) :
    RecyclerView.Adapter<FontColorAdapter.ColorViewHolder>() {

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

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: ColorViewHolder, position: Int) {
        val bean = colorList[position]
        holder.binding.bgView.setCircleColor(bean.colorString)
        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 colorList.size
    }

    inner class ColorViewHolder(val binding: AdapterColorItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    private var itemClickListener: OnItemClickListener? = null

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

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