package com.zxhy.simplefilesmanager.simpledup

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhy.simplefilesmanager.databinding.ItemJoDupJoBinding
import com.zxhy.simplefilesmanager.simpledata.JoGridFileJo
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import android.view.ViewGroup
import com.bumptech.glide.request.RequestOptions
import com.zxhy.simplefilesmanager.R
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.zxhy.simplefilesmanager.simpledata.JoDupFileJo
import com.zxhy.simplefilesmanager.simpleadpter.JoCommonJoAdapter
import com.zxhy.simplefilesmanager.simplejoke.DipPxEx
import com.zxhy.simplefilesmanager.simplejoke.JoXmljoEx.jojoinflate
import com.zxhy.simplefilesmanager.databinding.ItemJoIJoSelectBinding


class SimpleDupAdapter(
    private val selectAction: (allSelect: Boolean, count: Int) -> Unit
) : RecyclerView.Adapter<SimpleDupAdapter.DDD>() {

    private var dupList = arrayListOf<JoDupFileJo>()

    class DDD(view: View) : ViewHolder(view) {
        val binding = ItemJoDupJoBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DDD {
        return DDD(R.layout.item_jo_dup_jo.jojoinflate(parent))
    }

    override fun getItemCount(): Int = dupList.size
    override fun onBindViewHolder(holder: DDD, position: Int) = Unit

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: DDD, position: Int, payloads: MutableList<Any>) {
        super.onBindViewHolder(holder, position, payloads)

        val data = dupList[position]
        val context = holder.itemView.context
        if (payloads.isEmpty()) {

            holder.binding.ivSelector.isSelected = data.isSelected
            holder.binding.tvItems.text = context.getString(R.string.d_items, data.dups.size)

            val adapter = SSS(context) { flag ->
                data.isSelected = flag
                holder.binding.ivSelector.isSelected = data.isSelected
                notifyItemChanged(position, "sadsf")

                val allFlag = dupList.all { it.isSelected }
                val count = getSubList().size
                selectAction.invoke(allFlag, count)
            }
            holder.binding.rv.adapter = adapter
            adapter.setData(data.dups)

            holder.binding.ivSelector.setOnClickListener {
                it.isSelected = !it.isSelected
                data.isSelected = it.isSelected
                notifyItemChanged(position, "sfsaf")
                adapter.toggleSelect(it.isSelected)

                val count = getSubList().size
                selectAction.invoke(count != 0, count)
            }
        } else {
            holder.binding.ivSelector.isSelected = data.isSelected
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun toggleSelect(select: Boolean) {
        dupList.forEach { data ->
            data.dups.forEach { it.isSelected = select }
        }
        notifyDataSetChanged()
    }


    fun getSubList(): ArrayList<JoGridFileJo> {
        val select = arrayListOf<JoGridFileJo>()
        dupList.forEach { data ->
            select.addAll(data.dups.filter { it.isSelected })
        }
        return select
    }

    fun removeData(deletes: ArrayList<JoGridFileJo>) {
        dupList.forEach { data ->
            deletes.forEach { delete ->
                data.dups.remove(delete)
            }
        }
        val new = dupList.filter { it.dups.size > 1 }
        setData(new)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(data: List<JoDupFileJo>) {
        dupList.clear()
        dupList.addAll(data)
        notifyDataSetChanged()
    }


    private class SSS(
        context: Context,
        val allSelectAction: (allSelect: Boolean) -> Unit
    ) : JoCommonJoAdapter<SSS.FFF, JoGridFileJo>() {

        private val options =
            RequestOptions()
                .transform(CenterCrop(), RoundedCorners(DipPxEx.dp2px(context, R.dimen.dp_8)))

        inner class FFF(view: View) : ViewHolder(view) {
            val binding = ItemJoIJoSelectBinding.bind(view)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FFF {
            return FFF(R.layout.item_jo_i_jo_select.jojoinflate(parent))
        }

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

        override fun onBindViewHolder(holder: FFF, position: Int, payloads: MutableList<Any>) {
            val context = holder.binding.root.context
            val data = joFileList[position]
            if (payloads.isEmpty()) {
                Glide.with(context)
                    .load(data.path)
                    .apply(options)
                    .into(holder.binding.iv)
                holder.binding.ivSelector.isSelected = data.isSelected
                holder.binding.ivSelector.setOnClickListener {
                    data.isSelected = !data.isSelected
                    holder.binding.ivSelector.isSelected = data.isSelected
                    notifyItemChanged(position, "sumdk")
                    allSelectAction.invoke(joFileList.all { subData -> subData.isSelected })
                }
            } else {
                holder.binding.ivSelector.isSelected = data.isSelected
                super.onBindViewHolder(holder, position, payloads)
            }
        }

    }


}