package com.base.datarecovery.utils

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.base.datarecovery.BuildConfig
import com.base.datarecovery.fcm.RecoveryTimerManager.changeTimerSp
import com.base.datarecovery.help.BaseApplication
import org.json.JSONObject

/**
 * call before agree
 */
object InstallHelps {
    private val TAG = "InstallHelps"
    fun init() {

        //InstallReferrer not empty
//        if (!TextUtils.isEmpty(AppPreferences.getInstance().getString("install_source", ""))) {
//            if (AdmobMaxHelper.haveSpAdmobTrueMaxFalse()) {
//                AdmobMaxHelper.initAdmobMaxAd()
//                requestCfg(false)
//            } else {
//                requestCfg(true)
//            }
//            return
//        }

        //InstallReferrer empty
        val referrerClient = InstallReferrerClient.newBuilder(BaseApplication.context).build()
        referrerClient.startConnection(object : InstallReferrerStateListener {
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                LogEx.logDebug(TAG, "responseCode=$responseCode")
                try {
                    when (responseCode) {
                        InstallReferrerClient.InstallReferrerResponse.OK -> {
                            LogEx.logDebug(TAG, "InstallReferrerResponse.OK")
                            val response = referrerClient.installReferrer
                            val installInfo = response.installReferrer
                            val obj = JSONObject()
                            obj.put("referrerUrl", response.installReferrer)
                            obj.put("referrerClickTime", response.referrerClickTimestampSeconds)
                            obj.put("appInstallTime", response.installBeginTimestampSeconds)
                            obj.put("instantExperienceLaunched", installInfo.toString())
                            EventUtils.event("install_referrer", ext = obj)
                            AppPreferences.getInstance().put("referrerUrl", response.installReferrer)
                            if (listOf("gclid", "facebook", "instagram").all { !installInfo.contains(it, true) }) {
                                //自然用户
                                if (BuildConfig.DEBUG) {
                                    AppPreferences.getInstance().put("install_source", "channel")
                                } else {
                                    AppPreferences.getInstance().put("install_source", "origin")
                                }
                                LogEx.logDebug("glc", "origin user")
                            } else {
                                LogEx.logDebug("glc", "channel user")
                                //渠道用户
                                AppPreferences.getInstance().put("install_source", "channel")
                            }
                            requestCfg(true)

                        }

                        else -> {
                            EventUtils.event("install_referrer_error")
                            requestCfg(true)
                        }
                    }
                } catch (_: Exception) {
                    EventUtils.event("install_referrer_error")
                }

            }

            override fun onInstallReferrerServiceDisconnected() {

            }
        })
    }

    fun requestCfg(callBackAd: Boolean) {
        NewComUtils.requestCfg {
            if (callBackAd) {
               // AdmobMaxHelper.initAdmobMaxAd()
            }

            changeTimerSp()
        }
    }
}