package com.base.filerecoveryrecyclebin.adapter

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.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_PHOTOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_VIDEOS
import com.base.filerecoveryrecyclebin.bean.FolderBean
import com.base.filerecoveryrecyclebin.databinding.ItemFolderBinding
import com.base.filerecoveryrecyclebin.databinding.ItemFolderRecoveryBinding
import com.base.filerecoveryrecyclebin.utils.LogEx
import com.base.filerecoveryrecyclebin.utils.ScreenUtil.dpToPx
import com.base.filerecoveryrecyclebin.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 FileFolderAdapter(
    private val scanType: Int, private val click: (bean: FolderBean) -> Unit
) : RecyclerView.Adapter<FileFolderAdapter.FF>() {

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

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FF {
        val layout = when (scanType) {
            SCAN_PHOTOS, SCAN_VIDEOS -> R.layout.item_folder_recovery
            else -> R.layout.item_folder
        }
        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 = ItemFolderRecoveryBinding.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.videotu)
                    }
                    request1.into(binding.iv1)

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

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

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

            else -> {
                LogEx.logDebug(TAG, "scanType=$scanType")
                val binding = ItemFolderBinding.bind(holder.itemView)
                binding.tvFolder.text = bean.folder
                binding.tvFileNumber.text = "${bean.recoveryList.size}"
                binding.root.setOnClickListener {
                    click.invoke(bean)
                }
            }
        }


    }

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