package incredibleic.guffawic

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.easy.clean.BuildConfig
import tooic.fatalityic.Adventurousic
import tooic.fatalityic.redic.speckledic.hankeringic.Farawayic
import tooic.fatalityic.redic.speckledic.hankeringic.Upholsteryic
import org.json.JSONObject

/**
 * call before agree
 */
object InstallHelps {

    private val TAG = "InstallHelps"

    fun init(requestCfg: () -> Unit) {

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

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

            }

            override fun onInstallReferrerServiceDisconnected() {
            }
        })
    }
}