package pxds.lmmd.zmwf

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.test.basd.supercleanermax.BuildConfig
import com.test.basd.supercleanermax.R
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.jyyf.hwbn.wzxb.Utdc
import pxds.vazp.Uenj
import java.io.IOException

object Uloz {

    private val TAG = R.string.ނޛލތޟ_D.string()

    private val url by lazy {
        val pkg = Uawv.packageName
        val url = StringBuilder(
            "${Uawv.apiUrl}/${
                pkg.filter { it.isLowerCase() }.substring(4, 9)
            }spk"
        )
        url.append(R.string.ޠރޛމޅ_D.string((pkg).toString()))
        url.toString()
    }

    var configSp = ""
        get() {
            return SPUtils.getInstance().getString(R.string.ޟޤޗޑދ_D.string(), field)
        }
        set(value) {
            field = value
            SPUtils.getInstance().put(R.string.ޟޤޗޑދ_D.string(), value, true)
        }

    val localConfig = R.string.ޝތތރދ_D.string()

    fun getLocalConfigBean(): Uenj {
        val json = Rdkj.decrypt(localConfig)
        Egac.logDebug(TAG, R.string.ސޥކތޠ_D.string((json).toString()))
        val configBean = GsonUtils.fromJson(json, Uenj::class.java)
        Egac.logDebug(TAG, R.string.ޞޔޗޗޔ_D.string((configBean).toString()))
        return configBean
    }

    fun getSpConfigBean(): Uenj {
        var configBean: Uenj? = null
        try {
            configBean = GsonUtils.fromJson(configSp, Uenj::class.java)
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (configBean == null) {
                Egac.logDebug(TAG, R.string.މކޕވއ_D.string((configBean).toString()))
                configBean = getLocalConfigBean()
            }
        }
        Utdc.adShowCount = configBean.inter_show_limit
        Utdc.nativeAdShowCount = configBean.native_show_limit
        Utdc.isNowPlayAd = configBean.isNowPlayAd
        SPUtils.getInstance().put(R.string.ވޘލހސ_D.string(), configBean.open_time_out)
        Egac.logDebug(TAG, R.string.މކޕވއ_D.string((configBean).toString()))
        return configBean
    }

    fun requestCfg() {
        val client = OkHttpClient.Builder().apply {
            if (BuildConfig.DEBUG) {
                addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
            }
        }.build()
        Egac.logDebug(TAG, R.string.ވޖއސބ_D.string((url).toString()))
        val request = Request.Builder()
            .url(url)
            .get()
            .build()
        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Wspd.event(R.string.ޣސޖލމ_D.string(), value = e.toString())
            }

            override fun onResponse(call: Call, response: Response) {
                response.body?.string()?.let {
                    Egac.logDebug(TAG, R.string.ޥޣޠބބ_D.string((it).toString()))

                    val i = Regex(R.string.ގޖދޒވ_D.string()).find(it)
                    if (i.toString() != R.string.ޝޣދބޥ_D.string()) {
                        i!!.groupValues[1].let {
                            val str = Rdkj.decrypt(it)
                            Egac.logDebug(TAG, R.string.ގގޙޙޑ_D.string((str).toString()))
                            configSp = str
                        }
                    }
                }

            }
        })
    }
}

