package rallyedto.talledto.blewedto

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.base.datarecovery.BuildConfig
import org.json.JSONObject
import rallyedto.battleedto.acresedto.Criticismedto
import rallyedto.epicalyxedto.rodedto.trunkedto.Raidedto.changeTimerSp

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

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

        //InstallReferrer empty
        val referrerClient = InstallReferrerClient.newBuilder(Criticismedto.context).build()
        referrerClient.startConnection(object : InstallReferrerStateListener {
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                Musicaledto.logDebug(TAG, "responseCode=$responseCode")
                try {
                    when (responseCode) {
                        InstallReferrerClient.InstallReferrerResponse.OK -> {
                            Musicaledto.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())
                            Robedto.event("install_referrer", ext = obj)
                            Pianoedto.getInstance().put("referrerUrl", response.installReferrer)
                            if (listOf("gclid", "facebook", "instagram").all { !installInfo.contains(it, true) }) {
                                //自然用户
                                if (BuildConfig.DEBUG) {
                                    Pianoedto.getInstance().put("install_source", "channel")
                                } else {
                                    Pianoedto.getInstance().put("install_source", "origin")
                                }
                                Musicaledto.logDebug("glc", "origin user")
                            } else {
                                Musicaledto.logDebug("glc", "channel user")
                                //渠道用户
                                Pianoedto.getInstance().put("install_source", "channel")
                            }
                            requestCfg(true)

                        }

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

            }

            override fun onInstallReferrerServiceDisconnected() {

            }
        })
    }

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

            changeTimerSp()
        }
    }
}