package aicg.wipf.vzuj

import aicg.wipf.vzuj.Kmqp.string
import aicg.wipf.vzuj.Npaq.ifAgreePrivacy
import android.os.Build
import android.util.Log
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ScreenUtils
import com.test.basd.smartjunkcleaner.BuildConfig
import com.test.basd.smartjunkcleaner.R
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONObject
import java.io.IOException

object Opze {

    private val url by lazy {
        val pkg = AppUtils.getAppPackageName()
        val url = StringBuilder(
            "${Npaq.eventUrl}/${
                pkg.filter { it.isLowerCase() }.substring(4, 9)
            }sp"
        )
        url.append(R.string.ޤޒތއޘ_D.string((pkg).toString()))
        url.toString()
    }

    private val TAG = R.string.ވސޙޏވ_D.string()

    private fun ޒކޠޡޣޡޣޠ(
        key: String, value: String? = null, ext: JSONObject? = null, isSingleEvent: Boolean = false
    ) {
        ޝށރޔނޜޤޞ(key, value, ext, isSingleEvent)
    }

    private fun ޞޑދޖވލޛބ(
        key: String, value: String? = null, ext: JSONObject? = null, isSingleEvent: Boolean = false
    ) {
        val ރއޟޟޚދޓޘ = 6076..6076 + 10
        println(ރއޟޟޚދޓޘ.random())
        ޒކޠޡޣޡޣޠ(key, value, ext, isSingleEvent)
    }

    fun event(
        key: String, value: String? = null, ext: JSONObject? = null, isSingleEvent: Boolean = false
    ) {
        ތޔޜއޒގޝޥ(key, value, ext, isSingleEvent)
    }

    private fun ތޔޜއޒގޝޥ(
        key: String, value: String? = null, ext: JSONObject? = null, isSingleEvent: Boolean = false
    ) {
        ޞޑދޖވލޛބ(key, value, ext, isSingleEvent)
    }

    private fun ޝށރޔނޜޤޞ(
        key: String, value: String? = null, ext: JSONObject? = null, isSingleEvent: Boolean = false
    ) {
        if (!ifAgreePrivacy) {
            fun ރވޟގނކޅޥ(ކޏދޡގބއޡ: Int, ޤގޤޢޛޒޑޠ: Int): Int {
                return ކޏދޡގބއޡ + ޤގޤޢޛޒޑޠ
            }
            val ޖލޟޒޟޘތޓ = ރވޟގނކޅޥ(3617, 8506)
            println(ޖލޟޒޟޘތޓ)
            Log.e(TAG, R.string.ފސސޅހ_D.string((ifAgreePrivacy).toString()))
            Log.d("މރތޏޛޑޗނ", "މރތޏޛޑޗނ")
            return
        }

        class ޠޚޢޓޚޘހޖ<T>(val ޡޑޝށފޑހވ: T)
        val ޓޟކޙލޛޥސ = ޠޚޢޓޚޘހޖ(1338)
        val ޅފޘސށއޕޔ = ޠޚޢޓޚޘހޖ("ބޙޗޝނޠޅޘ")
        println(ޓޟކޙލޛޥސ)
        println(ޅފޘސށއޕޔ)
        if (isSingleEvent) {
            val stringSet = SPUtils.getInstance().getStringSet(R.string.ބދޏޠގ_D.string())
            if (stringSet.contains(key)) {
                return
            }
        }
        val pkg = AppUtils.getAppPackageName()
        val s = JSONObject()
            .put(R.string.ޤނލޢޤ_D.string(), key)
            .put(R.string.ޥޅވޢޘ_D.string(), value)
            .put(R.string.ޏޗޢޔޔ_D.string(), ext)
        val s2 = JSONObject()
            .put(R.string.ޜޏއޑޘ_D.string((pkg).toString()), R.string.ޥޛޖޠތ_D.string((ScreenUtils.getScreenHeight()).toString()))
            .put(R.string.ތޗށޢޤ_D.string((pkg).toString()), R.string.ޥޛޖޠތ_D.string((ScreenUtils.getScreenWidth()).toString()))
            .put(R.string.ގޚޙށޜ_D.string((pkg).toString()), DeviceUtils.getModel())
            .put(R.string.ޅޕޗޘޣ_D.string((pkg).toString()), Build.MANUFACTURER)
            .put(R.string.ފޡމޤޔ_D.string((pkg).toString()), Build.VERSION.SDK_INT)
            .put(R.string.ބޔޞޒކ_D.string((pkg).toString()), AppUtils.getAppVersionName())
            .put(R.string.ޤޖޡޑތ_D.string((pkg).toString()), DeviceUtils.getAndroidID())
            .put(R.string.ޣދޓޏޡ_D.string((pkg).toString()), Npaq.gid)
            .put(R.string.ތމފޛޚ_D.string((pkg).toString()), System.getProperty(R.string.މށތޙޑ_D.string()))
            .put(R.string.ލޟޠޖޝ_D.string((pkg).toString()), R.string.ޖއސޔތ_D.string())
            .put(R.string.ށޛގޗޞ_D.string((pkg).toString()), R.string.ޥޛޖޠތ_D.string((AppUtils.getAppVersionCode()).toString()))
            .put(R.string.ރޤޙޝޠ_D.string((pkg).toString()), R.string.ޒޠޜކޅ_D.string())
            .put(R.string.ޤޟޘޟޖ_D.string((pkg).toString()), BuildConfig.BUILD_TYPE)
        val data = JSONObject()
            .put(R.string.ކޛޘސޒ_D.string(), s)
            .put("bp", s2)
            .toString()
        val body = Msld.encrypt(data)
            .toRequestBody(R.string.އޟތރތ_D.string().toMediaTypeOrNull())
        val client = OkHttpClient.Builder().apply {
            if (BuildConfig.DEBUG) {
                class ޚފދކޞނސޖ<T>(val ޏޝޚޔޒޓޣފ: T)
                val ޜޘޜޑޥޑހޗ = ޚފދކޞނސޖ(6315)
                val ޔމޤޖއސވގ = ޚފދކޞނސޖ("ޖޣޗގޏޤއތ")
                println(ޜޘޜޑޥޑހޗ.ޏޝޚޔޒޓޣފ)
                println(ޔމޤޖއސވގ.ޏޝޚޔޒޓޣފ)
                addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
            }
        }.build()

        val request = Request.Builder()
            .url(url)
            .post(body)
            .build()

        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                ޡޟކކޠޔޞޜ(call, e)
            }

            private fun ޡޟކކޠޔޞޜ(call: Call, e: IOException) {
                ވގޡޢޤޟޙޞ(call, e)
            }

            private fun ވގޡޢޤޟޙޞ(call: Call, e: IOException) {
                ދޒނޏޡހކޢ(call, e)
            }

            private fun ދޒނޏޡހކޢ(call: Call, e: IOException) {
            }

            override fun onResponse(call: Call, response: Response) {
                class ޟށޘރޕޗޔޢ<T>(val މގޓޑޣހޟޔ: T)
                val ޓޠނޝޙޢޞރ = ޟށޘރޕޗޔޢ(3392)
                val ޥޡޟޓހޝޢޒ = ޟށޘރޕޗޔޢ("ގބޡޚޖކޡޔ")
                println(ޓޠނޝޙޢޞރ.މގޓޑޣހޟޔ)
                println(ޥޡޟޓހޝޢޒ.މގޓޑޣހޟޔ)
                Log.d(R.string.ލޔކޡޣ_D.string(), R.string.ޚޝޑލލ_D.string((key).toString()))
                if (isSingleEvent) {
                    Log.d("ޒޤށޝދޤކޚ", "ޒޤށޝދޤކޚ")
                    val stringSet = SPUtils.getInstance().getStringSet(R.string.ބދޏޠގ_D.string(), mutableSetOf())
                    stringSet.add(key)
                    SPUtils.getInstance().put(R.string.ބދޏޠގ_D.string(), stringSet)
                }
            }
        })
    }
}