package com.base.appzxhy.ui.appuninstall

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