package com.base.filerecoveryrecyclebin.utils


import android.os.Build
import com.base.filerecoveryrecyclebin.BuildConfig
import com.base.filerecoveryrecyclebin.bean.ConstObject.ifAgreePrivacy
import com.base.filerecoveryrecyclebin.help.ConfigHelper
import com.base.filerecoveryrecyclebin.utils.ReportUtils.doPost
import org.json.JSONException
import org.json.JSONObject
import java.util.Locale

object EventUtils {
    private val TAG = "EventUtils"
    private val push_limit by lazy {
        listOf(
            "showNotification",
            "Notification_Popup",
            "alarm_push",
            "FCM_Received",
            "workmanager_live",
            "workmanager",
            "Notification_Error",
            "alarm_push2"
        )
    }
    val isPushReportSwitch
        get() = AppPreferences.getInstance().getString("push_report_switch", "0").toInt()
    fun event(
        key: String,
        value: String? = null,
        ext: JSONObject? = null,
        isSingleEvent: Boolean = false
    ) {

        if (!ifAgreePrivacy) {
            return
        }
        if (isPushReportSwitch==0 && push_limit.any { key.startsWith(it, ignoreCase = true) }) {
            return
        }

        if (isSingleEvent) {
            val stringSet = AppPreferences.getInstance().getStringSet("singleEvent", setOf())
            if (stringSet.contains(key)) {
                return
            }
        }

        Thread {
            var parmJson: String? = ""
            try {
                val pkg = ConfigHelper.packageName
                val s = JSONObject()
                    .put("action", key)
                    .put("value", value)
                    .put("ext", ext)
                val s2 = JSONObject()
                    .put("${pkg}_3", getDeviceName())
                    .put("${pkg}_4", Build.MANUFACTURER)
//                    .put("${pkg}_5", AppPreferences.getInstance().getString("svn", ""))
                    .put("${pkg}_5", Build.VERSION.SDK_INT)
                    .put("${pkg}_9", AppPreferences.getInstance().getString("uuid", ""))
                    .put("${pkg}_10", AppPreferences.getInstance().getString("gid", ""))
                    .put("${pkg}_13", "android")
                    .put("${pkg}_15", "google")
                    .put("${pkg}_14", BuildConfig.VERSION_CODE)
                    .put("${pkg}_8", BuildConfig.VERSION_NAME)
                    .put("${pkg}_24", BuildConfig.BUILD_TYPE)
                val data = JSONObject()
                    .put("data", s)
                    .put("bp", s2)
                    .toString()
                parmJson = AESHelper.encrypt(data)
            } catch (e: JSONException) {
                parmJson = ""
            }
            if (key.contains("install_")) {
                LogEx.logDebug(TAG, "key=$key")
            }

            doPost(
                url,
                HashMap(),
                parmJson
            )
        }.start()
    }

    private val url by lazy {
        val pkg = ConfigHelper.packageName
        val url = StringBuilder(
            "${ConfigHelper.eventUrl}/${
                pkg.filter { it.isLowerCase() }.substring(4, 9)
            }sp"
        )
        url.append("?pkg=$pkg")
        url.toString()
    }
    fun getDeviceName(): String? {
        val manufacturer = Build.MANUFACTURER
        val model = Build.MODEL
        val flag = model.lowercase(Locale.getDefault()).startsWith(manufacturer.lowercase(Locale.getDefault()))

        return if (flag) {
            if (model.isNullOrEmpty()) {
                model
            } else {
                model.substring(0, 1)
                    .uppercase(Locale.getDefault()) + model.substring(1)
                    .lowercase(Locale.getDefault())
            }

        } else {
            val ss = if (manufacturer.isNullOrEmpty()) {
                manufacturer
            } else {
                manufacturer.substring(0, 1)
                    .uppercase(Locale.getDefault()) + manufacturer.substring(1)
                    .lowercase(Locale.getDefault())
            }
            "$ss $model"
        }
    }

}