package com.casic.br.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.casic.br.R

/**
 * 主页功能，九宫格Adapter
 * */
class HomeGridAdapter(
    context: Context, private val dataRows: Array<String>
) : RecyclerView.Adapter<HomeGridAdapter.ItemViewHolder>() {

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

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

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.homeFunctionView.text = dataRows[position]
        holder.itemView.setOnClickListener {
            clickListener?.onItemClick(position)
        }
    }

    override fun getItemCount(): Int = dataRows.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 homeFunctionView: TextView = itemView.findViewById(R.id.homeFunctionView)
    }
}