package rallyedto.dumpedto.editionedto

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.BeautifuledtoBinding
import com.base.datarecovery.databinding.GiveedtoBinding
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 rallyedto.farewelledto.Strategicedto.inflate
import rallyedto.policeedto.huntedto.Mediumedto
import rallyedto.policeedto.huntedto.Snuggleedto.SCAN_PHOTOS
import rallyedto.policeedto.huntedto.Snuggleedto.SCAN_VIDEOS
import rallyedto.talledto.blewedto.Musicaledto
import rallyedto.talledto.blewedto.Treasureredto.dpToPx

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

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

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FF {
        val layout = when (scanType) {
            SCAN_PHOTOS, SCAN_VIDEOS -> R.layout.beautifuledto
            else -> R.layout.giveedto
        }
        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 = BeautifuledtoBinding.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.plasticedto)
                    }
                    request1.into(binding.anniversaryedto)

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

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

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

            else -> {
                Musicaledto.logDebug(TAG, "scanType=$scanType")
                val binding = GiveedtoBinding.bind(holder.itemView)
                binding.distributoredto.text = bean.folder
                binding.clearanceedto.text = "${bean.recoveryList.size}"
                binding.root.setOnClickListener {
                    click.invoke(bean)
                }
            }
        }


    }

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