package com.zxhy.solarmasterace.quicktools

import android.os.Environment
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import com.zxhy.solarmasterace.data.DupFileBean
import com.zxhy.solarmasterace.data.FileBean
import com.zxhy.solarmasterace.data.isApk
import com.zxhy.solarmasterace.data.isAudio
import com.zxhy.solarmasterace.data.isDocument
import com.zxhy.solarmasterace.data.isExcel
import com.zxhy.solarmasterace.data.isImage
import com.zxhy.solarmasterace.data.isLogFile
import com.zxhy.solarmasterace.data.isPPt
import com.zxhy.solarmasterace.data.isPdf
import com.zxhy.solarmasterace.data.isTxt
import com.zxhy.solarmasterace.data.isVideo
import com.zxhy.solarmasterace.data.isWord
import com.zxhy.solarmasterace.data.isZip
import com.zxhy.solarmasterace.data.toFileBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.util.Stack

fun dupImage(): List<DupFileBean> {
    val rootDir = Environment.getExternalStorageDirectory()
    val list = FileHelper.recursiveTraverseFolder(rootDir)
    val dupMap = HashMap<String, ArrayList<File>>()
    list.map {
        if (it.isImage()) {
            val md5 = it.digestMd5().byteArrayToHexString()
            val dupList = dupMap[md5]
            if (dupList == null) {
                val newList = arrayListOf<File>()
                dupMap[md5] = newList
                newList.add(it)
            } else {
                dupList.add(it)
            }
        }
    }
    val dupList = dupMap.filter { it.value.size > 1 }
        .map { map ->
            DupFileBean(
                md5 = map.key,
                dupData = map.value.map { file ->
                    FileBean(
                        name = file.name,
                        path = file.absolutePath
                    )
                }
            )
        }
    return dupList
}

fun largeFile(
    lifecycleScope: LifecycleCoroutineScope,
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    lifecycleScope.launch(Dispatchers.IO) {
        val rootDir = Environment.getExternalStorageDirectory()
        val tempList = arrayListOf<FileBean>()
        var count = 0
        FileHelper.linkedLisTraverseFolder(rootDir) {
            if (it.length() > 10 * 1024 * 1024) {
                tempList.add(it.toFileBean())
                count++
                if (tempList.size == 10) {
                    onDo?.invoke(tempList)
                    tempList.clear()
                }
            }
        }
        onFinish?.invoke(tempList, count)

    }

}

fun audioFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isAudio()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun apkFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isApk()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}


fun zipFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isZip()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun logFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isLogFile()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun documentFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isDocument()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun txtFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isTxt()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun pdfFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isPdf()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun pptFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isPPt()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun excelFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isExcel()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}


fun wordFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isWord()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}


fun emptyFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.length() == 0L) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}

fun imageFile(
    lifecycleCoroutineScope: LifecycleCoroutineScope,
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Long) -> Unit)? = null
) {
    lifecycleCoroutineScope.launch(Dispatchers.IO) {
        val rootDir = Environment.getExternalStorageDirectory()
        val tempList = arrayListOf<FileBean>()
        var count = 0L
        FileHelper.linkedLisTraverseFolder(rootDir) {
            if (it.isImage()) {
                tempList.add(it.toFileBean(count))
                count++
                if (tempList.size == 10) {
                    onDo?.invoke(tempList)
                    tempList.clear()
                }
            }
        }
        onFinish?.invoke(tempList, count)
    }
}


fun videoFile(
    onDo: ((file: List<FileBean>) -> Unit)? = null,
    onFinish: ((file: List<FileBean>, count: Int) -> Unit)? = null
) {
    val rootDir = Environment.getExternalStorageDirectory()
    val tempList = arrayListOf<FileBean>()
    var count = 0
    FileHelper.linkedLisTraverseFolder(rootDir) {
        if (it.isVideo()) {
            tempList.add(it.toFileBean())
            count++
            if (tempList.size == 10) {
                onDo?.invoke(tempList)
                tempList.clear()
            }
        }
    }
    onFinish?.invoke(tempList, count)
}


fun deleteFiles(files: List<File>) {
    try {
        files.map {
            if (it.exists()) it.delete()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

