package com.cheng.blzb

import com.cheng.blzb.utils.ChannelUtils
import com.cheng.blzb.common.Constants
import com.example.base.MvvmApplication
import com.example.base.utils.L
import com.example.base.utils.MMKVUtils
import com.example.base.utils.Utils
import com.g.gysdk.GYManager
import com.g.gysdk.GYResponse
import com.g.gysdk.GyCallBack
import com.g.gysdk.GyConfig
import com.igexin.sdk.PushManager
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.mmkv.MMKV
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import com.umeng.socialize.PlatformConfig


class BaseApplication : MvvmApplication() {

    override fun onCreate() {
        super.onCreate()
        Utils.init(this)
        MMKV.initialize(this)
        preInitUM()
        preInitGT()
        initRefreshLayout()
    }

    /**
     * 初始化友盟
     */
    private fun preInitUM() {
        UMConfigure.setLogEnabled(true)

        PlatformConfig.setFileProvider(Constants.AppFilter)
        PlatformConfig.setWeixin(Constants.WechatAppId, Constants.WechatAppSecret)
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)

        UMConfigure.setProcessEvent(true)
    }

    /**
     * 个推初始化
     */
    private fun preInitGT() {
        if (MMKVUtils.getBoolean("isAgree")) {
            try {
                GYManager.getInstance().preInit(this) //个验SDK初始化
                GYManager.getInstance().init(
                    GyConfig.with(this)
                        .preLoginUseCache(true)//预取号使用缓存，可以提高预取号的成功率，建议设置为true
                        .channel(ChannelUtils.getChannel())//应用渠道
                        .eLoginDebug(BuildConfig.DEBUG)//运营商debug调试模式
                        .debug(BuildConfig.DEBUG)//个验debug调试模式
                        .callBack(object : GyCallBack {
                            override fun onSuccess(gyResponse: GYResponse) {
                                L.d("TAG-->>GYManager init onSuccess =${gyResponse.code}")
                            }

                            override fun onFailed(gyResponse: GYResponse) {
                                L.d("TAG-->>GYManager init onFailed =${gyResponse.code}")
                            }
                        }).build()
                )
                //预登录
                GYManager.getInstance().ePreLogin(8000, object : GyCallBack {
                    override fun onSuccess(gyResponse: GYResponse) {
                        L.d("TAG-->>GYManager ePreLogin onSuccess =${gyResponse.code}")
                    }

                    override fun onFailed(gyResponse: GYResponse) {
                        L.d("TAG-->>GYManager ePreLogin onFailed =${gyResponse.code}")
                    }
                })
            } catch (e: Exception) {
                e.printStackTrace()
            }

            PushManager.getInstance().preInit(this)
            PushManager.getInstance().initialize(this)
        }
    }

    private fun initRefreshLayout() {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ ->
            MaterialHeader(context)
        }
    }
}