package com.yzx.escreen

import android.app.Application
import com.blankj.utilcode.util.Utils
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpHeaders
import com.lzy.okgo.model.HttpParams
import com.tencent.smtt.export.external.TbsCoreSettings
import com.tencent.smtt.sdk.QbSdk
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import com.yzx.escreen.config.Config
import com.yzx.escreen.model.User


class App : Application() {


    companion object {
        var app: App? = null
        fun getContext(): App {
            return app ?: App()
        }
    }

    override fun onCreate() {
        super.onCreate()
        app = this

        val headers = HttpHeaders()
        val user = User.getUser()
        headers.put("phone",user.phone)
        headers.put("token",user.token)
        val params = HttpParams()
        params.put("app_id",Config.APP_ID)
        OkGo.getInstance()
            .addCommonHeaders(headers)
            .addCommonParams(params)
            .init(this)


        Utils.init(this)

        UMConfigure.init(
            this,
            "60334d01425ec25f10fbd5b4",
            "ebook",
            UMConfigure.DEVICE_TYPE_PHONE,
            ""
        )
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)

        // 在调用TBS初始化、创建WebView之前进行如下配置
        val map = HashMap<String, Any>()
        map[TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
        map[TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
        QbSdk.initTbsSettings(map)
        //腾讯x5内核
        QbSdk.initX5Environment(this,  object : QbSdk.PreInitCallback {
            override fun onCoreInitFinished() {
            }

            /**
             * 预初始化结束
             * 由于X5内核体积较大，需要依赖网络动态下发，所以当内核不存在的时候，默认会回调false，此时将会使用系统内核代替
             * @param isX5 是否使用X5内核
             */
            override fun onViewInitFinished(isX5: Boolean) {
            }
        })
        QbSdk.setDownloadWithoutWifi(true)


    }


}