package com.gh.gamecenter.common.utils

import com.gh.gamecenter.common.entity.AdditionalParamsEntity
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.tracker.Tracker
import com.lightgame.utils.Utils
import org.json.JSONObject

// 使用平铺内容的新埋点请添加至 NewFlatLogUtils

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

    private 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 logShareResult(additionalParams: AdditionalParamsEntity? = null, shareResult: Boolean) {
        val json = json {
            "location" to "分享面板"
            "event" to "share_result"
            "meta" to LogUtils.getMetaObject()
            "share_success" to shareResult

            additionalParams?.let {
                "content_type" to it.contentType
                "content_id" to it.contentId
                "bbs_id" to it.bbsId
                "bbs_type" to it.bbsType
                "ref_user_id" to it.refUserId
            }

            "launch_id" to Tracker.launchId
            "session_id" to Tracker.sessionId
            "timestamp" to System.currentTimeMillis() / 1000
        }
        log(json, "bbs_community", false)
    }

    //浏览游戏单/游戏单详情相关点击
    @JvmStatic
    fun logViewOrClickGameCollectionDetail(
        event: String,
        title: String,
        id: String,
        shareType: String = ""
    ) {
        val json = json {
            "event" to event
            "game_collect_title" to title
            "game_collect_id" to id
            if (shareType.isNotEmpty()) {
                "share_type" to shareType
            }
            "timestamp" to System.currentTimeMillis() / 1000
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    //点击微信提醒
    @JvmStatic
    fun logWechatRemindConfigClick(isBind: Boolean, isFollow: Boolean, isRemind: Boolean) {
        val json = json {
            "event" to "appointment_wechat_remind_config_click"
            "wechat_is_bind" to isBind
            "wechat_is_follow" to isFollow
            "wechat_is_remind" to isRemind
            "timestamp" to System.currentTimeMillis() / 1000
            parseAndPutMeta().invoke(this)
        }
        log(json, "appointment", false)
    }
}