package com.zxhy.solarmasterace.filebrowser

import android.annotation.SuppressLint
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.zxhy.solarmasterace.R
import com.zxhy.solarmasterace.adapter.CommonAdapter
import com.zxhy.solarmasterace.adapter.ItemCardDupImageViewHolder
import com.zxhy.solarmasterace.adapter.ItemCardImageSelect85ViewHolder
import com.zxhy.solarmasterace.data.DupFileBean
import com.zxhy.solarmasterace.quicktools.inflate
import java.io.File

class DupImageAdapter(
    private val selectClick: (flag: Boolean) -> Unit,
    private val itemClick: ((path: String) -> Unit)? = null
) : RecyclerView.Adapter<ItemCardDupImageViewHolder>() {

    private val dupFileList = arrayListOf<DupFileBean>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemCardDupImageViewHolder {
        return ItemCardDupImageViewHolder(R.layout.item_card_dup_image.inflate(parent))
    }

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

    override fun onBindViewHolder(holder: ItemCardDupImageViewHolder, position: Int) {
//        val data = dupFileList[position]
//        holder.binding.apply {
//            val adapter = ImageAdapter()
//            rv.adapter = adapter
//            adapter.setData(data.dupData)
//            iv.isSelected = data.isSelect
//            iv.setOnClickListener {
//                data.isSelect = !data.isSelect
//                notifyItemChanged(position, "单条刷新")
//            }
//        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: ItemCardDupImageViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        val data = dupFileList[position]
        if (payloads.isEmpty()) {
            holder.binding.apply {
                tvTip.text = "${data.dupData.size} items"
                val adapter = ImageAdapter {
                    ivSelector.isSelected = it
                    data.isSelect = it
                    val flag = dupFileList.all { dupFileBean -> dupFileBean.isSelect }
                    selectClick.invoke(flag)
                }
                rv.adapter = adapter
                adapter.setData(data.dupData)
                ivSelector.isSelected = data.isSelect
                ivSelector.setOnClickListener {
                    data.isSelect = !it.isSelected
                    notifyItemChanged(position, "单条刷新")
                    val flag = dupFileList.all { dupFileBean -> dupFileBean.isSelect }
                    selectClick.invoke(flag)
                    adapter.toggleSelect(data.isSelect)
                }
            }
        } else {
            holder.binding.apply {
                ivSelector.isSelected = data.isSelect
            }
            super.onBindViewHolder(holder, position, payloads)
        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    fun toggleSelect(select: Boolean) {
        dupFileList.map {
            it.isSelect = select
            it.dupData.map { fileBean -> fileBean.isSelect = select }
        }
        notifyDataSetChanged()
    }

    fun getSelectFile(): ArrayList<File> {
        val fileList = arrayListOf<File>()
        dupFileList.forEach {
            it.dupData.forEach { fileBean ->
                if (fileBean.isSelect) {
                    fileList.add(File(fileBean.path))
                }
            }
        }
        return fileList
    }


    private inner class ImageAdapter(
        val selectClick: (flag: Boolean) -> Unit
    ) : CommonAdapter<ItemCardImageSelect85ViewHolder>() {
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): ItemCardImageSelect85ViewHolder {
            return ItemCardImageSelect85ViewHolder(
                R.layout.item_card_image_selector_85.inflate(parent)
            )
        }


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

        override fun onBindViewHolder(
            holder: ItemCardImageSelect85ViewHolder,
            position: Int,
            payloads: MutableList<Any>
        ) {
            val data = fileList[position]
            if (payloads.isEmpty()) {
                holder.binding.apply {
                    iv.load(data.path)
                    iv.setOnClickListener {
                        itemClick?.invoke(data.path)
                    }
                    ivSelector.isSelected = data.isSelect
                    ivSelector.setOnClickListener {
                        data.isSelect = !data.isSelect
                        notifyItemChanged(position, "单条刷新")
                        val flag = fileList.all { it.isSelect }
                        selectClick.invoke(flag)
                    }
                }
            } else {
                holder.binding.apply {
                    ivSelector.isSelected = data.isSelect
                }
            }
            super.onBindViewHolder(holder, position, payloads)
        }
    }


}