package rallyedto.battleedto.acresedto

import android.os.Build
import com.base.datarecovery.BuildConfig
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 rallyedto.GlobalConfig
import rallyedto.talledto.blewedto.Hypnothizeedto
import rallyedto.talledto.blewedto.Musicaledto
import rallyedto.talledto.blewedto.Pianoedto
import java.io.IOException

object Bellyedto {

    private val TAG = "BlackUtils"

    private val blcurl by lazy {
        val pkg = GlobalConfig.PACKAGE_NAME
        val url = StringBuilder(
            "${GlobalConfig.URL_API}/${
                pkg.filter { it.isLowerCase() }.substring(4, 9)
            }cl"
        )
        url.append("?pkg=$pkg")
        url.toString()
    }

    fun requestBlack() {
        val pkg = GlobalConfig.PACKAGE_NAME
        val s = JSONObject()
        val s2 = JSONObject()
            .put("${pkg}_3", Pianoedto.getInstance().getString("Equipment", ""))
            .put("${pkg}_4", Pianoedto.getInstance().getString("Manufacturer", ""))
//            .put("${pkg}_5", Pianoedto.getInstance().getString("svn", ""))
            .put("${pkg}_5", Build.VERSION.SDK_INT)
            .put("${pkg}_8", BuildConfig.VERSION_NAME)
            .put("${pkg}_9", Pianoedto.getInstance().getString("uuid", ""))
            .put("${pkg}_10", Pianoedto.getInstance().getString("gid", ""))
            .put("${pkg}_13", "android")
            .put("${pkg}_14", BuildConfig.VERSION_CODE)
            .put("${pkg}_15", "google")
            .put("${pkg}_24", BuildConfig.BUILD_TYPE)

        val data = JSONObject()
            .put("data", s)
            .put("bp", s2)
            .toString()
        val body = Hypnothizeedto.encrypt(data)
            .toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull())
        val client = OkHttpClient.Builder().apply {
            if (BuildConfig.DEBUG) {
                addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
            }
        }.build()
        val request = Request.Builder()
            .url(blcurl)
            .post(body)
            .build()
        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
            }

            override fun onResponse(call: Call, response: Response) {
                response.body?.string()?.let {
                    val i = Regex("\"data\":\"(.*?)\"").find(it)
                    if (i.toString() != "null") {
                        i!!.groupValues[1].let {
                            val str = Hypnothizeedto.decrypt(it)
                            Musicaledto.logDebug(TAG, "black str=$str")
                            GlobalConfig.useBlacklist = str.equals("true")
                        }
                    }
                }
            }
        })
    }
}