package com.zxhy.solarmasterace.filebrowser

import android.view.ViewGroup
import com.zxhy.solarmasterace.R
import com.zxhy.solarmasterace.adapter.CommonAdapter
import com.zxhy.solarmasterace.adapter.ItemCardFileViewHolder
import com.zxhy.solarmasterace.quicktools.inflate

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

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

    override fun onBindViewHolder(
        holder: ItemCardFileViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        val data = fileList[position]
        val context = holder.binding.root.context
        if (payloads.isEmpty()) {
            holder.binding.apply {
                data.bindIconImage(ivIcon)
                tvName.text = data.name
                tvInfo.text = data.sizeFormat(context)
                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)
        }
    }

}