package importantedto.deserveedto.manufactureredto.viableedto

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.base.datarecovery.R
import com.base.datarecovery.databinding.AdoptionedtoBinding
import com.base.datarecovery.databinding.TheoristedtoBinding
import com.bumptech.glide.Glide
import importantedto.afteredto.constitutionaledto.Constraintedto.toFormatSize
import importantedto.concreteedto.cameedto.Vigorousedto
import importantedto.maidedto.witnessedto.nationalismedto.Gestureedto.inflate
import java.io.File

class Workshopedto(
    private val isList: Boolean = false,
    private val click: (allSelect: Boolean) -> Unit
) : RecyclerView.Adapter<Workshopedto.WhatMedia>() {

    private val beanList = arrayListOf<Vigorousedto>()

    class WhatMedia(view: View) : RecyclerView.ViewHolder(view)


    override fun getItemViewType(position: Int): Int {
        return if (isList) 0 else 1
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WhatMedia {
        val layout = if (viewType == 0) R.layout.theoristedto else R.layout.adoptionedto
        return WhatMedia(layout.inflate(parent))
    }

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

    override fun onBindViewHolder(holder: WhatMedia, position: Int) {

    }

    override fun onBindViewHolder(holder: WhatMedia, position: Int, payloads: MutableList<Any>) {


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

        if (isList) {
            val binding = TheoristedtoBinding.bind(holder.itemView)

            if (payloads.isEmpty()) {
                binding.deficitedto.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.forwardedto))
                val file = File(bean.path)
                binding.anotheredto.text = file.name
                binding.ignoranceedto.text = file.length().toFormatSize()
                binding.countedto.isSelected = bean.isSelect
                binding.root.setOnClickListener {
                    binding.countedto.isSelected = !binding.countedto.isSelected
                    bean.isSelect = binding.countedto.isSelected
                    notifyItemChanged(position, "sdd")
                    click.invoke(beanList.all { it.isSelect })
                }
            } else {
                binding.countedto.isSelected = bean.isSelect
                super.onBindViewHolder(holder, position, payloads)
            }
        } else {
            val binding = AdoptionedtoBinding.bind(holder.itemView)
            if (payloads.isEmpty()) {
                Glide.with(context).load(bean.path).centerCrop().into(binding.deficitedto)
                binding.countedto.isSelected = bean.isSelect
                binding.root.setOnClickListener {
                    binding.countedto.isSelected = !binding.countedto.isSelected
                    bean.isSelect = binding.countedto.isSelected
                    notifyItemChanged(position, "sdd")
                    click.invoke(beanList.all { it.isSelect })
                }
            } else {
                binding.countedto.isSelected = bean.isSelect
                super.onBindViewHolder(holder, position, payloads)
            }

        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    fun toggleSelect(select: Boolean) {
        beanList.forEach { it.isSelect = select }
        notifyDataSetChanged()
    }

    fun getSelectData(): List<String> {
        return beanList.filter { it.isSelect }.map { it.path }
    }

    fun getSelectDataSize(): Long {
        return getSelectData().map { File(it) }.sumOf { it.length() }
    }
}