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 setInstallSource(response: ReferrerDetails) {
        installReferrer = response.installReferrer
        installSource = if (listOf(
                "gclid",
                "facebook",
                "instagram"
            ).all { !response.installReferrer.contains(it, true) }
        ) {
            "origin"
        } else {
            "channel"
        }
    }

    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

                            setInstallSource(response)
                            eventInstallReferrer(response)
                            requestCfg()
                        }

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

            }

            override fun onInstallReferrerServiceDisconnected() {
            }
        })
    }
}