package com.gh.gamecenter.login.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.utils.LogUtils
import com.lightgame.utils.Utils
import org.json.JSONObject

object NewFlatLogUtils {

    private const val KEY_EVENT = "event"

    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.getNewMetaObject()
        val metaKeys = meta.keys()
        while (metaKeys.hasNext()) {
            val key: String = metaKeys.next().toString()
            val value = meta.getString(key)
            key to value
        }
    }

    // 一键登录界面展示/手机号码验证界面展示/隐私政策指引弹窗展示
    fun logFastLoginRelatedShow(event: String) {
        val json = json {
            KEY_EVENT to event
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    // 一键登录界面登录方式选择(`一键登录`\`QQ`\`微信`\`微博`)
    fun logFastLoginChoice(loginType: String) {
        val json = json {
            KEY_EVENT to "fast_login_choice"
            "login_type" to loginType
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    // 手机号码验证界面登录(``一键登录` \ `立即登录` \ `我的光环相关功能按钮` \ `社区内容发布按钮` \ `社区Tab点赞功能` \
    // `游戏评价点赞功能` \ `视频Tab点赞功能` \ `社区内容评论/回复` \ `视频流评论/回复` \ `游戏评价/回复``)
    fun logMobileNumberClick(loginSource: String) {
        val json = json {
            KEY_EVENT to "mobile_number_verification_click"
            "login_source" to loginSource
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    // 手机号码验证界面登录(`同意并继续`\`不同意`)
    fun logLoginPrivacyPolicyPopClick(buttonType: String) {
        val json = json {
            KEY_EVENT to "login_privacy_policy_pop_click"
            "button_type" to buttonType
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }

    // 一键登录-登录效果(`成功`\`失败`)
    fun logFastLoginResult(loginResult: String) {
        val json = json {
            KEY_EVENT to "fast_login_result"
            "button_type" to loginResult
            parseAndPutMeta().invoke(this)
        }
        log(json, "event", false)
    }
}