package com.gh.gamecenter.login.utils

import com.gh.gamecenter.common.json.JsonObjectBuilder
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.loghub.LoghubUtils
import com.gh.gamecenter.common.utils.LogUtils
import com.lightgame.utils.Utils
import org.json.JSONObject

object NewLogUtils {

    private const val KEY_EVENT = "event"

    private fun log(jsonObject: JSONObject, logStore: String = "event", uploadImmediately: Boolean = false) {
        Utils.log("NewLogUtils", jsonObject.toString(4))
        LoghubUtils.log(jsonObject, logStore, uploadImmediately)
    }

    fun parseAndPutMeta(): JsonObjectBuilder.() -> Unit = {
        val meta = LogUtils.getMetaObject()
        val metaKeys = meta.keys()
        while (metaKeys.hasNext()) {
            val key: String = metaKeys.next().toString()
            val value = meta.getString(key)
            key to value
        }
    }

    /**
     * 记录实名认证结果
     * @param result 0 失败, 1 成功但未成年 2 成功且成年 3 认证中
     */
    fun logCertificationResult(isForced: Boolean, result: Int) {
        val json = json {
            KEY_EVENT to "verification_finished"
            "trigger" to if (isForced) "实名认证流程" else ""
            "result" to result
            "timestamp" to System.currentTimeMillis() / 1000
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }
}