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
import com.casic.br.model.FavoriteModel

/**
 * TODO 现为模拟数据，待后期调整
 * 猜你喜欢，九宫格Adapter
 * */
class FavoriteAdapter(
    context: Context, private val dataRows: MutableList<FavoriteModel>
) : RecyclerView.Adapter<FavoriteAdapter.ItemViewHolder>() {

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

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

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.deviceNameView.text = dataRows[position].deviceName
//        holder.descriptionView.text = dataRows[position].deviceDesc
        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 deviceNameView: TextView = itemView.findViewById(R.id.deviceNameView)
//        val descriptionView: TextView = itemView.findViewById(R.id.descriptionView)
    }
}