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

    /**
     * 埋点序号：112
     * 事件ID：plugin_install_pop_show
     * 事件名称：插件化安装弹窗展示事件
     * 触发时机：安装插件包触发插件化安装弹窗时上报
     * @param gameId 触发弹窗的游戏ID
     * @param gameName 触发弹窗的游戏名称
     * @param platform 触发弹窗的游戏平台
     */
    fun logPluginInstallPopShow(
        gameId: String,
        gameName: String,
        platform: String
    ) {
        val json = json {
            KEY_EVENT to "plugin_install_pop_show"
            "game_name" to gameName
            "game_id" to gameId
            "platform" to platform
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    /**
     * 埋点序号：113
     * 事件ID：plugin_install_pop_click
     * 事件名称：插件化安装弹窗点击事件
     * 触发时机：点击插件化安装弹窗时上报
     * @param gameId 触发弹窗的游戏ID
     * @param gameName 触发弹窗的游戏名称
     * @param platform 触发弹窗的游戏平台
     * @param buttonName 点击内容，关闭弹窗/取消/开始XX化
     */
    fun logPluginInstallPopClick(
        gameId: String,
        gameName: String,
        platform: String,
        buttonName: String,
    ) {
        val json = json {
            KEY_EVENT to "plugin_install_pop_click"
            "game_name" to gameName
            "game_id" to gameId
            "platform" to platform
            "button_name" to buttonName
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    /**
     * 埋点序号：114
     * 事件ID：game_uninstall_complete
     * 事件名称：游戏卸载完成事件
     * 触发时机：完成卸载游戏时上报
     * @param gameId 游戏ID
     * @param gameName 游戏名称
     */
    fun logGameUninstallComplete(
        gameId: String,
        gameName: String
    ) {
        val json = json {
            KEY_EVENT to "game_uninstall_complete"
            "game_name" to gameName
            "game_id" to gameId
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    /**
     * 埋点序号：115
     * 事件ID：game_install
     * 事件名称：游戏安装点击事件
     * 触发时机：下载完成后点击安装或自动触发安装游戏时上报
     * @param gameId 游戏ID
     * @param gameName 游戏名称
     * @param trigger 触发类型，主动安装/自动安装
     */
    fun logGameInstall(
        gameId: String,
        gameName: String,
        trigger: String
    ) {
        val json = json {
            KEY_EVENT to "game_install"
            "game_name" to gameName
            "game_id" to gameId
            "trigger" to trigger
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    /**
     * 埋点序号：116
     * 事件ID：game_install_complete
     * 事件名称：游戏安装完成事件
     * 触发时机：完成安装游戏时上报
     * @param gameId 游戏ID
     * @param gameName 游戏名称
     */
    fun logGameInstallComplete(
        gameId: String,
        gameName: String
    ) {
        val json = json {
            KEY_EVENT to "game_install_complete"
            "game_name" to gameName
            "game_id" to gameId
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }
}