package com.zxhy.simplefilesmanager.internalstorage

import android.annotation.SuppressLint
import android.os.Environment
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zxhy.simplefilesmanager.R
import com.zxhy.simplefilesmanager.databinding.ItemJoListFileJoBinding
import com.zxhy.simplefilesmanager.simpleadpter.JoCommonJoAdapter
import com.zxhy.simplefilesmanager.simpledata.JoListFileJo
import com.zxhy.simplefilesmanager.simpledata.JoListFileJo.Companion.joListFileJo
import com.zxhy.simplefilesmanager.simpledata.JoListFileJo.Companion.sizeF
import com.zxhy.simplefilesmanager.simplejoke.JoXmljoEx.jojoinflate
import java.io.File

@SuppressLint("NotifyDataSetChanged")
class JoInternalJoStorageAdapter(
    private val allSelect: (select: Boolean) -> Unit,
    private val showEmpty: ((isShow: Boolean) -> Unit)? = null
) : JoCommonJoAdapter<JoInternalJoStorageAdapter.GGGG, JoListFileJo>() {

    private var showSelect: Boolean = false
    private var currentDir = Environment.getExternalStorageDirectory()
    private val cannotNextFiles = arrayListOf<File>()
    private var canNext: Boolean = true

    class GGGG(view: View) : ViewHolder(view) {
        val binding = ItemJoListFileJoBinding.bind(view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GGGG {
        return GGGG(R.layout.item_jo_list_file_jo.jojoinflate(parent))
    }

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

    }

    override fun onBindViewHolder(holder: GGGG, position: Int, payloads: MutableList<Any>) {
        val data = joFileList[position]
        val context = holder.binding.root.context

        if (payloads.isEmpty()) {
            bindIcon(data, holder.binding.iv)
            holder.binding.tvName.text = data.fName
            val file = data.toFile()
            if (file.isFile) {
                holder.binding.tvInfo.text = data.timeE + "  ${file.sizeF(context)}"
            } else {
                val count = file.list()?.size ?: 0
                holder.binding.tvInfo.text = data.timeE + "  ${count} items"
            }

            //================================是否显示选中按钮逻辑==================================
            if (showSelect) {
                val drawable = ContextCompat.getDrawable(context, R.drawable.bg_list_selector)
                holder.binding.ivSelector.isSelected = data.isSelected
                holder.binding.ivSelector.isVisible = true
                holder.binding.ivSelector.setImageDrawable(drawable)
                holder.binding.ivSelector.setOnClickListener {
                    data.isSelected = !data.isSelected
                    holder.binding.ivSelector.isSelected = data.isSelected
                    notifyItemChanged(position, "sdada")
                    allSelect.invoke(joFileList.all { it.isSelected })
                }
            } else {
                val drawable = ContextCompat.getDrawable(context, R.mipmap.dsf_597)
                holder.binding.ivSelector.setImageDrawable(drawable)
                holder.binding.ivSelector.isVisible = file.isDirectory
                holder.binding.ivSelector.setOnClickListener { }
            }
            //============================选中目录无法再进入子目录逻辑==============================
            holder.binding.root.setOnClickListener {
                if (!cannotNextFiles.contains(file) && file.isDirectory && canNext) nextPage(file)
            }
            if (cannotNextFiles.contains(file) && file.isDirectory) {
                val color = ContextCompat.getColor(context, R.color.color_6e6e6e)
                holder.binding.llItem.setBackgroundColor(color)
            } else {
                val color = ContextCompat.getColor(context, R.color.white)
                holder.binding.llItem.setBackgroundColor(color)
            }

        } else {
            holder.binding.root.setOnClickListener {
                holder.binding.ivSelector.isSelected = data.isSelected
            }
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    private fun nextPage(file: File) {
        currentDir = file
        val files = currentDir.listFiles()
        val data = files?.map { joListFileJo(it) } ?: listOf()
        setData(data, isEmpty = { showEmpty?.invoke(it) })
    }

    fun getCurrentDir(): File {
        return currentDir
    }

    fun notifyCurrentDir() {
        val files = currentDir.listFiles()
//        files?.sortByDescending { it.lastModified() }
        val data = files?.map { joListFileJo(it) } ?: listOf()
        setData(data, isEmpty = { showEmpty?.invoke(it) })
    }

    fun changeSelect(
        isSelect: Boolean,
        isAddCannotNext: Boolean = false,
        isRemoveCannotNext: Boolean = false,
        canNext: Boolean = true,
    ) {
        showSelect = isSelect
        if (isAddCannotNext) {
            cannotNextFiles.addAll(getSelectData().map { it.toFile() })
        }
        if (isRemoveCannotNext) {
            cannotNextFiles.clear()
        }
        this.canNext = canNext
        notifyDataSetChanged()
    }


    /**
     *@param 是否是根目录
     */
    fun beforePage(): Boolean {
        return if (currentDir.absoluteFile == Environment.getExternalStorageDirectory().absoluteFile) {
            true
        } else {
            currentDir = currentDir.parentFile
            val files = currentDir.listFiles()
            val data = files?.map { joListFileJo(it) } ?: listOf()
            setData(data, isEmpty = { showEmpty?.invoke(it) })
            false
        }
    }

    fun getOperationList(): ArrayList<File> {
        return cannotNextFiles
    }
}