package com.tgv.malware.ui.main

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.tgv.malware.R
import com.tgv.malware.bean.FeatureBean
import com.tgv.malware.databinding.ItemFeatureRvBinding
import com.tgv.malware.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class FeatureAdapter : BaseQuickAdapter<List<FeatureBean>, FeatureAdapter.Koko6666>() {

    inner class Koko6666(view: View) : ViewHolder(view)

    var itemClick: ((bean: FeatureBean) -> Unit)? = null

    override fun onBindViewHolder(holder: Koko6666, position: Int, item: List<FeatureBean>?) {
        item ?: return
        val binding = ItemFeatureRvBinding.bind(holder.itemView)
        val context = holder.itemView.context

        binding.rv.layoutManager = GridLayoutManager(context, 3).apply {
            spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position2: Int): Int {
                    val featureBean = item[position2]
                    return featureBean.span
                }
            }
        }
        val adapter = FeatureItemAdapter()
        adapter.itemClick = this.itemClick
        binding.rv.adapter = adapter
        adapter.submitList(item)
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): Koko6666 {
        return Koko6666(R.layout.item_feature_rv.inflate(parent))
    }

}