package com.all.pdfreader.pdf.reader

import android.app.Application
import android.content.Context
import androidx.annotation.StringRes
import com.all.pdfreader.pdf.reader.room.repository.PdfRepository
import com.all.pdfreader.pdf.reader.util.AnalyticsUtils
import com.all.pdfreader.pdf.reader.util.FileChangeObserver
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.RequestConfiguration
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader
import com.up.uploadlibrary.UpLoadManager

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()
        AnalyticsUtils.init()
        AnalyticsUtils.logEvent(AnalyticsUtils.Event.APP_LAUNCH_CK)
        instance = this
        isNeedFullScan = true
        // 初始化文件变化监听（不立即启动）
        fileChangeObserver = FileChangeObserver(this)
        // 初始化数据库
        PdfRepository.initialize(this)
        // 初始化pdfbox
        PDFBoxResourceLoader.init(this)
        // 上传
        UpLoadManager.init(context = this, tag = "PRApp_upload_task") { _, _ -> }
        // 广告初始化
        MobileAds.initialize(this)
    }

    private fun initMobileAds() {
        val testDeviceIds = listOf("9A96E667D69B45F744FD7D724DF8B093")
        val configuration = RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build()
        MobileAds.setRequestConfiguration(configuration)
    }

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