package rowedto.jockeyedto.sheetedto.raceedto.pavementedto

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.base.datarecovery.BuildConfig
import org.json.JSONObject
import rowedto.jockeyedto.sheetedto.interactiveedto.twistedto.Banquetedto.changeTimerSp
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Driededto

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

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

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

                        }

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

            }

            override fun onInstallReferrerServiceDisconnected() {

            }
        })
    }

    fun requestCfg(callBackAd: Boolean) {
        Permissionedto.requestCfg {
            if (callBackAd) {
               // Scandaledto.initAdmobMaxAd()
            }

            changeTimerSp()
        }
    }
}