package com.sunny.app.soft.timberkeyboardnew

import android.app.Application
import android.content.Context
import android.util.Log
import com.anythink.core.api.ATSDK
import com.anythink.core.api.NetTrafficeCallback
import com.anythink.debug.api.ATDebuggerUITest
import com.sunny.app.soft.timberkeyboardnew.data.DataManager
import com.sunny.app.soft.timberkeyboardnew.room.MyDatabase

import com.sunny.app.soft.timberkeyboardnew.topon.AdManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class App : Application() {

    companion object {
        lateinit var appContext: Context
        const val TAG = "============="

        const val ROOM_NAME = "My_Keyboard"
        const val ROOM_VERSION = 1
        const val TABle_name_1 = "category"
        const val TABle_name_2 = "keyboard"
    }
    private val debugKey = "5a963ac260793c4ffdf9580cd63743d4ce258e51"
    private val appId = "h669e2559e94df"
    private val appKey = "a6c2a655e13bd18d58cb7ccaf46ac8468"

    override fun onCreate() {
        super.onCreate()
        appContext = this
        initTopOn()
        CoroutineScope(Dispatchers.IO).launch {
            Log.d(TAG, " 协程start")
            val list = MyDatabase.myDatabase.CategoryEntityDao().queryAllData()
            if(list.size<=0){
                DataManager.getCategoryEntity("Abstract.json")
                DataManager.getCategoryEntity("Chinese.json")
                DataManager.getCategoryEntity("Christmas.json")
                DataManager.getCategoryEntity("Glitter.json")
                DataManager.getCategoryEntity("Halloween.json")
                DataManager.getCategoryEntity("Pastel.json")
                DataManager.getCategoryEntity("Unicorn.json")
            }
            Log.d(TAG, " 协程end")
        }


    }

    private fun initTopOn() {

        ATSDK.checkIsEuTraffic(this, object : NetTrafficeCallback {
            override fun onResultCallback(isEU: Boolean) {
                Log.e(TAG, "onResultCallback:$isEU")
                if (isEU && ATSDK.getGDPRDataLevel(this@App) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(this@App)
                }
            }

            override fun onErrorCallback(errorMsg: String) {
                Log.e(TAG, "onErrorCallback:$errorMsg")
            }
        })

        ATSDK.init(this, appId, appKey)
//        ATSDK.setNetworkLogDebug(true)
        AdManager.loadAllAd()

        //测试工具
        //ATDebuggerUITest.showDebuggerUI(this,debugKey)
    }
}