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.FlooredtoBinding
import rallyedto.farewelledto.Strategicedto.inflate
import rallyedto.policeedto.huntedto.Cookedto
import rallyedto.policeedto.huntedto.Outletedto

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

    private val beanList = arrayListOf<Cookedto>()

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

    }

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

        if (payloads.isEmpty()) {

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

            binding.auntedto.isSelected = bean.isSelect

            val subAdapter = Bootedto().apply {
                subClickAction = { clickAction.invoke(this@Inspirationedto.getSelectData()) }
            }
            binding.formedto.adapter = subAdapter
            subAdapter.setData(bean.beans)

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

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

    }

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

        clickAction.invoke(getSelectData())
    }

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

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

}