package com.zxbw.xxsq

import android.app.Application
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import com.tencent.bugly.crashreport.CrashReport
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(false)
        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)
        //初始化bugly
        CrashReport.initCrashReport(applicationContext, "f9fb9a9ef5", false);
        getOaid()
        initByteDance()
        val helper = AppFrontBackHelper()
        helper.register(this, object : AppFrontBackHelper.OnAppStatusListener {
            override fun onFront() {
                //应用切到前台处理
                Log.e("MXL", "应用切到前台处理")
//                val intent = Intent(AppContext.get(), PlayingMusicServices::class.java)
//                intent.putExtra("type", 1)
//                startService(intent)
            }

            override fun onBack() {
                Log.e("MXL", "应用切到后台处理")
                //启动服务，播放音乐
//                val intent = Intent(AppContext.get(), PlayingMusicServices::class.java)
//                intent.putExtra("type", 2)
//                startService(intent)
            }
        })

    }

    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() {
        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)
//        }
    }

}