package com.ccwangluo.accelerator.utils.datareport

import com.ccwangluo.accelerator.utils.SysUtils
import com.ccwangluo.accelerator.utils.http.HttpGo
import com.google.gson.Gson
import com.google.gson.JsonParser

/**
 * 日志上报
 */
object DataRePortUtils {

    val list = mutableSetOf<BaseData>()
    val gson = Gson()

    fun report(eventName: String, param: Map<String, Any>) {
        report(eventName, gson.toJson(param).toString())
    }

    fun report(eventName: String, dataInfo: String?) {
        val data = BaseData()
        dataInfo?.let {
            data.data = JsonParser().parse(it).asJsonObject
        }
        data.event = eventName
        list.add(data)
        if (SysUtils.getConnectivityStatus()) {
            var set = mutableSetOf<BaseData>()
            set.addAll(list)
            list.clear()
            HttpGo.postDataASync("https://d-tt-agent.srccwl.com/data/batch", set) {
                if (!it) {
                    list.addAll(set)
                }
            }
        }
    }

    fun report(eventName: String) {
        report(eventName, null)
    }
}