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

    private const val KEY_EVENT = "event"

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

    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 logHaloSelfNightModeOsSwitch(isOn: Boolean) {
        val json = json {
            KEY_EVENT to "halo_self_night_mode_os_switch"
            "is_on" to isOn
            parseAndPutMeta().invoke(this)
        }
        log(json)
    }

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

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

    // 切换深色模式
    fun logNightModeSwitch(isAutoSwitch: Boolean, isNightMode: Boolean) {
        val json = json {
            KEY_EVENT to "night_mode_switch"
            "is_auto_switch" to isAutoSwitch
            "is_night_mode" to isNightMode
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }
}