package com.casic.br.adapter

import android.content.Context
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.casic.br.R

/**
 * 图片在左，文字在右，九宫格Adapter
 * */
class ConnectGridAdapter(
    context: Context, private val icons: IntArray, private val titles: Array<String>
) : RecyclerView.Adapter<ConnectGridAdapter.ItemViewHolder>() {

    private var layoutInflater: LayoutInflater = LayoutInflater.from(context)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ItemViewHolder(
        layoutInflater.inflate(R.layout.item_connect_rv_g, parent, false)
    )

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.imageView.setImageResource(icons[position])
        holder.textView.text = titles[position]
        holder.itemView.setOnClickListener {
            clickListener?.onItemClick(position)
        }
    }

    override fun getItemCount(): Int = icons.size

    private var clickListener: OnGridItemClickListener? = null

    interface OnGridItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnGridItemClickListener(clickListener: OnGridItemClickListener?) {
        this.clickListener = clickListener
    }

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