package importantedto.deserveedto.manufactureredto.viableedto

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.databinding.SacrededtoBinding
import com.base.datarecovery.databinding.ThereedtoBinding
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.concreteedto.cameedto.Changingedto
import importantedto.concreteedto.cameedto.Hateedto.SCAN_PHOTOS
import importantedto.concreteedto.cameedto.Hateedto.SCAN_VIDEOS
import importantedto.gallonedto.Mightedto.dpToPx
import importantedto.gallonedto.Tumbleedto
import importantedto.maidedto.witnessedto.nationalismedto.Gestureedto.inflate

class Earnedto(
    private val scanType: Int, private val click: (bean: Changingedto) -> Unit
) : RecyclerView.Adapter<Earnedto.FF>() {

    private val TAG = "FileFolderAdapter"
    private val beanList = arrayListOf<Changingedto>()

    class FF(view: View) : ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FF {
        val layout = when (scanType) {
            SCAN_PHOTOS, SCAN_VIDEOS -> R.layout.sacrededto
            else -> R.layout.thereedto
        }
        return FF(layout.inflate(parent))
    }

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

    @SuppressLint("SetTextI18n", "CheckResult")
    override fun onBindViewHolder(holder: FF, position: Int) {

        val bean = beanList[position]
        val context = holder.itemView.context

        when (scanType) {
            SCAN_PHOTOS, SCAN_VIDEOS -> {
                val binding = SacrededtoBinding.bind(holder.itemView)
                runCatching {
                    val options = RequestOptions().transform(CenterCrop(), RoundedCorners(context.dpToPx(10)))
                    val image1 = bean.recoveryList[0].path
                    val request1 = Glide.with(context).load(image1).apply(options)
                    if (scanType == SCAN_VIDEOS) {
                        request1.error(R.mipmap.furtheredto)
                    }
                    request1.into(binding.thoughedto)

                    val image2 = bean.recoveryList[1].path
                    val request2 = Glide.with(context).load(image2).apply(options)
                    if (scanType == SCAN_VIDEOS) {
                        request2.error(R.mipmap.furtheredto)
                    }
                    request2.into(binding.possessionedto)

                    val image3 = bean.recoveryList[2].path
                    val request3 = Glide.with(context).load(image3).apply(options)
                    if (scanType == SCAN_VIDEOS) {
                        request3.error(R.mipmap.furtheredto)
                    }
                    request3.into(binding.effectedto)
                }

                binding.tacticedto.text = bean.folder
                binding.harmonyedto.text = "(${bean.recoveryList.size})"
                binding.root.setOnClickListener {
                    click.invoke(bean)
                }
            }

            else -> {
                Tumbleedto.logDebug(TAG, "scanType=$scanType")
                val binding = ThereedtoBinding.bind(holder.itemView)
                binding.tacticedto.text = bean.folder
                binding.becomeedto.text = "${bean.recoveryList.size}"
                binding.root.setOnClickListener {
                    click.invoke(bean)
                }
            }
        }


    }

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