package importantedto.deserveedto.manufactureredto.viableedto

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.CurledtoBinding
import com.base.datarecovery.databinding.IntroduceedtoBinding
import com.bumptech.glide.Glide
import importantedto.concreteedto.cameedto.Oxygenedto
import importantedto.concreteedto.cameedto.ScreenPhotoBean
import importantedto.maidedto.witnessedto.nationalismedto.Gestureedto.inflate


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

    var beans = arrayListOf<Oxygenedto>()

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


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

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

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

        bean.screenPhotoBean.forEach { screenPhotoBean ->

            val itemView = R.layout.introduceedto.inflate(binding.batteryedto)

            val photoBinding = IntroduceedtoBinding.bind(itemView)

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

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

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

}