package rallyedto.dumpedto.editionedto

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.BuyedtoBinding
import com.base.datarecovery.databinding.LawyeredtoBinding
import com.bumptech.glide.Glide
import rallyedto.battleedto.acresedto.Fireplaceedto.toFormatSize
import rallyedto.farewelledto.Strategicedto.inflate
import rallyedto.policeedto.huntedto.Outletedto
import java.io.File

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

    private val beanList = arrayListOf<Outletedto>()

    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.buyedto else R.layout.lawyeredto
        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 = BuyedtoBinding.bind(holder.itemView)

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

        }
    }

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