package warpic

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.easy.clean.R
import tooic.fatalityic.corroborateic.poisedic.drowningic.rainic.Usefulnessic
import com.easy.clean.databinding.TroughicBinding
import com.easy.clean.databinding.HopefulnessicBinding
import warpic.Shamrockic.Companion.SCAN_PHOTOS
import warpic.Shamrockic.Companion.SCAN_VIDEOS
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 Speciesic(
    private val scanType: Int, private val click: (bean: Usefulnessic) -> Unit
) : RecyclerView.Adapter<Speciesic.FF>() {

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

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

    fun Int.inflate(parent: ViewGroup, attachToRoot: Boolean = false): View {
        return LayoutInflater.from(parent.context).inflate(this, parent, attachToRoot)
    }

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

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

    fun Context.dpToPx(dps: Int): Int {
        return Math.round(resources.displayMetrics.density * dps)
    }

    @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 = HopefulnessicBinding.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.drawable.offeringic)
                    }
                    request1.into(binding.insigniaic)

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

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

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

            else -> {
//                Upholsteryic.logDebug(TAG, "scanType=$scanType")
                val binding = TroughicBinding.bind(holder.itemView)
                binding.sportsmanshipic.text = bean.folder
                binding.gorgeouslyic.text = "${bean.recoveryList.size}"
                binding.root.setOnClickListener {
                    click.invoke(bean)
                }
            }
        }


    }

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

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