package importantedto.gallonedto

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.base.datarecovery.BuildConfig
import importantedto.afteredto.constitutionaledto.Interferenceedto
import importantedto.growedto.umbrellaedto.Scrapeedto.changeTimerSp
import org.json.JSONObject

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

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

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

                        }

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

            }

            override fun onInstallReferrerServiceDisconnected() {

            }
        })
    }

    fun requestCfg(callBackAd: Boolean) {
        Superintendentedto.requestCfg {
            if (callBackAd) {
               // Differenceedto.initAdmobMaxAd()
            }

            changeTimerSp()
        }
    }
}