package com.ujuad.demo

import android.app.Application
import com.ujuad.demo.constant.DemoConfig
import com.ujuad.demo.utils.DemoLogUtils
import com.ujuad.demo.viewmodel.AppViewModel
import com.ujusdk.adsdk.public.config.UjuAdInitConfig
import com.ujusdk.adsdk.public.UjuAdSdk
import com.ujusdk.adsdk.public.base.BaseInitListener
import com.ujusdk.adsdk.exception.UjuException

class MyApplication : Application() {
    val appViewModel: AppViewModel by lazy {
        AppViewModel(this)
    }

    companion object {
        // 单例实例
        lateinit var instance: MyApplication
            private set
    }

    override fun onCreate() {
        super.onCreate()
        // 保存单例实例
        instance = this

        // 初始化SDK，网络权限是普通权限，不需要动态请求
        initializeSdk()
    }

    /**
     * 初始化SDK
     */
    private fun initializeSdk() {
        val config = UjuAdInitConfig(
            appId = DemoConfig.APP_ID,
            appKey = DemoConfig.APP_KEY,
            isDebug = true,
            wxAppId = "wx"
        )

        UjuAdSdk.init(this, config)
        UjuAdSdk.start(object : BaseInitListener {
            override fun onInitSuccess() {
                appViewModel.sdkInitialized.value = true
            }

            override fun onInitFailed(error: UjuException) {
                appViewModel.sdkInitialized.value = false
                DemoLogUtils.e("SDK初始化失败: ${error.message}")
            }
        })

    }

}
