package com.zxdemo.utils
import android.text.TextUtils
import android.util.Log
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.zxdemo.ZxApplication
import com.zxdemo.http.ZxHttp
import org.json.JSONObject

object InstallRefeerUtils {
    fun init() {
        if (TextUtils.isEmpty(SpUtils.getInstance().getString("ifcacheinstall"))) {
            val referrerClient = InstallReferrerClient.newBuilder(ZxApplication.context).build()
            referrerClient.startConnection(object : InstallReferrerStateListener {
                override fun onInstallReferrerSetupFinished(responseCode: Int) {
                    try {
                        when (responseCode) {
                            InstallReferrerClient.InstallReferrerResponse.OK -> {
                                SpUtils.getInstance().putString("ifcacheinstall", "1")
                                val response = referrerClient.installReferrer
                                Log.d("TAG", "onInstallReferrerSetupFinished: $response")
                                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())
                                ZxHttp.getHttpReportInterface("install_referrer", "", obj)
                                SpUtils.getInstance()
                                    .putString("referrer", response.installReferrer)
                                SpUtils.getInstance().putString("referrerData", "installInfo")
                                if (listOf(
                                        "gclid",
                                        "facebook",
                                        "instagram"
                                    ).all { !installInfo.contains(it, true) }
                                ) {
                                    //自然用户
                                    SpUtils.getInstance().getString("install_source", "origin")
                                } else {
                                    //渠道用户
                                    SpUtils.getInstance().getString("install_source", "channel")
                                }
                            }

                            else -> {
                                ZxHttp.getHttpReportInterface(
                                    "install_referrer_error",
                                    "",
                                    null
                                )
                            }
                        }
                        getHttpConfig()
                    } catch (_: Exception) {
                        ZxHttp.getHttpReportInterface("install_referrer_error", "", null)
                        getHttpConfig()
                    }

                }

                override fun onInstallReferrerServiceDisconnected() {
                }
            })
        } else {
            updateInstatllRefer()
        }

    }

    //去更新installreffer
    private fun updateInstatllRefer() {
        val referrerClient = InstallReferrerClient.newBuilder(ZxApplication.context).build()
        referrerClient.startConnection(object : InstallReferrerStateListener {
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                try {
                    when (responseCode) {
                        InstallReferrerClient.InstallReferrerResponse.OK -> {
                            SpUtils.getInstance().putString("ifcacheinstall", "1")
                            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())
                            SpUtils.getInstance().putString("referrer", response.installReferrer)
                            ZxHttp.getHttpReportInterface("install_referrer", "", obj)
                            if (listOf(
                                    "gclid",
                                    "facebook",
                                    "instagram"
                                ).all { !installInfo.contains(it, true) }
                            ) {
                                //自然用户
                                SpUtils.getInstance().putString("install_source", "origin")
                            } else {
                                //渠道用户
                                SpUtils.getInstance().putString("install_source", "channel")
                            }
                        }

                        else -> {
                            ZxHttp.getHttpReportInterface("install_referrer_error", "", null)
                        }
                    }
                    getHttpConfig()
                } catch (_: Exception) {
                    ZxHttp.getHttpReportInterface("install_referrer_error", "", null)
                    getHttpConfig()
                }

            }

            override fun onInstallReferrerServiceDisconnected() {

            }
        })
    }

    fun getHttpConfig() {
        ZxHttp.getHttpConfig {
            var timerStatus = SpUtils.getInstance().getInt("timerStatus", 0)
            Log.d("TAG", "timerStatus: $timerStatus")
            if (timerStatus == 1) {
                //打开定时器
                var timerDelay =
                    SpUtils.getInstance().getInt("timerDelay", 3)
                var timerInterval =
                    SpUtils.getInstance().getInt("timerInterval", 7)
                GlobalTimer.scheduleTask(
                    timerDelay *  1000L,
                    timerInterval * 1000L
                )
            }
        }
    }
}