package com.simplecleaner.app.ui.main

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.simplecleaner.app.R
import com.simplecleaner.app.bean.FeatureBean
import com.simplecleaner.app.databinding.ItemFeatureSpan1Binding
import com.simplecleaner.app.databinding.ItemFeatureSpan2Binding
import com.simplecleaner.app.utils.Utils.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class FeatureItemAdapter : BaseQuickAdapter<FeatureBean, FeatureItemAdapter.Koko8888>() {

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

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

    override fun onBindViewHolder(holder: Koko8888, position: Int, item: FeatureBean?) {
        item ?: return
        val context = holder.itemView.context
        when(item.span){
            1->{
                val binding = ItemFeatureSpan2Binding.bind(holder.itemView)
                binding.ivIcon.setImageResource(item.icon)
                binding.tvName.text = context.getString(item.titleRes)
                binding.root.setOnClickListener {
                    itemClick?.invoke(item)
                }
            }
            2->{
                val binding = ItemFeatureSpan1Binding.bind(holder.itemView)
                binding.ivIcon.setImageResource(item.icon)
                binding.tvName.text = context.getString(item.titleRes)
                binding.root.setOnClickListener {
                    itemClick?.invoke(item)
                }
            }
        }


    }

    override fun getItemViewType(position: Int, list: List<FeatureBean>): Int {
        val bean = list[position]
        return bean.span
    }


    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): Koko8888 {
        val layout = if (viewType == 1) R.layout.item_feature_span2 else R.layout.item_feature_span1
        return Koko8888(layout.inflate(parent))
    }
}