package com.drawing.drawsketch

import android.app.Application
import android.content.Context
import android.util.Log

class App : Application() {

    companion object {
        val TAG = "#################"
        var assetFolders = listOf<String>()
        lateinit var mInstance:App
    }

    override fun onCreate() {
        super.onCreate()
        mInstance = this
        DbBase.init(this)
        assetFolders = getAssetFolders(this)
    }

    fun getAssetFolders(context: Context): List<String> {
        val assetManager = context.assets
        val folderNames = mutableListOf<String>()
        val excluded = setOf("images", "webkit")
        fun findFolders(path: String) {
            val list = assetManager.list(path) ?: return
            for (name in list) {
                if (excluded.contains(name)) continue
                if (!name.startsWith("kt_")) continue
                val fullPath = if (path.isEmpty()) name else "$path/$name"
                val children = assetManager.list(fullPath)
                if (!children.isNullOrEmpty()) {
                    // 如果该路径还有内容，视为文件夹
                    folderNames.add(fullPath)
                    Log.d(App.TAG,"add name=$fullPath")
                    findFolders(fullPath) // 递归查找子文件夹
                }
            }
        }

        findFolders("")
        return folderNames
    }
}