package com.tgv.malware.ui.main

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.tgv.malware.R
import com.tgv.malware.bean.FeatureBean
import com.tgv.malware.bean.FeatureBean.Companion.ANTIVIRUS
import com.tgv.malware.bean.FeatureBean.Companion.JUNK_CLEAN
import com.tgv.malware.databinding.ItemFeatureSpan12Binding
import com.tgv.malware.databinding.ItemFeatureSpan1Binding
import com.tgv.malware.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

        if (item.todayUsed) {
            val binding = ItemFeatureSpan1Binding.bind(holder.itemView)
            binding.ivIcon.setImageResource(item.icon)
            binding.tvName.text = context.getString(item.titleRes)
            binding.tvContent.text = context.getString(item.content)
            binding.ll.setBackgroundColor(item.color)
            binding.root.setOnClickListener {
                itemClick?.invoke(item)
            }
        } else {
            val binding = ItemFeatureSpan12Binding.bind(holder.itemView)
            binding.ivIcon.setImageResource(item.icon)
            binding.tvName.text = context.getString(item.titleRes)
            binding.tvContent.text = context.getString(item.content)
            binding.ll.setBackgroundColor(item.color)
            binding.root.setOnClickListener {
                itemClick?.invoke(item)
            }
        }

    }

    override fun getItemViewType(position: Int, list: List<FeatureBean>): Int {
        val bean = list[position]
        return if (!bean.todayUsed && (bean.key == JUNK_CLEAN || bean.key == ANTIVIRUS)) 0 else 1
    }


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