package com.zxhy.solarmasterace.internalstorage

import android.annotation.SuppressLint
import android.content.Context
import android.os.Environment
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.zxhy.solarmasterace.R
import com.zxhy.solarmasterace.adapter.CommonAdapter
import com.zxhy.solarmasterace.adapter.ItemCardFileViewHolder
import com.zxhy.solarmasterace.data.FileBean
import com.zxhy.solarmasterace.data.toFileBean
import com.zxhy.solarmasterace.quicktools.inflate
import java.io.File

class FileDirectoryAdapter(
    private val selectClick: (flag: Boolean) -> Unit,
    private val showEmpty: (flag: Boolean) -> Unit,
) : CommonAdapter<ItemCardFileViewHolder>() {

    private var currentDir = Environment.getExternalStorageDirectory()
    private var isSelectMode = false
    private var pageTurn = true
    private var operationFile = arrayListOf<FileBean>()

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

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

    fun setPageTurn(flag: Boolean) {
        pageTurn = flag
    }

    @SuppressLint("SetTextI18n")
    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
                if (data.isDir()) {
                    tvInfo.text = data.time + "  ${data.file().listFiles()?.size ?: 0} items"
                } else {
                    tvInfo.text = data.time + "  ${data.sizeFormat(context)}"
                }
                if (isSelectMode) {
                    ivSelector.visibility = View.VISIBLE
                } else {
                    ivSelector.visibility = View.INVISIBLE
                }
                ivSelector.isSelected = data.isSelect

                ivSelector.setOnClickListener {
                    data.isSelect = !data.isSelect
                    notifyItemChanged(position, "单条刷新")
                    val flag = fileList.all { it.isSelect }
                    selectClick.invoke(flag)
                }
                root.setOnClickListener {
                    if (!pageTurn) return@setOnClickListener
                    nextPage(data)
                }

                //该列表是选中列表
                if (operationFile.map { it.path }.contains(data.path)) {
                    root.setOnClickListener { }
                    root.setCardBackgroundColor(
                        ContextCompat.getColor(
                            context, R.color.color_F4F4F4
                        )
                    )
                } else {
                    root.setCardBackgroundColor(ContextCompat.getColor(context, R.color.white))
                }
            }
        } else {
            holder.binding.apply {
                ivSelector.isSelected = data.isSelect
            }
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    private fun nextPage(data: FileBean) {
        if (data.isDir()) {
            currentDir = data.file()
            val list = data.file().listFiles()?.toList()?.map { it.toFileBean() } ?: listOf()
            setData(list)
            if (list.isEmpty()) showEmpty(true) else showEmpty(false)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun showSelect(flag: Boolean) {
        isSelectMode = flag
        notifyDataSetChanged()
    }

    /**
     * @return 是否是根目录
     */
    fun beforePage(): Boolean {
        return if (currentDir == Environment.getExternalStorageDirectory().absoluteFile) {
            true
        } else {
            val parentDir = currentDir.parentFile ?: File("")
            val files = parentDir.listFiles()?.toList()?.map { it.toFileBean() } ?: listOf()
            setData(files)
            currentDir = parentDir
            if (files.isEmpty()) showEmpty(true) else showEmpty(false)
            false
        }
    }

    fun notifyCurrentDir() {
        val data = currentDir.listFiles()?.map { it.toFileBean() } ?: listOf()
        setData(data)
        if (data.isEmpty()) showEmpty(true) else showEmpty(false)
    }

    fun getCurrentDir(): File? {
        return currentDir
    }

    //当选中选中的列表,无法复制 移动到自身
    fun notifyCannotSelect(data: List<FileBean>) {
        val selectList = arrayListOf<FileBean>()
        selectList.addAll(data)
        operationFile = selectList
        notifyDataSetChanged()
    }

    fun clearCannotSelect() {
        operationFile = arrayListOf()
    }

}