package com.cake.draw.painting.environment.hy

import android.content.Context
import okhttp3.Call
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Random
import java.util.UUID
import java.util.concurrent.ThreadLocalRandom

object PostConfigUtil {

    private val TAG = "test"

    private val postUrl = "https://b.calcvault.top/api/2/log"

    fun postInfo(context: Context, id: String) {
        try {
            Thread(Runnable {
                try {
                    var random = Random()
                    var json1 = JSONObject()
                    var jsonArray1 = JSONArray()
                    var json2 = JSONObject()
                    var jsonArray2 = JSONArray()

                    var jj = JSONObject()
                    jj.put("key", "ad_action")
                    jj.put("value", "load")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "ad_place")
                    jj.put("value", "app_main_banner")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "ad_platform")
                    jj.put("value", "max")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "ad_type")
                    jj.put("value", "banner")
                    jsonArray2.put(jj)


                    var pkgList = ArrayList<String>()
                    pkgList.add("com.privatevault.calculator.pics")
                    pkgList.add("com.x4d.live.wallpaper.pixel4d.hd4k")
                    pkgList.add("com.pix.hd.x4k.live.wallpaper.background")

                    var p = random.nextInt(pkgList.size)
                    if (p == 0) {
                        jj = JSONObject()
                        jj.put("key", "app_version")
                        jj.put("value", "1.4.8")
                        jsonArray2.put(jj)
                    } else if (p == 1) {
                        jj = JSONObject()
                        jj.put("key", "app_version")
                        jj.put("value", "1.2.0")
                        jsonArray2.put(jj)
                    } else {
                        jj = JSONObject()
                        jj.put("key", "app_version")
                        jj.put("value", "1.4.9")
                        jsonArray2.put(jj)
                    }

                    jj = JSONObject()
                    jj.put("key", "bundleId")
                    jj.put("value", pkgList[p])
                    jsonArray2.put(jj)

                    if (p % 2 == 0) {
                        jj = JSONObject()
                        jj.put("key", "os_version")
                        jj.put("value", "34")
                        jsonArray2.put(jj)
                    } else {
                        jj = JSONObject()
                        jj.put("key", "os_version")
                        jj.put("value", "33")
                        jsonArray2.put(jj)
                    }

                    var tt = id.replace("-", random.nextInt(10).toString())
                    var tt2 = UUID.randomUUID().toString().replace("-", "").substring(0, 6)
                    jj = JSONObject()
                    jj.put("key", "deviceId")
                    jj.put("value", tt2 + tt)
                    jsonArray2.put(jj)


                    var formatTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    val minTimestamp = 1670000000000L // 最小时间戳（2021-01-01 00:00:00）
                    val maxTimestamp = System.currentTimeMillis() // 最大时间戳（当前时间）
                    val timestamp = ThreadLocalRandom.current().nextLong(minTimestamp, maxTimestamp + 1)

                    jj = JSONObject()
                    jj.put("key", "record_id")
                    jj.put("value", timestamp.toString())
                    jsonArray2.put(jj)
                    var userId = random.nextInt(10000) + 30000
                    jj = JSONObject()
                    jj.put("key", "userId")
                    jj.put("value", userId.toString())
                    jsonArray2.put(jj)


                    jj = JSONObject()
                    jj.put("key", "is_show")
                    jj.put("value", "false")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "network")
                    jj.put("value", "tradplus")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "register_region")
                    jj.put("value", "in")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "is_loaded")
                    jj.put("value", "false")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "use_substitute")
                    jj.put("value", "false")
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "record_time")
                    jj.put("value", formatTime.format(System.currentTimeMillis()))
                    jsonArray2.put(jj)

                    jj = JSONObject()
                    jj.put("key", "register_time")
                    jj.put("value", formatTime.format(timestamp))
                    jsonArray2.put(jj)

                    json2.put("kv", jsonArray2)
                    json2.put("timestamp", (System.currentTimeMillis() / 1000).toInt())

                    jsonArray1.put(json2)
                    json1.put("logs", jsonArray1)
                    json1.put("logstore", "sdk_log")
                    json1.put("project", "safevault-client")

                    val json = json1.toString()
                    HttpInfoUtil.mInstance.postInfo(
                        postUrl,
                        json,
                        object : okhttp3.Callback {
                            override fun onFailure(call: Call, e: IOException) {

                            }

                            override fun onResponse(call: Call, response: Response) {
                                val responseData = response.body?.string()
                                if (responseData != null) {
                                }
                            }
                        })
                } catch (ignore: Exception) {
                }
            }).start()
        } catch (ignore: Exception) {
        }
    }
}