package com.zxhy.hfilemanagermaster.video

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.ItemVideoSelectBinding
import com.zxhy.hfilemanagermaster.data.MediaDataC
import com.zxhy.hfilemanagermaster.data.file2MediaDataC
import com.zxhy.hfilemanagermaster.glide.loadIntoImageView
import com.zxhy.hfilemanagermaster.knife.inflate
import java.io.File

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

    private val mediaList = arrayListOf<MediaDataC>()

    inner class VideoSelectorViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val binding = ItemVideoSelectBinding.bind(view)
    }

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

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

    override fun onBindViewHolder(holder: VideoSelectorViewHolder, 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
        }
    }

    override fun onBindViewHolder(
        holder: VideoSelectorViewHolder,
        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
    }

    fun getSelectSize(): Int {
        return mediaList.count { it.select }
    }

    fun getSelectFile(): List<MediaDataC> {
        return mediaList.filter { it.select }
    }

    fun notifyCurrentDir(context: Context) {
        val dir = File(mediaList[0].path).parentFile
        val data = dir?.listFiles()?.map { it.file2MediaDataC(context) } ?: listOf()
        setData(data)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(file: List<MediaDataC>) {
        mediaList.removeAll(file.toSet())
//        file.forEachIndexed { index, mediaDataC ->
//       Log.e("removeData", "index=$index $mediaDataC")
//        }
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addData(data: List<MediaDataC>) {
        val index = mediaList.size
        mediaList.addAll(data)
        notifyItemRangeChanged(index, data.size)
//        Log.e("FileSelectorAdapter", mediaList.size.toString())
    }
}