package com.zxhl.shop

import android.app.Application
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.zx.ad.modulecommon.AdNetConfig
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.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)
        AdNetConfig.setBase_Url(AdNetConfig.Environment.PRODUCT)
        if (!SettingPreference.getFirstApp()) {
            initAppConfig()
        }

        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")
            }
        }
    }

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

    fun initAppConfig(){
        UMConfigure.init(AppContext.get(), UMConfigure.DEVICE_TYPE_PHONE, "")
        Utils.checkDebugMode(AppContext.get())
        PlatformConfig.setWeixin(Constant.WX_APPID, Constant.WX_APPKEY)
        PlatformConfig.setWXFileProvider("com.tencent.sample2.fileprovider");
        Config.init(AppContext.get())
        //初始化bugly
        CrashReport.initCrashReport(AppContext.get(), "f9fb9a9ef5", false);
        getOaid()
    }
}