package rallyedto.dumpedto.editionedto

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.datarecovery.R
import com.base.datarecovery.databinding.DonkeyedtoBinding
import com.base.datarecovery.databinding.KneeledtoBinding
import com.bumptech.glide.Glide
import rallyedto.farewelledto.Strategicedto.inflate
import rallyedto.policeedto.huntedto.ScreenPhotoBean
import rallyedto.policeedto.huntedto.Thatedto


class Falsifyedto(
    val select: (size: Int) -> Unit
) : RecyclerView.Adapter<Falsifyedto.SSS>() {

    var beans = arrayListOf<Thatedto>()

    inner class SSS(view: View) : ViewHolder(view)


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SSS {
        return SSS(R.layout.kneeledto.inflate(parent))
    }

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

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindViewHolder(holder: SSS, position: Int) {
        val binding = KneeledtoBinding.bind(holder.itemView)
        val context = holder.itemView.context
        val bean = beans[position]
        binding.brownedto.text = if (bean.isSelect) "UNSELECT ALL" else "SELECT ALL"
        binding.brownedto.setOnClickListener {
            bean.isSelect = !bean.isSelect
            bean.screenPhotoBean.forEach { subBean -> subBean.isSelect = bean.isSelect }
            notifyItemChanged(position)
            select.invoke(getSelectDataSize())
        }
        binding.beautyedto.text = bean.time
        binding.tireedto.removeAllViews()

        bean.screenPhotoBean.forEach { screenPhotoBean ->

            val itemView = R.layout.donkeyedto.inflate(binding.tireedto)

            val photoBinding = DonkeyedtoBinding.bind(itemView)

            Glide.with(context).load(screenPhotoBean.file).centerCrop().into(photoBinding.usefuledto)
            photoBinding.auntedto.isSelected = screenPhotoBean.isSelect
            arrayOf(photoBinding.root, photoBinding.auntedto).forEach {
                it.setOnClickListener {
                    screenPhotoBean.isSelect = !screenPhotoBean.isSelect
                    photoBinding.auntedto.isSelected = screenPhotoBean.isSelect
                    bean.isSelect = bean.screenPhotoBean.all { it.isSelect }
                    notifyDataSetChanged()
                    select.invoke(getSelectDataSize())
                }
            }
            binding.tireedto.addView(itemView)
        }

        if (bean.screenPhotoBean.size < 3) {
            repeat(2) {
                val itemView = R.layout.donkeyedto.inflate(binding.tireedto)
                itemView.visibility = View.INVISIBLE
                binding.tireedto.addView(itemView)
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(screenshotBean: List<Thatedto>) {
        beans.clear()
        beans.addAll(screenshotBean)
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun toggleSelect(selected: Boolean) {
        beans.forEach {
            it.isSelect = selected
            it.screenPhotoBean.forEach {
                it.isSelect = selected
            }
        }
        notifyDataSetChanged()
    }

    fun isAllSelect(): Boolean {
        return beans.all { bean -> bean.screenPhotoBean.all { it.isSelect } }
    }

    fun getSelectDataSize(): Int {
        return beans.sumOf { bean -> bean.screenPhotoBean.filter { it.isSelect }.size }
    }

    fun getSelectData(): List<ScreenPhotoBean> {
        return beans.flatMap { bean ->
            bean.screenPhotoBean.filter { it.isSelect }
        }
    }
    fun getAllData(): List<ScreenPhotoBean> {
        return beans.flatMap { bean ->
            bean.screenPhotoBean
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(files: List<ScreenPhotoBean>) {
        beans.forEach { screenshotBean ->
            screenshotBean.screenPhotoBean.removeAll(files.toSet())
        }
        beans.removeIf { it.screenPhotoBean.isEmpty() }
        notifyDataSetChanged()
    }

}