package com.ritoq.quickphone.business.helper

import android.util.Log
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.android.installreferrer.api.ReferrerDetails
import com.ritoq.quickphone.BuildConfig
import com.ritoq.quickphone.MyApplication
import com.ritoq.quickphone.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("inse", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("inse", 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 {
            //当与 Google Play 的 Install Referrer 服务连接完成时回调
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                try {
                    when (responseCode) {
                        InstallReferrerClient.InstallReferrerResponse.OK -> {
                            val response = referrerClient.installReferrer

                            eventInstallReferrer(response)
                            installReferrer = response.installReferrer
                            Log.e("tttttttttttttttttt", installReferrer)
                            installSource = if (listOf(
                                    "gclid",
                                    "facebook",
                                    "instagram"
                                ).all { !response.installReferrer.contains(it, true) }
                            ) {
                                //自然用户
                                if (BuildConfig.DEBUG) {
                                    "cl"
                                } else {
                                    "or"
                                }
                            } else {
                                "cl"
                            }
                            requestCfg()
                        }

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

            }

            //当与 Install Referrer 服务的连接意外断开时回调
            override fun onInstallReferrerServiceDisconnected() {
            }
        })
    }
}