package com.base.locationsharewhite.helper

import android.annotation.SuppressLint
import android.content.Context
import com.example.mydemo.strategy.ads.AdsConfigBean
import com.example.mydemo.strategy.ads.AdsMgr
import com.example.mydemo.strategy.fcm.MsgConfig
import com.example.mydemo.strategy.fcm.MsgConfigBean
import com.example.mydemo.strategy.fcm.MsgMgr
import com.example.mydemo.strategy.fcm.MsgType
import com.example.mydemo.strategy.fcm.RemoteConfigBean
import com.example.mydemo.strategy.notification.NotificationMgr
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.gson.Gson

/**
 * admob广告与fireBase与FCM的初始化等管理类
 */
@SuppressLint("StaticFieldLeak")
object GoogleSdkMgr {
    lateinit var context: Context
    fun init(
        context: Context, packageName: String,
        msgConfig: MsgConfig = MsgConfig.Builder()
            //这里只是展示默认值并没有配置自定义通知栏视图即addRemoteBean方法，默认情况下，1.timer默认首次3分钟，之后7分钟。
            .setConfig(MsgConfigBean(3, 7), MsgType.TIMER)
            //2.解锁场景，默认间隔1分钟，默认无次数限制。
            .setConfig(MsgConfigBean(1), MsgType.UNLOCK)
            //3.电量场景，默认间隔1小时，默认无次数限制。
            .setConfig(MsgConfigBean(60), MsgType.BATTERY)
            //4.安装与卸载场景，默认间隔5分钟，默认无次数限制。
            .setConfig(MsgConfigBean(5), MsgType.PACKAGE)
            //设置悬停通知配置，默认开启，0代表关闭，1代表开启，持续时间默认为5s
            .setHover(MsgConfigBean(1, 5))
            .build(),
        //默认情况下，广告展示，点击和请求都没有次数限制，间隔时间为1分钟。
        adsConfigBean: AdsConfigBean = AdsConfigBean()
    ) {
        GoogleSdkMgr.context = context.applicationContext
        //初始化注册通知栏通知渠道等
        NotificationMgr.init(context)
        //先注册自定义通知栏，这个不需要等云控数据回调
        MsgMgr.remoteViewContainer = msgConfig.remoteViewContainer
        val remoteConfig = FirebaseRemoteConfig.getInstance()
        remoteConfig.fetchAndActivate().addOnCompleteListener { task ->
            if (task.isSuccessful) {
                //解析云控消息配置
                val config = remoteConfig.getString(ConstConfig.REMOTE_MSG)
                //Log.d(this::class.java.simpleName, config)
                val bean: RemoteConfigBean? = Gson().fromJson(config, RemoteConfigBean::class.java)
                // Log.d(this::class.java.simpleName, bean.toString())
                bean?.apply {
                    hover?.let {
                        msgConfig.hover = it
                    }
                    unlock?.let {
                        msgConfig.unlock = it
                    }
                    battery?.let {
                        msgConfig.battery = it
                    }
                    pkChange?.let {
                        msgConfig.packageChange = it
                    }
                    timer?.let {
                        msgConfig.timer = it
                    }
                }

                //解析云控广告配置
                val adConfig = remoteConfig.getString(ConstConfig.REMOTE_AD)
                //Log.d(this::class.java.simpleName, adConfig)
                val adBean: AdsConfigBean? = Gson().fromJson(adConfig, AdsConfigBean::class.java)
                //Log.d(this::class.java.simpleName, adBean.toString())
                adBean?.apply {
                    adsConfigBean.isInBlackList = isInBlackList
                    adsConfigBean.numDisplayLimit = numDisplayLimit
                    adsConfigBean.numRequestLimit = numRequestLimit
                    adsConfigBean.numClickLimit = numClickLimit
                    adsConfigBean.timeInterval = timeInterval
                }
            }
            //Log.d(this::class.java.simpleName, msgConfig.toString())
            //Log.d(this::class.java.simpleName, adsConfigBean.toString())
            //初始化通知栏推送相关业务
            MsgMgr.init(context, packageName, msgConfig)
            //初始化广告相关业务
            AdsMgr.init(context, adsConfigBean)
        }
    }
}