package com.zhangxin.magicbox

import android.app.Application
import android.text.TextUtils
import com.umeng.commonsdk.UMConfigure
import com.umeng.socialize.PlatformConfig
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.ApplicationAsLibrary
import com.zxhl.cms.common.Config
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.utils.EventUtils.onEvent
import com.zxhl.cms.utils.MiitHelper
import com.zxhl.cms.utils.TTAdManagerHolder
import com.zxhl.cms.utils.Utils
import java.util.*

/**
 * Created by gaoleichao on 2018/12/26
 */
class Appli : Application() {
    private val mChildApplicationList = ArrayList<ApplicationAsLibrary>()

    override fun onCreate() {
        super.onCreate()
        AppContext.init(applicationContext)
        UMConfigure.setLogEnabled(true)
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "")
        Utils.checkDebugMode(this)
        PlatformConfig.setWeixin(Constant.WX_APPID, Constant.WX_APPKEY)
        PlatformConfig.setWXFileProvider("com.tencent.sample2.fileprovider");
        Config.init(this)
        getOaid()
        initByteDance()

    }

    private fun getOaid() {
        MiitHelper(appIdsUpdater).getDeviceIds(AppContext.mAppContext)
    }

    private val appIdsUpdater: MiitHelper.AppIdsUpdater = object : MiitHelper.AppIdsUpdater {
        override fun OnIdsAvalid(ids: String) {
            if (!TextUtils.isEmpty(ids)) {
                SettingPreference.setOaid(ids)
                onEvent("tab_imp", "获取到oaid$ids")
            }
        }
    }

    private fun initByteDance() {
//        TTAdSdk.init(
//            this,
//            TTAdConfig.Builder()
//                .appId("5229735") //  .appId("520728")
//                .useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
//                .appName("幸运魔盒")
//                .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
//                .allowShowNotify(true) //是否允许sdk展示通知栏提示AnalyticsProvider
//                .allowShowPageWhenScreenLock(true) //是否在锁屏场景支持展示广告落地页
//                .debug(true) //测试阶段打开，可以通过日志排查问题，上线时去除该调用
//                .directDownloadNetworkType(
//                    TTAdConstant.NETWORK_STATE_WIFI,
//                    TTAdConstant.NETWORK_STATE_3G
//                ) //允许直接下载的网络状态集合
//                .supportMultiProcess(false) //是否支持多进程，true支持
//                .build()
//        )
        TTAdManagerHolder.init(this)
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
//        for (app in mChildApplicationList) {
//            app.onTrimMemoryAsLibrary(this, level)
//        }
    }

    override fun onLowMemory() {
        super.onLowMemory()
//        for (app in mChildApplicationList) {
//            app.onLowMemoryAsLibrary(this)
//        }
    }

}