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.NationedtoBinding
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Ambitionedto.inflate
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Cunningedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Sisteredto

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

    private val beanList = arrayListOf<Sisteredto>()

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

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NNN {
        return NNN(R.layout.nationedto.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 = NationedtoBinding.bind(holder.itemView)
//        val context = holder.itemView.context
        val bean = beanList[position]

        if (payloads.isEmpty()) {

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

            binding.relationshipedto.isSelected = bean.isSelect

            val subAdapter = Exerciseedto().apply {
                subClickAction = { clickAction.invoke(this@Complainedto.getSelectData()) }
            }
            binding.spilledto.adapter = subAdapter
            subAdapter.setData(bean.beans)

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

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

    }

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

        clickAction.invoke(getSelectData())
    }

    fun getSelectData(): Pair<Boolean, List<Cunningedto>> {
        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<Sisteredto>) {
        beanList.clear()
        beanList.addAll(list)
        notifyDataSetChanged()
        clickAction.invoke(getSelectData())
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(cleanList: List<Cunningedto>) {
        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()
    }

}