package com.zxhy.fastfilemanagerpro.kokodup

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.zxhy.fastfilemanagerpro.R
import com.zxhy.fastfilemanagerpro.databinding.ItemKokoDupFileBinding
import com.zxhy.fastfilemanagerpro.databinding.ItemKokoDupImageBinding
import com.zxhy.fastfilemanagerpro.kokoadapter.AdapterCommonDataFunction
import com.zxhy.fastfilemanagerpro.kokoadapter.CommonAdapter
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.loadImageIcon
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.toFile
import com.zxhy.fastfilemanagerpro.kokotools.GlideHelper.loadImageView
import com.zxhy.fastfilemanagerpro.kokotools.XmlEx.inflate

@SuppressLint("NotifyDataSetChanged")
class KoDupFilekoAdapter(
    private val allSelect: (flag: Boolean) -> Unit
) : RecyclerView.Adapter<KoDupFilekoAdapter.KoDupFilekoViewHolder>(),
    AdapterCommonDataFunction<KoDupFileDatako> {

    private var dupFileList = arrayListOf<KoDupFileDatako>()

    inner class KoDupFilekoViewHolder(view: View) : ViewHolder(view) {
        val binding = ItemKokoDupFileBinding.bind(view)
    }

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

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

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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: KoDupFilekoViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        val context = holder.binding.root.context
        val data = dupFileList[position]
        if (payloads.isEmpty()) {
            holder.binding.apply {
                val adapter = DupImageAdapter {
                    data.isSelect = it
                    notifyItemChanged(position, "局部刷洗")
                    allSelect.invoke(dupFileList.all { it.isSelect })
                }
                rv.adapter = adapter
                adapter.setData(data.dups)
                ivSelector.isSelected = data.isSelect
                ivSelector.setOnClickListener {
                    data.isSelect = !data.isSelect
                    ivSelector.isSelected = data.isSelect
                    notifyItemChanged(position, "局部刷洗")
                    adapter.toggleSelect(data.isSelect)
                    allSelect.invoke(dupFileList.all { it.isSelect })
                }
                tvItems.text = "${data.dups.size} items"
            }
        } else {
            holder.binding.apply {
                ivSelector.isSelected = data.isSelect
            }
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    override fun setData(data: List<KoDupFileDatako>) {
        dupFileList.clear()
        dupFileList.addAll(data)
        notifyDataSetChanged()
    }

    override fun addData(data: List<KoDupFileDatako>) {
        dupFileList.addAll(data)
        notifyDataSetChanged()
    }

    override fun clearData() {
        if (dupFileList.size == 0) return
        dupFileList.clear()
        notifyDataSetChanged()
    }

    override fun getSelectData(): List<KoDupFileDatako> {
        return dupFileList.filter { it.isSelect }
    }

    fun getSelectDupData(): List<KoDupFileDatako> {
        return dupFileList.filter { it.isSelect }
    }

    fun getSelectSubData(): List<KoFileDatako> {
        return dupFileList.flatMap { it.dups }.filter { it.isSelect }
    }

    override fun toggleSelect(select: Boolean) {
        dupFileList.forEach { fileBean ->
            fileBean.isSelect = select
            fileBean.dups.forEach { koFileDatako ->
                koFileDatako.isSelect = select
            }
        }
        notifyDataSetChanged()
    }

    override fun removeData(data: List<KoDupFileDatako>) {
        data.forEach {
            val index = dupFileList.indexOf(it)
            if (index != -1) {
                dupFileList.removeAt(index)
                notifyItemRemoved(index)
            }
        }
    }

    fun removeSubData(data: List<KoFileDatako>) {
        dupFileList.forEach { koDupFileDatako ->
            data.forEach { koFileDatako ->
                koDupFileDatako.dups.remove(koFileDatako)
            }
        }
        val removedList = dupFileList.filter { it.dups.size > 1 }
        setData(removedList)
    }

    inner class DupImageAdapter(
        private val allSelect: (flag: Boolean) -> Unit
    ) : CommonAdapter<DupImageAdapter.DupImageViewHolder>() {
        inner class DupImageViewHolder(view: View) : ViewHolder(view) {
            val binding = ItemKokoDupImageBinding.bind(view)
        }

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

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

        @SuppressLint("SetTextI18n")
        override fun onBindViewHolder(
            holder: DupImageViewHolder,
            position: Int,
            payloads: MutableList<Any>
        ) {
            val content = holder.binding.root.context
            val data = fileList[position]
            if (payloads.isEmpty()) {
                holder.binding.apply {
                    data.loadImageIcon(content, iv)
                    tvName.text = data.name
                    tvInfo.text = "${data.timeE}  ${data.sizeF}"
                    ivSelector.isSelected = data.isSelect
                    ivSelector.setOnClickListener {
                        data.isSelect = !data.isSelect
                        ivSelector.isSelected = data.isSelect
                        notifyItemChanged(position, "局部刷洗")
                        allSelect.invoke(fileList.all { it.isSelect })
                    }
                }
            } else {
                holder.binding.apply {
                    ivSelector.isSelected = data.isSelect
                }
                super.onBindViewHolder(holder, position, payloads)
            }
        }
    }


}