package com.gh.common.util

import com.gh.gamecenter.common.loghub.LoghubUtils.log
import com.gh.gamecenter.common.tracker.Tracker.launchId
import com.gh.gamecenter.common.tracker.Tracker.sessionId
import com.gh.gamecenter.common.utils.debugOnly
import com.gh.gamecenter.common.utils.tryWithDefaultCatch
import com.lightgame.utils.Utils
import org.json.JSONObject

object IntegralLogHelper {

    fun log(event: String, location: String) = log(event, location, null)

    fun log(event: String, location: String, entrance: String? = null) {
        val json = JSONObject().apply {
            tryWithDefaultCatch {
                put("meta", LogUtils.getMetaObject())
                put("event", event)
                put("location", location)
                put("timestamp", System.currentTimeMillis() / 1000)
                put("launch_id", launchId)
                put("session_id", sessionId)
                entrance?.let {
                    put("entrance", it)
                }
            }
        }

        debugOnly {
            Utils.log("LogUtils->$json")
        }
        log(json, "score", false)
    }

    fun logTask(event: String, location: String, jobId: String, jobName: String, jobType: String) {
        val json = JSONObject().apply {
            tryWithDefaultCatch {
                put("meta", LogUtils.getMetaObject())
                put("event", event)
                put("location", location)
                put("timestamp", System.currentTimeMillis() / 1000)
                put("launch_id", launchId)
                put("session_id", sessionId)
                put("job_id", jobId)
                put("job_name", jobName)
                put("job_type", jobType)
            }
        }

        debugOnly {
            Utils.log("LogUtils->$json")
        }
        log(json, "score", false)
    }

    fun logCommodityCategory(
        event: String,
        location: String,
        entrance: String,
        categoryId: String,
        categoryName: String
    ) {
        val json = JSONObject().apply {
            tryWithDefaultCatch {
                put("meta", LogUtils.getMetaObject())
                put("event", event)
                put("location", location)
                put("timestamp", System.currentTimeMillis() / 1000)
                put("launch_id", launchId)
                put("session_id", sessionId)
                put("entrance", entrance)
                put("goods_species_id", categoryId)
                put("goods_species_name", categoryName)
            }
        }

        debugOnly {
            Utils.log("LogUtils->$json")
        }
        log(json, "score", false)
    }

    fun logCommodity(event: String, location: String, commodityId: String, categoryId: String, categoryName: String) {
        val json = JSONObject().apply {
            tryWithDefaultCatch {
                put("meta", LogUtils.getMetaObject())
                put("event", event)
                put("location", location)
                put("timestamp", System.currentTimeMillis() / 1000)
                put("launch_id", launchId)
                put("session_id", sessionId)
                put("goods_id", commodityId)
                put("goods_species_id", categoryId)
                put("goods_species_name", categoryName)
            }
        }

        debugOnly {
            Utils.log("LogUtils->$json")
        }
        log(json, "score", false)
    }

    fun logEnergyRange(event: String, location: String, range: String) {
        val json = JSONObject().apply {
            tryWithDefaultCatch {
                put("meta", LogUtils.getMetaObject())
                put("event", event)
                put("location", location)
                put("timestamp", System.currentTimeMillis() / 1000)
                put("launch_id", launchId)
                put("session_id", sessionId)
                put("energy_range", range)
            }
        }

        debugOnly {
            Utils.log("LogUtils->$json")
        }
        log(json, "score", false)
    }

    fun logPendent(event: String, location: String, pendentId: String, pendentType: String? = null) {
        val json = JSONObject().apply {
            tryWithDefaultCatch {
                put("meta", LogUtils.getMetaObject())
                put("event", event)
                put("location", location)
                put("timestamp", System.currentTimeMillis() / 1000)
                put("launch_id", launchId)
                put("session_id", sessionId)
                put("pendant_id", pendentId)
                pendentType?.run {
                    put("pendant_type", this)
                }
            }
        }

        debugOnly {
            Utils.log("LogUtils->$json")
        }
        log(json, "score", false)
    }

    fun logInviteResult(result: String, type: String? = null) {
        val json = JSONObject().apply {
            tryWithDefaultCatch {
                put("meta", LogUtils.getMetaObject())
                put("event", "invite_result")
                put("location", "邀请途径面板")
                put("timestamp", System.currentTimeMillis() / 1000)
                put("launch_id", launchId)
                put("session_id", sessionId)
                put("invitation_result", result)
                put("invitation_type", type)
            }
        }

        debugOnly {
            Utils.log("LogUtils->$json")
        }
        log(json, "score", false)
    }
}