package com.wall.photography.wallpaper

import android.app.Application
import android.util.Log
import com.anythink.core.api.ATSDK
import com.anythink.core.api.NetTrafficeCallback
import com.anythink.debug.api.ATDebuggerUITest
import com.wall.photography.wallpaper.data.Bean
import com.wall.photography.wallpaper.firebase.RemoteConfigNew
import com.wall.photography.wallpaper.manager.DealData
import com.wall.photography.wallpaper.room.MyDataBase
import com.wall.photography.wallpaper.topon.AdManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class MyApp : Application() {
    companion object {
        lateinit var application: Application
        const val TAG = "-------------tt"
        const val ROOM_NAME = "Photography_Wallpaper"
        const val ROOM_Version = 1
        const val TABLE_NAME = "wall_paper"
        var data_Wallpaper: List<Bean>? = null
        var data_animals: List<Bean>? = null
        var data_experimental: List<Bean>? = null
        var data_film: List<Bean>? = null
        var data_nature: List<Bean>? = null
        var data_streetphotography: List<Bean>? = null
        var data_texturespatterns: List<Bean>? = null
        var data_travel: List<Bean>? = null
        var dataAlready: Boolean = false
        const val pageSize = 10

        lateinit var strings: Array<String>



    }

    private val APPId = "h66b1d37c22a77"
    private val AppKey = "a3f5e17582cca6e43dcbb0732c125dec8"

    private val debug_Key = "63db2183733f4027ed5333d9ea59e1540d46b1a6"
    override fun onCreate() {
        super.onCreate()
        application = this
        Log.d(TAG, "--------------Application-onCreate")
        RemoteConfigNew.instance.init(this)
        initTopOnSDK()
        CoroutineScope(Dispatchers.IO).launch {
            Log.d(TAG, "---------------协程开始")
            strings = resources.getStringArray(R.array.category_name)
            val allWallpaper = MyDataBase.getInstance().getBeanDao().getCategoryCovert(strings[7])
            if (allWallpaper != null) {
                Log.d(TAG, "---------------协程结束  --")
                return@launch
            }
            for (i in 0 until 8) {
                val initData = initData(strings[i] + ".json")
                val subList = initData.subList(0, 500)
                for (data in subList) {
                    MyDataBase.getInstance().getBeanDao().insertBean(data)
                }

            }
            dataAlready = true
            Log.d(TAG, "---------------协程结束  --")
        }
        Log.d(TAG, "---------------Application onCreate end")
    }

    private fun initData(fileName: String): List<Bean> {
        Log.d(TAG, "-----------协程.......----fileName=$fileName")
        val fdInput = assets.open(fileName)
        val jsonStr = DealData.getJsonString(fdInput)
        val cateName = fileName.substringBefore(".json")
        val bean = DealData.getBean(this, jsonStr, cateName)
        return bean
    }

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

            override fun onErrorCallback(errorMsg: String) {
                Log.e(TAG, "onErrorCallback:$errorMsg")
            }
        })
        ATSDK.init(this, APPId, AppKey)
        AdManager.loadAllAd()
        //测试工具
//        ATDebuggerUITest.showDebuggerUI(this, debug_Key)
//        ATSDK.setNetworkLogDebug(true)
    }


}