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 androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.casic.br.R
import com.casic.br.model.FeaturedGoodsModel

/**
 * TODO 现为模拟数据，待后期调整
 * 精选好物，九宫格Adapter
 * */
class FeaturedGoodsAdapter(
    private val context: Context, private val dataRows: MutableList<FeaturedGoodsModel>
) : RecyclerView.Adapter<FeaturedGoodsAdapter.ItemViewHolder>() {

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

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

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        Glide.with(context)
            .load(dataRows[position].deviceImage)
            .placeholder(R.mipmap.load_image_error)
            .into(holder.deviceLogoView)
//        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 deviceLogoView: ImageView = itemView.findViewById(R.id.deviceLogoView)
//        val deviceNameView: TextView = itemView.findViewById(R.id.deviceNameView)
//        val descriptionView: TextView = itemView.findViewById(R.id.descriptionView)
    }
}