package com.base.datarecovery.adapter

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.bean.MediaBean
import com.base.datarecovery.bean.MediaTimeBean
import com.base.datarecovery.databinding.ItemTimeMedia2Binding
import com.base.datarecovery.view.XmlEx.inflate
import com.bumptech.glide.Glide

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

    private val beanList = arrayListOf<MediaTimeBean>()

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

    }

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

        if (payloads.isEmpty()) {

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

            binding.ivSelector.isSelected = bean.isSelect

            val subAdapter = MediaSubAdapter().apply {
                subClickAction = { clickAction.invoke(this@MediaAdapter.getSelectData()) }
            }
            binding.rvMediaColumns.adapter = subAdapter
            subAdapter.setData(bean.beans)

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

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

    }

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

        clickAction.invoke(getSelectData())
    }

    private fun getSelectData(): Pair<Boolean, List<MediaBean>> {
        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<MediaTimeBean>) {
        beanList.clear()
        beanList.addAll(list)
        notifyDataSetChanged()
    }

}