package com.all.pdfreader.pro.app

import android.app.Application
import android.content.Context
import androidx.annotation.StringRes
import com.all.pdfreader.pro.app.room.repository.PdfRepository
import com.all.pdfreader.pro.app.util.FileChangeObserver
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader

class PRApp : Application() {

    companion object {
        private lateinit var instance: PRApp

        fun getInstance(): PRApp = instance

        fun getContext(): Context = instance.applicationContext

        //是新创建了界面，则需要全盘扫描，进入onResume判定，扫描方法调用后置为false
        var isNeedFullScan = false

        fun getStringRes(@StringRes resId: Int, vararg formatArgs: Any): String {
            return instance.getString(resId, *formatArgs)
        }
    }

    private lateinit var fileChangeObserver: FileChangeObserver

    override fun onCreate() {
        super.onCreate()
        instance = this
        isNeedFullScan = true
        // 初始化文件变化监听（不立即启动）
        fileChangeObserver = FileChangeObserver(this)

        // 初始化数据库
        PdfRepository.initialize(this)
        // 初始化pdfbox
        PDFBoxResourceLoader.init(this)
    }

    // 在权限授权后调用
    fun startFileChangeObserving() {
        fileChangeObserver.startObserving()
    }
}
