package rowedto.jockeyedto.sheetedto.meadowedto.naturaledto.idealedto

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.FameedtoBinding
import com.base.datarecovery.databinding.StumbleedtoBinding
import com.bumptech.glide.Glide
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Ambitionedto.inflate
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Cunningedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Suitcaseedto.toFormatSize
import java.io.File

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

    private val beanList = arrayListOf<Cunningedto>()

    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.stumbleedto else R.layout.fameedto
        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 = StumbleedtoBinding.bind(holder.itemView)

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

        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(pathList: List<Cunningedto>) {
        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() }
    }
}