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 NewFlatLogUtils {

    private const val KEY_EVENT = "event"
    private const val KEY_ENTRANCE = "entrance"
    private const val KEY_GAME_ID = "game_id"
    private const val KEY_GAME_NAME = "game_name"

    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
        }
    }

    // 进入游戏动态事件
    @JvmStatic
    fun logGameActivityEnter(source: String, countNum: Int) {
        val json = json {
            KEY_EVENT to "game_activity_enter"
            "source" to source
            "count_num" to countNum
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

    // 游戏动态信息流点击事件
    @JvmStatic
    fun logGameActivityClick(
        source: String,
        id: String?,
        idType: String?,
        sequence: Int,
        button: String,
        gameId: String?,
        gameName: String
    ) {
        val json = json {
            KEY_EVENT to "game_activity_click"
            "source" to source
            "id" to id
            "id_type" to idType
            "sequence" to sequence
            "button" to button
            KEY_GAME_ID to gameId
            KEY_GAME_NAME to gameName
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

    // 消息中心-进入消息事件
    @JvmStatic
    fun logMessageInformEnter(source: String, countNum: Int) {
        val json = json {
            KEY_EVENT to "message_inform_enter_message"
            "source" to source
            "count_num" to countNum
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

    // 消息中心-消息会话点击事件
    @JvmStatic
    fun logMessageInformClick(
        source: String,
        type: String,
        gameId: String,
        gameName: String,
        sequence: Int,
        isTop: Boolean,
        countNum: Int,
        button: String
    ) {
        val json = json {
            KEY_EVENT to "message_inform_message_click"
            "source" to source
            "message_type" to type
            KEY_GAME_ID to gameId
            KEY_GAME_NAME to gameName
            "sequence" to sequence
            "is_top" to isTop
            "count_num" to countNum
            "button" to button
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

    // 消息中心-消息一键清除事件
    @JvmStatic
    fun logMessageInformClean(sessionNum: Int, countNum: Int) {
        val json = json {
            KEY_EVENT to "message_inform_clean"
            "session_num" to sessionNum
            "count_num" to countNum
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

}