package com.zxhy.hfilemanagermaster.duplicate

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.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.ItemDupImageSelectBinding
import com.example.hfilemanagermaster.databinding.ItemImageSelect2Binding
import com.zxhy.hfilemanagermaster.data.DupImageData
import com.zxhy.hfilemanagermaster.data.MediaDataC
import com.zxhy.hfilemanagermaster.glide.loadIntoImageView
import com.zxhy.hfilemanagermaster.knife.inflate
import java.io.File

class DupImageAdapter(
    private val selectAction: ((flag: Boolean) -> Unit)? = null
) : RecyclerView.Adapter<DupImageAdapter.DupImageVideHolder>() {

    private val dupImageList = arrayListOf<DupImageData>()

    class DupImageVideHolder(view: View) : RecyclerView.ViewHolder(view) {
        val binding = ItemDupImageSelectBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DupImageVideHolder {
        val root = R.layout.item_dup_image_select.inflate(parent)
        return DupImageVideHolder(root)
    }

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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: DupImageVideHolder, position: Int) {
//        val context = holder.binding.root.context
        val data = dupImageList[position]
        holder.binding.apply {

            ivSelector.isSelected = data.select
            tvNum.text = "${data.dupList.size} items"
            val itemAdapter = DupImageItemAdapter {
                ivSelector.isSelected = it
                //当条被选
                data.select = it
                //影响外部全选
                selectAction?.invoke(allSelectCheck())
            }
            rvDup.adapter = itemAdapter
            itemAdapter.setData(data.dupList)

            ivSelector.setOnClickListener {
                //影响子列表
                ivSelector.isSelected = itemAdapter.setToggleSelect()
                //影响外部全选
                selectAction?.invoke(ivSelector.isSelected)
                //当调条数据被改
                data.select = !data.select
                notifyItemChanged(position, "单条刷新")
            }

            if (data == dupImageList.last()) {
                selectAction?.invoke(dupImageList.all { it.select })
            }
        }
    }

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

//        val context = holder.binding.root.context
        val data = dupImageList[position]
        //判断是做局部刷新还是单条刷新
        if (payloads.isEmpty()) {//局部刷新
            holder.binding.apply {

                ivSelector.isSelected = data.select
                tvNum.text = "${data.dupList.size} items"
                val itemAdapter = DupImageItemAdapter {
                    ivSelector.isSelected = it
                    //当条被选
                    data.select = it
                    //影响外部全选
                    selectAction?.invoke(allSelectCheck())
                }
                rvDup.adapter = itemAdapter
                itemAdapter.setData(data.dupList)

                ivSelector.setOnClickListener {
                    //影响子列表
                    ivSelector.isSelected = itemAdapter.setToggleSelect()
                    //影响外部全选
                    selectAction?.invoke(ivSelector.isSelected)
                    //当条数据被改
                    data.select = !data.select
                    notifyItemChanged(position, "单条刷新")
                }
                if (data == dupImageList.last()) {
                    selectAction?.invoke(dupImageList.all { it.select })
                }
            }
        } else {
            holder.binding.apply {
                ivSelector.isSelected = data.select
            }
            super.onBindViewHolder(holder, position, payloads)
            selectAction?.invoke(dupImageList.all { it.select })
        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    fun setToggleSelect(): Boolean {
        val flag = dupImageList.all { it.select }
        dupImageList.forEach {
            it.select = !flag
            it.dupList.forEach { mediaDataC -> mediaDataC.select = !flag }
        }
        notifyDataSetChanged()
        return !flag
    }

    private fun allSelectCheck(): Boolean {
        return dupImageList.all { it.select }
    }

    fun getSelectNum(): Int {
        var i = 0
        dupImageList.forEach {
            it.dupList.forEach { mediaDataC ->
                if (mediaDataC.select) i++
            }
        }
        return i
    }

    fun getSelectData(): ArrayList<MediaDataC> {
        val list = arrayListOf<MediaDataC>()
        dupImageList.forEach {
            it.dupList.forEach { mediaDataC ->
                if (mediaDataC.select) list.add(mediaDataC)
            }
        }
        return list
    }
}

private class DupImageItemAdapter(
    private val selectAction: ((flag: Boolean) -> Unit)? = null
) : RecyclerView.Adapter<DupImageItemAdapter.DupImageItemViewHolder>() {

    private val mediaList = arrayListOf<MediaDataC>()

    class DupImageItemViewHolder(view: View) : ViewHolder(view) {
        val binding = ItemImageSelect2Binding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DupImageItemViewHolder {
        val root = R.layout.item_image_select_2.inflate(parent)
        return DupImageItemViewHolder(root)
    }

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

    override fun onBindViewHolder(holder: DupImageItemViewHolder, position: Int) {
        val context = holder.binding.root.context
        val data = mediaList[position]
        holder.binding.apply {
            loadIntoImageView(context, File(data.path), ivImage)
            ivSelector.isSelected = data.select
            ivSelector.setOnClickListener {
                data.select = !data.select
                notifyItemChanged(position, "单条刷新")
            }
            if (data == mediaList.last()) {
                selectAction?.invoke(mediaList.all { it.select })
            }
        }
    }

    override fun onBindViewHolder(
        holder: DupImageItemViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        val context = holder.binding.root.context
        val data = mediaList[position]
        //判断是做局部刷新还是单条刷新
        if (payloads.isEmpty()) {//局部刷新
            holder.binding.apply {
                loadIntoImageView(context, File(data.path), ivImage)
                ivSelector.isSelected = data.select
                ivSelector.setOnClickListener {
                    data.select = !data.select
                    notifyItemChanged(position, "单条刷新")
                }
                if (data == mediaList.last()) {
                    selectAction?.invoke(mediaList.all { it.select })
                }
            }
        } else { // 单条刷新
            holder.binding.apply {
                ivSelector.isSelected = data.select
            }
            super.onBindViewHolder(holder, position, payloads)
            selectAction?.invoke(mediaList.all { it.select })
        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    fun setToggleSelect(): Boolean {
        val flag = mediaList.all { it.select }
        mediaList.forEach { it.select = !flag }
        notifyDataSetChanged()
        return !flag
    }

}