package com.develop.food.base

import android.app.Application
import android.content.Context
import android.util.Log
import androidx.multidex.MultiDex
import com.blankj.utilcode.util.FileUtils
import com.develop.food.base.repo.CURRENT_USER_ID
import com.develop.food.base.repo.FoodDataProvider
import com.develop.food.base.utils.TopResumedActivityHolder
import com.lzf.easyfloat.EasyFloat
import java.io.File

open class FoodCookBaseApp: Application() {

    override fun onCreate() {
        super.onCreate()
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        application = this
        MultiDex.install(this)



        registerActivityLifecycleCallbacks(TopResumedActivityHolder)
    }

    companion object {
        lateinit var application: Application
    }

    fun deleteRoomDb() {

        FoodDataProvider.getUserDatabase().userInfoDao().apply {
            removeAllOnlineRecipe(CURRENT_USER_ID)
            removeAllFavouriteRecipe(CURRENT_USER_ID)
            removeAllHistoryRecipe(CURRENT_USER_ID)
            removeAllOnUserTag(CURRENT_USER_ID)
        }

        FoodDataProvider.getUserDatabase().devConfigDao().apply {
            removeAllDevVersion()
        }

        Log.d("TAG 删除room", "删除 FoodDataProvider Dao文件")

        val applicationDirectory = cacheDir.parent?.let { File(it) }
        if (applicationDirectory?.exists() == true) {

            val files = applicationDirectory.listFiles() ?: emptyArray()
            for (file in files) {
                //databases目录放着room 的db文件,重新解压需要删除
                if (file.name.equals("databases")) {
                    Log.d("TAG 删除room", "删除 databases的room-db文件")
                    FileUtils.delete(file)
                }
            }
        }
    }


}