package com.test.basd.supercleanermax.adapter

import android.annotation.SuppressLint
import android.text.format.Formatter
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.bean.FileBean
import com.test.basd.supercleanermax.databinding.ItemLargeFileBinding
import com.test.basd.supercleanermax.helps.LogEx
import com.test.basd.supercleanermax.view.XmlEx.inflate

class LargeFileAdapter(
    val itemClick: (data: FileBean) -> Unit,
    val selectClick: (selectList: List<FileBean>) -> Unit
) : Adapter<LargeFileAdapter.UUU>() {
    private val TAG = "BigFileAdapter"

    private val files = arrayListOf<FileBean>()

    class UUU(view: View) : ViewHolder(view) {

    }

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

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

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

    }

    override fun onBindViewHolder(holder: UUU, position: Int, payloads: MutableList<Any>) {

        val binding = ItemLargeFileBinding.bind(holder.itemView)
        val data = files[position]
        val content = holder.itemView.context


        if (payloads.isEmpty()) {
            binding.ivPlay.isVisible = data.isVideo()
            when {
                data.isImage() -> {
                    Glide.with(content).load(data.path).into(binding.iv)
                }

                data.isVideo() -> {
                    Glide.with(content).load(data.path).into(binding.iv)
                }

                data.isAudio() -> {
                    binding.iv.setImageResource(R.mipmap.icon_tingyinyue)
                }

                data.isDoc() -> {
                    binding.iv.setImageResource(R.mipmap.icon_wendang)
                }

                data.isApk() -> {
                    binding.iv.setImageResource(R.mipmap.icon_apk)
                }

                data.isZip() -> {
                    binding.iv.setImageResource(R.mipmap.icon_archives)
                }


                else -> {
                    binding.iv.setImageResource(R.mipmap.icon_other_file)
                }
            }
            binding.tvName.text = data.name
            binding.tvSize.text = Formatter.formatFileSize(content, data.size)
            binding.root.setOnClickListener {
                itemClick.invoke(data)
            }
            binding.ivSelector.isSelected = data.isSelect
            binding.flSelect.setOnClickListener {
                data.isSelect = !data.isSelect
                binding.ivSelector.isSelected = data.isSelect
                notifyItemChanged(position, "dasds")
                val selectList = files.filter { it.isSelect }
                selectClick.invoke(selectList)
            }
        } else {
            binding.ivSelector.isSelected = data.isSelect
            super.onBindViewHolder(holder, position, payloads)
        }

    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(bigFileList: List<FileBean>) {
        bigFileList.forEach {
            LogEx.logDebug(TAG, "$it")
        }
        files.clear()
        files.addAll(bigFileList)
        files.sortByDescending { it.size }
        notifyDataSetChanged()
    }

    fun getSelectData(): List<FileBean> {
        return files.filter { it.isSelect }
    }

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