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

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.LaundryedtoBinding
import com.base.datarecovery.databinding.LocateedtoBinding
import com.bumptech.glide.Glide
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Ambitionedto.inflate
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.ScreenPhotoBean
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Supplyedto


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

    var beans = arrayListOf<Supplyedto>()

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


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

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

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

        bean.screenPhotoBean.forEach { screenPhotoBean ->

            val itemView = R.layout.locateedto.inflate(binding.blondeedto)

            val photoBinding = LocateedtoBinding.bind(itemView)

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

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

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

}