package com.base.easyfilemanager.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.easyfilemanager.R
import com.base.easyfilemanager.bean.FileBean
import com.base.easyfilemanager.databinding.ItemFileListBinding
import com.base.easyfilemanager.view.XmlEx.inflate
import com.bumptech.glide.Glide

class FileBrowseAdapter(
    private val select: (size: Int) -> Unit,
    private val empty: () -> 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()
        empty.invoke()
    }


    private fun FileBean.loadImageIcon(context: Context, ivIcon: ImageView) {
        when {
            isImage() or isVideo() -> Glide.with(context).load(path).centerCrop().into(ivIcon)
            isAudio() -> ivIcon.setImageResource(R.mipmap.audio)
            isZip() -> ivIcon.setImageResource(R.mipmap.zip)
            isApk() -> ivIcon.setImageResource(R.mipmap.apk)
            isEmptyFile() -> ivIcon.setImageResource(R.mipmap.empty)
        }

    }

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

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

    fun isEmpty(): Boolean {
        return fileList.isEmpty()
    }
}


