package com.ritoq.quickphone.ui.main

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.ritoq.quickphone.R
import com.ritoq.quickphone.bean.FeatureBean
import com.ritoq.quickphone.databinding.ItemFeatureSpan1Binding
import com.ritoq.quickphone.databinding.ItemFeatureSpan2Binding
import com.ritoq.quickphone.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.ivBg.setBackgroundResource(item.bg)
                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)

                if (item.showTitle) {
                    binding.tvTitle.visibility = View.VISIBLE
                } else {
                    binding.tvTitle.visibility = View.GONE
                }

                binding.card.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))
    }
}