package com.zxhy.solarmasterace.quicktools

import android.os.Environment
import android.util.Log
import java.io.File
import java.util.LinkedList
import java.util.Stack

object FileHelper {

    //key:directory path
    //value:directory sublist
    private val fileMap = HashMap<String, Array<File>?>()

    //递归遍历
    //只返回文件,不返回目录
    //递归返回是所有递归执行完后返回的,所以该方法没意义了
    fun recursiveTraverseFolder(
        folder: File,
        onDo: ((file: File) -> Unit)? = null
    ): ArrayList<File> {
        val fileList = arrayListOf<File>()
        folder.listFiles()?.forEach {
            if (it.isDirectory) {
                fileList.addAll(recursiveTraverseFolder(it) { file -> onDo?.invoke(file) })
            } else {
                fileList.add(it)
                onDo?.invoke(it)
            }
        }
        return fileList
    }

    //LinkedLis方式遍历
    fun linkedLisTraverseFolder(folder: File, onDo: ((file: File) -> Unit)?) {

        //添加第一层文件到链表
        val linkList = LinkedList<File>()
        val fileList = folder.listFiles()
        fileList?.forEach {
            if (it.isDirectory) {
                linkList.add(it)
            } else {
                onDo?.invoke(it)
            }
        }

        //链表取文件
        var tempFile: File
        while (!linkList.isEmpty()) {
            tempFile = linkList.removeFirst()
            val tempFileList = tempFile.listFiles()
            tempFileList?.forEach {
                if (it.isDirectory) {
                    linkList.add(it)
                } else {
                    onDo?.invoke(it)
                }
            }
        }
    }


    fun traverseFolderMap(folder: File) {
        folder.listFiles()?.forEach {
            if (it.isDirectory) {
                fileMap[it.absolutePath] = it.listFiles()
                traverseFolderMap(it)
            }
        }
    }

    private fun iniFileMap() {
        val rootDir = Environment.getExternalStorageDirectory()
        traverseFolderMap(rootDir)
    }

    //删除文件夹
    fun deleteDirectory(dir: File): Boolean {
        val stackFiles = Stack<File>()
        stackFiles.push(dir)
        while (stackFiles.size > 0) {
            val currentFile = stackFiles.peek()
            val subFiles = currentFile.listFiles()

            if (subFiles != null) {
                for (i in subFiles.indices) {
                    if (subFiles[i].isFile) {
                        if (!subFiles[i].delete()) {
                            return false
                        }
                    } else {
                        stackFiles.push(subFiles[i])
                    }
                }
            }
            if (currentFile === stackFiles.peek()) {
                if (!currentFile.delete()) {
                    return false
                }
                stackFiles.pop()
            }
        }
        return true
    }

    /**
     * 复制文件夹
     * @param source 原文件夹
     * @param target 目标文件夹
     */
    fun copyDirectory(source: File, target: File) {
        try {
            if (source.isDirectory) {
                if (!target.exists()) target.mkdir()

                source.list()?.map { sub ->
                    copyDirectory(File(source, sub), File(target, sub))
                }
            } else {
                if (!target.exists()) {
                    source.copyTo(target)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

}