package com.base.scanqrclear.luma

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.base.scanqrclear.R
import com.base.scanqrclear.databinding.ItemFunction2Binding
import com.base.scanqrclear.luma.AppHelps.dpToPx

class FunctionAdapter(
    data: List<FunctionBean>,
    private val isShowHot: Boolean = false
) : BaseAdapter<FunctionBean, ItemFunction2Binding>(data) {

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup
    ): ItemFunction2Binding {
        return ItemFunction2Binding.inflate(layoutInflater, parent, false)
    }

    override fun bind(
        holder: BaseViewHolder<ItemFunction2Binding>,
        position: Int,
        item: FunctionBean
    ) {

        if (!isShowHot) {
            val width = holder.itemView.context.resources.displayMetrics.widthPixels
            val margin = holder.itemView.context.dpToPx(58).toInt()
            val imageWidth = (width - margin) / 3
            holder.itemView.layoutParams.width = imageWidth
            holder.itemView.layoutParams.height = (imageWidth * 0.85).toInt()
//            holder.itemView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        holder.binding.ivIcon.setImageResource(item.icon)
        holder.binding.tvHot.visibility = if (isShowHot && item.name == R.string.junk_scan) View.VISIBLE else View.GONE
        val name = holder.itemView.context.getString(item.name)
        holder.binding.tvName.text = name
        if (item.backgroundId != 0)
            holder.itemView.setBackgroundResource(item.backgroundId)
        holder.itemView.setOnClickListener { callback?.onClicked(it, position, item) }
    }
}