package pxds.lmmd.zmwf

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.supercleanermax.BuildConfig
import com.test.basd.supercleanermax.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 pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Uawv.ifAgreePrivacy
import java.io.IOException

object Wspd {

    private val url by lazy {
        val pkg = AppUtils.getAppPackageName()
        val url = StringBuilder(
            "${Uawv.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()

    fun event(
        key: String, value: String? = null, ext: JSONObject? = null, isSingleEvent: Boolean = false
    ) {
        if (!ifAgreePrivacy) {

            return
        }
        val filterKey = arrayOf(R.string.ޤޣސޜޘ_D.string(), R.string.ދޝފމޡ_D.string(), R.string.ގޞލޜޡ_D.string(), R.string.ޞޗޟވވ_D.string(), R.string.ކޟފޞއ_D.string())
        if (!filterKey.contains(key)) {
            return
        }

        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()), Uawv.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 = Rdkj.encrypt(data)
            .toRequestBody(R.string.ޘކޛޡޟ_D.string().toMediaTypeOrNull())
        val client = OkHttpClient.Builder().apply {
            if (BuildConfig.DEBUG) {
                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) {
            }

            override fun onResponse(call: Call, response: Response) {
                Log.d(R.string.ޔކނޝއ_D.string(), R.string.ޟޤއވލ_D.string((key).toString()))
                if (isSingleEvent) {
                    val stringSet = SPUtils.getInstance().getStringSet(R.string.ޤވޓޘޟ_D.string(), mutableSetOf())
                    stringSet.add(key)
                    SPUtils.getInstance().put(R.string.ޤވޓޘޟ_D.string(), stringSet)
                }
            }
        })
    }
}