package com.rgt.superr.ui.appuninstall

import android.annotation.SuppressLint
import android.content.Context
import android.view.ViewGroup
import com.rgt.superr.R
import com.rgt.superr.bean.AppInfoBean
import com.rgt.superr.databinding.ItemAppUninstallBinding
import com.rgt.superr.ui.adapter.CommonViewHolder
import com.rgt.superr.utils.KotlinExt.toFormatSize
import com.rgt.superr.utils.KotlinExt.toFormatTime4
import com.rgt.superr.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class AppAdapter : BaseQuickAdapter<AppInfoBean, CommonViewHolder>() {

    var uninstallAction: ((item: AppInfoBean) -> Unit)? = null

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: CommonViewHolder,
        position: Int,
        item: AppInfoBean?
    ) {
        item ?: return
        val binding = ItemAppUninstallBinding.bind(holder.itemView)

        binding.ivIcon.setImageDrawable(item.icon)
        binding.tvName.text = item.appName
        binding.tvInfo.text = "${item.storageSize.toFormatSize()} ${item.installTime.toFormatTime4()}"
        binding.tvBtn.setOnClickListener {
            uninstallAction?.invoke(item)
        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): CommonViewHolder {
        return CommonViewHolder(R.layout.item_app_uninstall.inflate(parent))
    }
}