package com.gh.gamecenter.common.loghub

import com.lightgame.utils.Utils
import com.volcengine.model.tls.LogItem
import com.volcengine.model.tls.producer.CallBack
import com.volcengine.model.tls.producer.ProducerConfig
import com.volcengine.service.tls.Producer
import com.volcengine.service.tls.ProducerImpl

object TLogHubHelper {

    private const val TAG = "TLogHubHelper"

    // 阿里云 logstore to 火山云 topicId 的 mapping
    private val topicIdMapping = hashMapOf(
        "appointment" to "2429e0ac-ee8b-4d2b-bab6-351d610f12df",
        "bbs_community" to "6b3b32b9-6f04-4279-8117-a5f938ef7ebd",
        "collection" to "77d6bd2b-c551-43d9-b36d-ae3d46b0fdbb",
        "common" to "7fd7ef5e-bfa6-47e5-adc3-0bcb1f8d57bc",
        "community" to "d8c296c9-ceb2-470f-8f3c-4c6e4e2f64d1",
        "download_debug" to "a48acc1b-c863-459a-8681-99e25a63d19b",
        "empty" to "30cf8475-81c0-42b7-8b81-19c9a731a644",
        "event" to "ab6d32f5-e76f-4ebe-b484-2b456ffddfd8",
        "exposure" to "3612f0f5-a5d4-4db8-91aa-8ec950107e7a",
        "genshin" to "13d82937-45f2-4ee1-bd39-c17a63c570d5",
        "halo-api-device-installed" to "c2a34712-8d43-4352-a3aa-7d3717b27a22",
        "launch_activity" to "607dd48c-fbf9-44dc-8ebb-496bdea55447",
        "score" to "af580973-ffef-4cd2-b010-f2589bc29201",
        "video_streaming" to "d5efdf24-fa48-4e31-aeb6-8d961c6de51f",
    )

    private val producer: Producer by lazy {
        ProducerImpl(
            ProducerConfig(
                "https://tls-cn-guangzhou.volces.com",
                "cn-guangzhou",
                "AKLTYmUwZTI0ZWM4Y2UwNDE5ZGEwYzM0YTcyMDFlZjI4NzE",
                "TmpGa05EWTNOVGhpWkRSaU5EazVabUV4WmpVMVlUUTBOemswWkRGbE5qZw==",
                ""
            ).apply {
                maxThreadCount = 5
            }
        ).also {
            it.start()
        }
    }

    fun sendLog(log: LogItem, logStore: String) {
        // 如果不需要回调，callback参数传null即可
        val callBack = CallBack { result -> Utils.log(TAG, "sendLog result ${result.isSuccess}, " + "$logStore -> $log") }
        val topicId = topicIdMapping[logStore] ?: return

        Utils.log(TAG, "sendLog, $logStore -> $topicId -> $log")

        producer.sendLogV2("", topicId, "android", "logFile", log, callBack)
    }

}