package com.ym.modulecommon.base

import android.os.Handler
import android.text.TextUtils
import androidx.multidex.MultiDexApplication
import cn.tongdun.android.shell.FMAgent
import cn.tongdun.android.shell.inter.FMCallback
import com.ym.modulecommon.AdConstant
import com.ym.modulecommon.AppliContext
import com.ym.modulecommon.net.NetConfig
import com.ym.modulecommon.utils.*

/**
 * Created by erwin on 2020/11/26
 */
open class BaseApplication : MultiDexApplication() {

    override fun onCreate() {
        super.onCreate()
        AppliContext.init(this)
        getOaid()
        mHandler?.sendEmptyMessageDelayed(0, 100)
        AdConstant.param.ReportSessionId = "" + System.currentTimeMillis() + Utils.getRandom(10)
    }

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

    private val appIdsUpdater =
        MiitHelper.AppIdsUpdater { ids ->
            if (!TextUtils.isEmpty(ids)) {
                AdEventUtils.onEventUM("app_active","获取到oaid"+ids)
                AdPreference.setOaid(ids)
            }
        }

    private var mHandler: Handler = Handler {
        when (it.what) {
            0 -> {
                FMAgent.initWithCallback(this, if (NetConfig.sEnvironment == NetConfig.Environment.PRODUCT) FMAgent.ENV_PRODUCTION else FMAgent.ENV_SANDBOX, FMCallback { s ->//BlackBox
                    BlackBoxEventUtils.onEventBlackBox(s)
                    AdPreference.setBlackBox(s)
                })
            }
        }
        false
    }
}