package com.zxhy.fastfilemanagerpro.kokolistfile

import android.content.Context
import android.os.Environment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako
import com.zxhy.fastfilemanagerpro.kokodata.KoFileDatako.Companion.koFileDatako
import com.zxhy.fastfilemanagerpro.kokotools.HelperTraverse.linkedLisTraverseFolder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.io.File
import kotlin.reflect.KFunction1

class KoListFilekoViewModel : ViewModel() {


    fun isEmptyFilter(file: File): Boolean {
        return file.length() == 0L
    }

    fun isLargeFilter(file: File): Boolean {
        return file.length() >= 10 * 1024 * 1024L
    }

    fun isAudioFilter(file: File): Boolean {
        return file.length() >= 10 * 1024 * 1024L
    }

    fun isLogFilter(file: File): Boolean {
        return file.length() >= 10 * 1024 * 1024L
    }

    fun isZipFilter(file: File): Boolean {
        return file.length() >= 10 * 1024 * 1024L
    }

    fun isApkFilter(file: File): Boolean {
        return file.length() >= 10 * 1024 * 1024L
    }

    fun isDocumentsFilter(file: File): Boolean {
        return file.length() >= 10 * 1024 * 1024L
    }

    fun loadEmptyFile(
        context: Context,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {
        loadFile(context, filter = (::isEmptyFilter),
            onDo = { list ->
                onDo.invoke(list)
            },
            onFinish = { list ->
                onFinish.invoke(list)
            }
        )
    }

    fun loadLargeFile(
        context: Context,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {
        loadFile(context, filter = (::isLargeFilter),
            onDo = { list ->
                onDo.invoke(list)
            },
            onFinish = { list ->
                onFinish.invoke(list)
            }
        )
    }

    fun loadAudioFile(
        context: Context,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {
        loadFile(context, filter = (::isAudioFilter),
            onDo = { list ->
                onDo.invoke(list)
            },
            onFinish = { list ->
                onFinish.invoke(list)
            }
        )
    }

    fun loadLogFile(
        context: Context,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {
        loadFile(context, filter = (::isLogFilter),
            onDo = { list ->
                onDo.invoke(list)
            },
            onFinish = { list ->
                onFinish.invoke(list)
            }
        )
    }

    fun loadZipFile(
        context: Context,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {
        loadFile(context, filter = (::isZipFilter),
            onDo = { list ->
                onDo.invoke(list)
            },
            onFinish = { list ->
                onFinish.invoke(list)
            }
        )
    }

    fun loadApkFile(
        context: Context,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {
        loadFile(context, filter = (::isApkFilter),
            onDo = { list ->
                onDo.invoke(list)
            },
            onFinish = { list ->
                onFinish.invoke(list)
            }
        )
    }


    fun loadDocumentsFile(
        context: Context,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {
        loadFile(context, filter = (::isDocumentsFilter),
            onDo = { list ->
                onDo.invoke(list)
            },
            onFinish = { list ->
                onFinish.invoke(list)
            }
        )
    }


    private fun loadFile(
        context: Context,
        filter: (file: File) -> Boolean,
        onDo: (list: List<KoFileDatako>) -> Unit,
        onFinish: (list: List<KoFileDatako>) -> Unit
    ) {

        viewModelScope.launch(Dispatchers.IO) {
            val root = Environment.getExternalStorageDirectory()
            val resultList = arrayListOf<KoFileDatako>()
            var firstSend = false

            linkedLisTraverseFolder(root) { file ->
                if (filter(file)) {
                    resultList.add(file.koFileDatako(context))
                    if (resultList.size == 20 && !firstSend) {
                        onDo.invoke(resultList)
                        resultList.clear()
                        firstSend = true
                    }
                }
            }
            onFinish.invoke(resultList)
        }

    }

    fun deleteFiles(files: List<File>, finishAction: () -> Unit) =
        viewModelScope.launch(Dispatchers.IO) {
            runCatching {
                files.forEach {
                    if (it.exists()) {
                        it.delete()
                    }
                }
            }
            finishAction.invoke()
        }


}