package com.base.superfilemanager.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.superfilemanager.R
import com.base.superfilemanager.bean.FileBean
import com.base.superfilemanager.databinding.ItemFileListBinding
import com.base.superfilemanager.view.XmlEx.inflate
import com.bumptech.glide.Glide

class FileBrowseAdapter(private val select: (size: Int) -> Unit) : RecyclerView.Adapter<FileBrowseAdapter.FB>() {

    private val fileList = arrayListOf<FileBean>()

    inner class FB(view: View) : ViewHolder(view)

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

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

    }

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


    override fun onBindViewHolder(holder: FB, position: Int, payloads: MutableList<Any>) {
        val binding = ItemFileListBinding.bind(holder.itemView)
        val context = holder.itemView.context
        val data = fileList[position]

        if (payloads.isEmpty()) {
            data.loadImageIcon(context, binding.ivIcon)
            binding.tvName.text = data.name
            binding.tvPath.text = data.path
            binding.ivSelector.isSelected = data.isSelect
            binding.root.setOnClickListener {
                data.isSelect = !data.isSelect
                binding.ivSelector.isSelected = data.isSelect
                notifyItemChanged(position, "局部刷洗")
                select.invoke(fileList.filter { it.isSelect }.size)
            }
        } else {
            binding.ivSelector.isSelected = data.isSelect
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(data: List<FileBean>, clear: Boolean = false) {
        if (clear) {
            fileList.clear()
        }
        fileList.addAll(data)
        notifyDataSetChanged()
    }


    fun FileBean.loadImageIcon(context: Context, ivIcon: ImageView) {
        when {
            isImage() or isVideo() -> Glide.with(context).load(path).into(ivIcon)
            isAudio() -> ivIcon.setImageResource(R.drawable.audio)
            isZip() -> ivIcon.setImageResource(R.drawable.zip)
            isApk() -> ivIcon.setImageResource(R.drawable.apk)
            isDocx() -> ivIcon.setImageResource(R.drawable.doc)
            isPdf() -> ivIcon.setImageResource(R.drawable.pdf)
            isXls() -> ivIcon.setImageResource(R.drawable.xls)
            isTxt() -> ivIcon.setImageResource(R.drawable.txt)
            isPpt() -> ivIcon.setImageResource(R.drawable.ppt)
        }

    }

    fun getSelect(): List<FileBean> {
        return fileList.filter { it.isSelect }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(deleteList: List<FileBean>) {
        fileList.removeAll(deleteList.toSet())
        notifyDataSetChanged()
    }
}


