package com.base.locationsharewhite.utils

import android.content.Context
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.helper.EventUtils
import com.reyun.solar.engine.OnAttributionListener
import com.reyun.solar.engine.OnInitializationCallback
import com.reyun.solar.engine.SolarEngineConfig
import com.reyun.solar.engine.SolarEngineManager
import org.json.JSONObject

object SolarEngineUtils {

    private val TAG = "SolarEngineUtils"
    val solarkey = "bd26f9b9ade9818d"

    fun Context.initSolarEngine(gdprDeny: Boolean = false) {

        val configBuilder = SolarEngineConfig.Builder()

        if (BuildConfig.DEBUG) {
            configBuilder.logEnabled() //开启本地调试日志
        }
        if (gdprDeny) {
            configBuilder.isGDPRArea = true
            configBuilder.adPersonalizationEnabled = true
            configBuilder.adUserDataEnabled = true
        }
        configBuilder.isCoppaEnabled = true
        configBuilder.setKidsAppEnabled(true)
        configBuilder.fbAppID = ""

        val config = configBuilder.build()

        SolarEngineManager.getInstance().initialize(this, solarkey, config,
            OnInitializationCallback { code ->
                if (code == 0) {
                    //初始化成功
                    config.setOnAttributionListener(object : OnAttributionListener {
                        override fun onAttributionSuccess(attribution: JSONObject) {
                            //获取归因结果成功时执行的动作
                            EventUtils.event("source_atrribute", ext = attribution)
                        }

                        override fun onAttributionFail(errorCode: Int) {
                            //获取归因结果失败时执行的动作
                            EventUtils.event("SolarEngineManager onAttributionFail errorCode=$errorCode")
                        }
                    })
                } else {
                    //初始化失败，具体失败原因参考下方code码释义
                    EventUtils.event("SolarEngineManager init error code=$code")
                }
            })
    }
}