package com.ydl.ydlcommon.utils.log

import android.util.Log
import com.aliyun.sls.android.producer.*

class AliYunLogHelper {

    companion object {
        var mConfig: LogProducerConfig? = null
        var mClient: LogProducerClient? = null

        fun getInstance(): AliYunLogHelper {
            return Holder.INSTANCE
        }
    }

    constructor() {
        val endpoint = "cn-hangzhou.log.aliyuncs.com"
        val project = "ydl-app-log"
        val logstore = "ydl-app-log"
        val accesskeyid = "LTAI5tH6WGqWUqzo4u5GfrCS"
        val accesskeysecret = "uzccm79mr9d4rIsABu3xwfbdUdaeWT"
        try {
            mConfig = LogProducerConfig(endpoint, project, logstore, accesskeyid, accesskeysecret)
            mClient = LogProducerClient(mConfig)
        } catch (e: LogProducerException) {
            e.printStackTrace()
        }
    }


    private object Holder {
        val INSTANCE = AliYunLogHelper()
    }

    /**
     *
     * @param key AliYunLogConfig 接口、登录、声网、WebView、支付、其他
     * @param value 具体的状态值
     * */
    fun sendLog(key: String, value: String) {
        val log = Log()
        log.putContent(key, value)
        if (mClient != null) {
            val res: LogProducerResult = mClient!!.addLog(log)
            Log.d("LogProducerResult", String.format("%s %s", res, res.isLogProducerResultOk))
        }
    }
}