package com.base.datarecovery.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.bean.WhatsAppCleanerBean
import com.base.datarecovery.databinding.ItemWhatsappBinding
import com.base.datarecovery.help.KotlinExt.toFormatSize
import com.base.datarecovery.utils.ScreenUtil.dpToPx
import com.base.datarecovery.view.XmlEx.inflate
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class WhatsAppCleanerAdapter(val click: (bean: WhatsAppCleanerBean) -> Unit) : RecyclerView.Adapter<WhatsAppCleanerAdapter.Whats>() {

    private val beanList = arrayListOf<WhatsAppCleanerBean>()

    class Whats(view: View) : ViewHolder(view) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Whats {
        return Whats(R.layout.item_whatsapp.inflate(parent))
    }

    override fun getItemCount(): Int {
        return beanList.size
    }

    override fun onBindViewHolder(holder: Whats, position: Int) {
        val binding = ItemWhatsappBinding.bind(holder.itemView)
        val context = holder.itemView.context
        val bean = beanList[position]
        binding.iv.setImageDrawable(ContextCompat.getDrawable(context, bean.icon))
        binding.tvTitle.text = bean.tittle
        if (bean.subFile.isEmpty()) {
            binding.llIv.visibility = View.GONE
            binding.tvSize.setTextColor(Color.parseColor("#999999"))
            binding.ivArrow.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.jianotu_whatsapp))
            binding.tvNoMessage.visibility = View.VISIBLE
        } else {
            binding.llIv.visibility = View.VISIBLE
            runCatching {
                val options = RequestOptions().transform(CenterCrop(), RoundedCorners(context.dpToPx(10)))
                val image1 = bean.subFile[0]
                Glide.with(context).load(image1).apply(options).into(binding.iv1)

                val image2 = bean.subFile[1]
                Glide.with(context).load(image2).apply(options).into(binding.iv2)

                val image3 = bean.subFile[2]
                Glide.with(context).load(image3).apply(options).into(binding.iv3)
            }
            binding.tvSize.setTextColor(Color.parseColor("#355BEA"))
            binding.tvSize.text = bean.size.toFormatSize()
            binding.ivArrow.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.jianotul_whatsapp))
            binding.tvNoMessage.visibility = View.GONE
        }
        binding.root.setOnClickListener {
            if (bean.subFile.isEmpty()) {
                return@setOnClickListener
            }
            click.invoke(bean)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: List<WhatsAppCleanerBean>) {
        beanList.clear()
        beanList.addAll(list)
        notifyDataSetChanged()
    }


}