package com.gh.gamecenter.common.utils

import com.gh.gamecenter.common.json.JsonObjectBuilder
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.loghub.LoghubUtils
import com.lightgame.utils.Utils
import org.json.JSONObject

object NewFlagLogUtils {

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

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

    /**
     * 记录外部存储权限提示弹窗出现
     */
    fun logExternalStoragePermissionDialogAppearance() {
        val json = json {
            "event" to "storage_permission_pop_show"
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    /**
     * 记录外部存储权限弹窗行为
     */
    fun logExternalStoragePermissionDialogAction(action: String) {
        val json = json {
            "event" to "storage_permission_pop_click"
            "button" to action
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

}