package com.easy.clean.ui.main

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.easy.clean.R
import com.easy.clean.bean.FeatureBean
import com.easy.clean.databinding.ItemFeatureSpan1Binding
import com.easy.clean.databinding.ItemFeatureSpan2Binding
import com.easy.clean.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 getItemViewType(position: Int, list: List<FeatureBean>): Int {
        val bean = list[position]
        return bean.span
    }

    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)
                }
            }

            3 -> {
                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 onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): Koko8888 {

        val layout = when (viewType) {
            1 -> R.layout.item_feature_span2
            3 -> R.layout.item_feature_span1
            else -> 0
        }
        return Koko8888(layout.inflate(parent))
    }
}