package com.base.datarecovery.help

import android.os.Environment
import android.util.Log
import com.base.datarecovery.utils.LogEx
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import java.io.File
import java.io.IOException
import java.io.RandomAccessFile
import java.util.LinkedList


object FileHelp {

    private val TAG = "FileHelp"

    fun CoroutineScope.loadFileByFilter(
        mutableSharedFlow: MutableSharedFlow<String>? = null,
        folder: File,
        filter: (file: File) -> Boolean,
        onDo: ((file: File) -> Unit)? = null,
        onFinish: (() -> Unit)? = null
    ) = launch(Dispatchers.IO) {

        //添加第一层文件到链表
        val linkList = LinkedList<File>()
        val fileList = folder.listFiles()
        fileList?.forEach {
            if (it.isDirectory) {
                linkList.add(it)
            } else {
                val flag = filter(it)
                LogEx.logDebug(TAG, "$it flag=$flag")
                if (flag) {
                    onDo?.invoke(it)
                }
                mutableSharedFlow?.emit(it.absolutePath)
            }
        }

        //链表取文件
        var tempFile: File
        while (!linkList.isEmpty()) {
            tempFile = linkList.removeFirst()
            val tempFileList = tempFile.listFiles()
            tempFileList?.forEach {
                if (it.isDirectory) {
                    linkList.add(it)
                } else {
                    val flag = filter(it)
//                    LogEx.logDebug(TAG, "$it flag=$flag")
                    if (flag) {
                        onDo?.invoke(it)
                    }
                    mutableSharedFlow?.emit(it.absolutePath)
                }
            }
        }
        launch(Dispatchers.Main) {
            onFinish?.invoke()
        }
    }

    fun readFileRange(filePath: String, start: Long, end: Long): ByteArray {
        var data = byteArrayOf()
        try {
            val file = RandomAccessFile(filePath, "r")
            file.seek(start)
            val length = end - start
            data = ByteArray(length.toInt())
            file.readFully(data)
            file.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return data
    }

    fun getDirFiles(
        folder: File,
    ): ArrayList<File> {
        val result = arrayListOf<File>()
        //添加第一层文件到链表
        val linkList = LinkedList<File>()
        folder.listFiles()?.forEach {
            if (it.isDirectory) {
                linkList.add(it)
            } else {
                result.add(it)
            }
        }

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