package com.various.funny.sounds.audio

import android.annotation.SuppressLint
import android.app.Application
import android.content.Intent
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkInitializationConfiguration
import com.various.funny.sounds.audio.db.MyRoomBase
import com.various.funny.sounds.audio.helper.ComUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class VariousFunnySounds : Application() {

    companion object {
        lateinit var appContext: VariousFunnySounds
        const val MAX_SDK = "3cUMfTcsZKzlJevxK4IkNysgDAeQA4B5w332p3g8B9ZAgC54WQNZLVxuxnCx4sCHA5StLJnDTAFa68mFTi8rd8"
        const val MAX_Provider = "max"
        const val MAX_INIT_SDK_ACTION = "init_sdk_success"
        var initSDkOk = false
    }


    override fun onCreate() {
        super.onCreate()
        appContext = this
        MAXSDK()
        getData()
    }

    @SuppressLint("SuspiciousIndentation")
    private fun getData() {
        val open = assets.open("resource_target.json")
        CoroutineScope(Dispatchers.IO).launch {
            if (MyRoomBase.dataBase.getNewBeanDao().getData().isEmpty()) {
                val fileString = ComUtils.getFileString(open)
                  ComUtils.parseJsonStr(fileString)
            }

        }
    }
    private fun MAXSDK(){
        val initConfig =
            AppLovinSdkInitializationConfiguration.builder(MAX_SDK, this)
                .setMediationProvider(MAX_Provider)
                .build()
        AppLovinSdk.getInstance(this).initialize(initConfig) {
            initSDkOk = true
            LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(MAX_INIT_SDK_ACTION))
        }
        AppLovinSdk.getInstance(this).settings.setVerboseLogging(true)
    }
}