package com.simplecleaner.app.ui.appuninstall

import android.annotation.SuppressLint
import android.content.Context
import android.view.ViewGroup
import com.simplecleaner.app.R
import com.simplecleaner.app.bean.AppInfoBean
import com.simplecleaner.app.databinding.ItemAppUninstallBinding
import com.simplecleaner.app.ui.adapter.CommonViewHolder
import com.simplecleaner.app.utils.KotlinExt.toFormatSize
import com.simplecleaner.app.utils.KotlinExt.toFormatTime4
import com.simplecleaner.app.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))
    }
}