package importantedto.deserveedto.manufactureredto.viableedto

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.databinding.ChairedtoBinding
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
import importantedto.afteredto.constitutionaledto.Constraintedto.toFormatSize
import importantedto.concreteedto.cameedto.Highlightedto
import importantedto.gallonedto.Mightedto.dpToPx
import importantedto.maidedto.witnessedto.nationalismedto.Gestureedto.inflate

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

    private val beanList = arrayListOf<Highlightedto>()

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

    }

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

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

    override fun onBindViewHolder(holder: Whats, position: Int) {
        val binding = ChairedtoBinding.bind(holder.itemView)
        val context = holder.itemView.context
        val bean = beanList[position]
        binding.deficitedto.setImageDrawable(ContextCompat.getDrawable(context, bean.icon))
        binding.assaultedto.text = bean.tittle
        if (bean.subFile.isEmpty()) {
            binding.heavenedto.visibility = View.GONE
            binding.ignoranceedto.setTextColor(Color.parseColor("#999999"))
            binding.jawedto.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.breadedto))
            binding.facilityedto.visibility = View.VISIBLE
        } else {
            binding.heavenedto.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.thoughedto)

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

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

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


}