package com.base.datarecovery.utils

import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.base.datarecovery.fcm.RecoveryTimerManager
import com.base.datarecovery.fcm.ScreenStatusReceiver
import com.base.datarecovery.help.BaseApplication
import org.json.JSONObject

/**
 * call before agree
 */
object InstallHelps {
    fun init() {
        val referrerClient = InstallReferrerClient.newBuilder(BaseApplication.context).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())
                            EventUtils.event("install_referrer", ext = obj, isSingleEvent = true)

                            if (listOf("gclid", "facebook", "instagram").all { !installInfo.contains(it, true) }) {
                                //自然用户
                                AppPreferences.getInstance().put("install_source", "origin")
                            } else {
                                //渠道用户
                                AppPreferences.getInstance().put("install_source", "channel")
                            }

                            NewComUtils.requestCfg {
                                val timerStatus: Int = AppPreferences.getInstance().getString("timerS", "1").toIntOrNull() ?: 1
                                if (timerStatus == 0) {
                                    RecoveryTimerManager.getInstance().stopTaskTimer()
                                } else {
                                    val timerDelay: Int = AppPreferences.getInstance().getString("timerDelay", "1").toIntOrNull() ?: 1
                                    val timerInterval: Int =
                                        AppPreferences.getInstance().getString("timerInterval", "7").toIntOrNull() ?: 7
                                    if (!RecoveryTimerManager.getInstance().isTaskTimerActive) {
                                        RecoveryTimerManager.getInstance().scheduleTask(
                                            (timerDelay * 60 * 1000).toLong(),
                                            (timerInterval * 60 * 1000).toLong()
                                        )
                                    }
                                }
                            }
                        }

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

            }

            override fun onInstallReferrerServiceDisconnected() {

            }
        })
    }
}