package rowedto.jockeyedto.sheetedto.raceedto.pavementedto

import android.os.Build
import com.base.datarecovery.BuildConfig
import org.json.JSONException
import org.json.JSONObject
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.ifAgreePrivacy
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Celledto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dotedto.doPost
import java.util.Locale
import java.util.TimeZone

object Slantedto {
    private val TAG = "EventUtils"
    fun event(
        key: String,
        value: String? = null,
        ext: JSONObject? = null,
        isSingleEvent: Boolean = false
    ) {
        if (!ifAgreePrivacy) {
            return
        }

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

        Thread {
            var paramJson: String? = ""
            try {
                val pkg = Celledto.packageName
                val timeZone: TimeZone = TimeZone.getDefault()
                val timeZoneId: String = timeZone.id
                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", Build.VERSION.SDK_INT)
                    .put("${pkg}_8", BuildConfig.VERSION_NAME)
                    .put("${pkg}_9", Stewardedto.getInstance().getString("uuid", ""))
                    .put("${pkg}_10", Stewardedto.getInstance().getString("gid", ""))
                    .put("${pkg}_13", "android")
                    .put("${pkg}_15", "google")
                    .put("${pkg}_14", BuildConfig.VERSION_CODE)
                    .put("${pkg}_24", BuildConfig.BUILD_TYPE)
                    .put("${pkg}_34", timeZoneId)
                val data = JSONObject()
                    .put("data", s)
                    .put("bp", s2)
                    .toString()
                Dishedto.logDebug(TAG, "data=${Stewardedto.getInstance().getString("uuid", "")}")
                paramJson = Broadcastedto.encrypt(data)
            } catch (e: JSONException) {
                paramJson = ""
            }
            Dishedto.logDebug(TAG, "report!")
            doPost(
                url,
                HashMap(),
                paramJson
            )
        }.start()
    }

    private val url by lazy {
        val pkg = Celledto.packageName
        val url = StringBuilder(
            "${Celledto.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"
        }
    }
}