package com.gh.gamecenter.message.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 const val KEY_GAME_ID = "game_id"
    private const val KEY_CONTENT_ID = "content_id"

    private const val LOG_STORE_EVENT = "event"

    private fun log(jsonObject: JSONObject, logStore: String, 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
        }
    }

    //点击消息中心-头部图标/赞同/系统图标
    @JvmStatic
    fun logMessageInformTopIconClick(
        isInform: Boolean,
        displayType: String,
    ) {
        val json = json {
            KEY_EVENT to "message_inform_top_icon_click"
            "is_inform" to isInform
            "display_type" to displayType
            parseAndPutMeta().invoke(this)
        }
        log(json, LOG_STORE_EVENT)
    }

    //点击消息通知
    @JvmStatic
    fun logMessageInformClick(
        contentId: String,
        newsId: String,
        gameId: String,
        gameCollectionId: String,
        sessionGameId: String,
        sessionGameName: String,
        sessionMessageType: String,
        messageType: String
    ) {
        val json = json {
            KEY_EVENT to "message_inform_click"
            KEY_CONTENT_ID to contentId
            "news_id" to newsId
            KEY_GAME_ID to gameId
            "game_collect_id" to gameCollectionId
            "session_game_id" to sessionGameId
            "session_game_name" to sessionGameName
            "session_message_type" to sessionMessageType
            "message_type" to messageType
            parseAndPutMeta().invoke(this)
        }
        log(json, LOG_STORE_EVENT)
    }

    //点击消息通知信息用户头像/昵称
    @JvmStatic
    fun logMessageInformUserClick(event: String) {
        val json = json {
            KEY_EVENT to event
            parseAndPutMeta().invoke(this)
        }
        log(json, LOG_STORE_EVENT)
    }
}