package com.gh.gamecenter.feedback.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.tracker.Tracker
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_META = "meta"
    private const val KEY_TIMESTAMP = "timestamp"
    private const val KEY_LAUNCH_ID = "launch_id"
    private const val KEY_SESSION_ID = "session_id"
    private const val KEY_CONTENT_ID = "content_id"
    private const val KEY_CONTENT_TITLE = "content_title"
    private const val KEY_HELP_ID = "help_id"
    private const val KEY_SEARCH_KEY = "search_key"

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

    //进入QA内容详情
    fun logEnterQaContent(clickLocation: String, contentId: String, contentTitle: String, helpId: String) {
        val json = json {
            KEY_EVENT to "qa_enter_content_detail"
            "click_location" to clickLocation
            KEY_CONTENT_ID to contentId
            KEY_CONTENT_TITLE to contentTitle
            KEY_HELP_ID to helpId
            KEY_META to LogUtils.getMetaObject()
            KEY_LAUNCH_ID to Tracker.launchId
            KEY_SESSION_ID to Tracker.sessionId
            KEY_TIMESTAMP to System.currentTimeMillis() / 1000
        }
        log(json)
    }

    //点击QA更多
    fun logClickQaMoreBtn(helpId: String) {
        val json = json {
            KEY_EVENT to "qa_click_title_more"
            KEY_HELP_ID to helpId
            KEY_META to LogUtils.getMetaObject()
            KEY_LAUNCH_ID to Tracker.launchId
            KEY_SESSION_ID to Tracker.sessionId
            KEY_TIMESTAMP to System.currentTimeMillis() / 1000
        }
        log(json)
    }

    //点击QA搜索结果
    fun logClickQaSearchItem(searchKey: String, contentId: String, contentTitle: String, helpId: String) {
        val json = json {
            KEY_EVENT to "qa_click_search_result"
            KEY_SEARCH_KEY to searchKey
            KEY_CONTENT_ID to contentId
            KEY_CONTENT_TITLE to contentTitle
            KEY_HELP_ID to helpId
            KEY_META to LogUtils.getMetaObject()
            KEY_LAUNCH_ID to Tracker.launchId
            KEY_SESSION_ID to Tracker.sessionId
            KEY_TIMESTAMP to System.currentTimeMillis() / 1000
        }
        log(json)
    }

    //点击QA搜索按钮
    fun logClickQaSearchBtn(searchKey: String, isEmptyResult: Boolean) {
        val json = json {
            KEY_EVENT to "qa_click_search_btn"
            KEY_SEARCH_KEY to searchKey
            "is_empty_result" to isEmptyResult
            KEY_META to LogUtils.getMetaObject()
            KEY_LAUNCH_ID to Tracker.launchId
            KEY_SESSION_ID to Tracker.sessionId
            KEY_TIMESTAMP to System.currentTimeMillis() / 1000
        }
        log(json)
    }
}