package com.rgt.superr.business.helper

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.android.installreferrer.api.ReferrerDetails
import com.rgt.superr.BuildConfig
import com.rgt.superr.MyApplication
import com.rgt.superr.utils.AppPreferences
import org.json.JSONObject

/**
 * call before agree
 */

object InstallHelps {

    private val TAG = "InstallHelps"

    var installReferrer = ""
        get() {
            return AppPreferences.getInstance().getString("install_referrer", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("install_referrer", value, true)
        }

    //用户来源
    var installSource = ""
        get() {
            return AppPreferences.getInstance().getString("install_source", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("install_source", value, true)
        }

    //用户是否上报过
    var installEvent = false
        get() {
            return AppPreferences.getInstance().getBoolean("installEvent", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("installEvent", value, true)
        }

    fun eventInstallReferrer(response: ReferrerDetails) {
        val obj = JSONObject()
        obj.put("referrerUrl", response.installReferrer)
        obj.put("referrerClickTime", response.referrerClickTimestampSeconds)
        obj.put("appInstallTime", response.installBeginTimestampSeconds)
        obj.put("instantExperienceLaunched", response.installReferrer.toString())
        EventUtils.event("install_referrer", ext = obj)
    }


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

        if (installEvent) {
            requestCfg.invoke()
            return
        }


        val referrerClient = InstallReferrerClient.newBuilder(MyApplication.appContext).build()
        referrerClient.startConnection(object : InstallReferrerStateListener {
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                try {
                    when (responseCode) {
                        InstallReferrerClient.InstallReferrerResponse.OK -> {
                            val response = referrerClient.installReferrer

                            installReferrer = response.installReferrer
                            eventInstallReferrer(response)

                            installSource = if (listOf(
                                    "gclid",
                                    "facebook",
                                    "instagram"
                                ).all { !response.installReferrer.contains(it, true) }
                            ) {
                                //自然用户
                                if (BuildConfig.DEBUG) {
                                    "channel"
                                } else {
                                    "origin"
                                }
                            } else {
                                "channel"
                            }

                            requestCfg()
                        }

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

            }

            override fun onInstallReferrerServiceDisconnected() {
            }
        })
    }
}