package com.zxhy.fastfilemanagerpro.gridfile

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhy.fastfilemanagerpro.R
import com.zxhy.fastfilemanagerpro.databinding.ItemKokoGridFileBinding
import com.zxhy.fastfilemanagerpro.kokoadapter.CommonAdapter
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.isImage
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.isVideo
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.toFile
import com.zxhy.fastfilemanagerpro.kokotools.GlideHelper.loadImageView
import com.zxhy.fastfilemanagerpro.kokotools.XmlEx.inflate

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

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

    override fun onBindViewHolder(holder: KoFileGridkoViewHolder, position: Int) {

    }

    override fun onBindViewHolder(
        holder: KoFileGridkoViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        val context = holder.binding.root.context
        val data = fileList[position]
        if (payloads.isEmpty()) {
            holder.binding.apply {
                ivIcon.loadImageView(context, data.toFile())
                if (data.isImage()) {
                    ivCamera.visibility = View.GONE
                }
                if (data.isVideo()) {
                    ivCamera.visibility = View.VISIBLE
                }
                ivSelector.isSelected = data.isSelect
                ivSelector.setOnClickListener {
                    data.isSelect = !data.isSelect
                    notifyItemChanged(position, "局部刷新")
                    allSelect.invoke(fileList.all { it.isSelect })
                }
            }
        } else {
            holder.binding.apply {
                ivSelector.isSelected = data.isSelect
            }
            super.onBindViewHolder(holder, position, payloads)
        }
    }
}