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.WeaponedtoBinding
import importantedto.concreteedto.cameedto.Commentedto
import importantedto.concreteedto.cameedto.Vigorousedto
import importantedto.maidedto.witnessedto.nationalismedto.Gestureedto.inflate

class Eagleedto(private val clickAction: (Pair<Boolean, List<Vigorousedto>>) -> Unit) : RecyclerView.Adapter<Eagleedto.NNN>() {

    private val beanList = arrayListOf<Commentedto>()

    inner class NNN(view: View) : ViewHolder(view) {

    }

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

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

    override fun onBindViewHolder(holder: NNN, position: Int) {}

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: NNN, position: Int, payloads: MutableList<Any>) {
        val binding = WeaponedtoBinding.bind(holder.itemView)
//        val context = holder.itemView.context
        val bean = beanList[position]

        if (payloads.isEmpty()) {

            binding.eatedto.text = bean.time + " (${bean.beans.size})"

            binding.countedto.isSelected = bean.isSelect

            val subAdapter = Characteredto().apply {
                subClickAction = { clickAction.invoke(this@Eagleedto.getSelectData()) }
            }
            binding.reactionedto.adapter = subAdapter
            subAdapter.setData(bean.beans)

            binding.prosecutionedto.setOnClickListener {
                bean.isSelect = !bean.isSelect
                binding.countedto.isSelected = bean.isSelect
                notifyItemChanged(position, "222")
                toggleSubList(bean, subAdapter)
            }

        } else {
            binding.countedto.isSelected = bean.isSelect
            super.onBindViewHolder(holder, position, payloads)
        }

    }

    @SuppressLint("NotifyDataSetChanged")
    private fun toggleSubList(bean: Commentedto, subAdapter: Characteredto) {
        bean.beans.forEach { it.isSelect = bean.isSelect }
        subAdapter.notifyDataSetChanged()

        clickAction.invoke(getSelectData())
    }

    fun getSelectData(): Pair<Boolean, List<Vigorousedto>> {
        val flatMap = beanList.flatMap { it.beans }
        val allSelect = flatMap.all { it.isSelect }
        val pathList = flatMap.filter { it.isSelect }
        return Pair(allSelect, pathList)
    }


    @SuppressLint("NotifyDataSetChanged")
    fun toggleAllSelect(select: Boolean) {
        beanList.forEach { it.isSelect = select }
        beanList.flatMap { it.beans }.forEach { it.isSelect = select }
        notifyDataSetChanged()
        clickAction.invoke(getSelectData())
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: List<Commentedto>) {
        beanList.clear()
        beanList.addAll(list)
        notifyDataSetChanged()
        clickAction.invoke(getSelectData())
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(cleanList: List<Vigorousedto>) {
        beanList.forEach {
            val iterator = it.beans.listIterator()
            while (iterator.hasNext()) {
                val bean = iterator.next()
                if (cleanList.contains(bean)) {
                    iterator.remove()
                }
            }
        }
        beanList.removeIf { it.beans.size <= 1 }
        notifyDataSetChanged()
    }

    fun isEmptyData(): Boolean {
        return beanList.isEmpty()
    }

}