package com.gh.gamecenter.feature.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 NewFlatLogUtils {

    private const val KEY_EVENT = "event"
    private const val KEY_GAME_ID = "game_id"
    private const val KEY_GAME_NAME = "game_name"
    private const val KEY_TEXT = "text"
    private const val KEY_ERROR = "error"
    private const val KEY_INIT_SDK_FUN = "init_sdk_fun"

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

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

    //关注tab关注游戏 我的游戏-关注 ，点击热门游戏推荐时上报
    @JvmStatic
    fun logMyGameFollowTabHotGameClick(content: String, gameId: String, gameName: String) {
        val json = json {
            KEY_EVENT to "game_self_follow_hot_recommend_click"
            KEY_TEXT to content
            KEY_GAME_ID to gameId
            KEY_GAME_NAME to gameName
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

    @JvmStatic
    fun logQGameClick(qqGameId: String, qqGameName: String?) {
        val json = json {
            KEY_EVENT to "qq_game_click"
            "qq_game_id" to qqGameId
            "qq_game_name" to (qqGameName ?: "")
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

    @JvmStatic
    fun logAcceleratorSetTokenError(initResults: List<String>, error: String) {
        json {
            KEY_EVENT to "accelerator_set_token_error"
            KEY_ERROR to error
            KEY_INIT_SDK_FUN to initResults.toString()
            parseAndPutMeta().invoke(this)
        }.let(::log)
    }
}