package com.ydl.ydlcommon.app

import android.content.Context
import com.apm.insight.MonitorCrash
import com.apm.insight.log.VLog
import com.bytedance.apm.insight.ApmInsight
import com.bytedance.apm.insight.ApmInsightInitConfig
import com.bytedance.apm.insight.IDynamicParams
import com.meituan.android.walle.WalleChannelReader

object Apm {

    private var crash: MonitorCrash? = null

    fun initApm(context: Context, appId: String, vCode: Long, vName: String, debug: Boolean, uidCall: () -> String?) {
        val channel = if (debug) "debug" else WalleChannelReader.getChannel(context)
        crash = MonitorCrash.init(
            context,
            appId,
            vCode,
            vName
        )
        crash?.config()?.setChannel(channel)
        val builder = ApmInsightInitConfig.builder()
            .aid(appId)
            //.batteryMonitor(true)
            //.cpuMonitor(true)
            //.fpsMonitor(true)
            //.seriousBlockDetect(true)
            .blockDetect(true)
//            .enableWebViewMonitor(true)
            .channel(channel)
            .debugMode(debug)
            .enableLogRecovery(true)
            .setDynamicParams(ApmParams(crash, uidCall))
        ApmInsight.getInstance().init(context, builder.build())
        VLog.init(context, 20)
    }

    fun reportCustom(type: String, msg: String, throwable: Throwable) {
        crash?.reportCustomErr(msg, type, throwable)
    }
}

private class ApmParams(val crash: MonitorCrash?, val uidCall: () -> String?) : IDynamicParams() {
    override fun getUserUniqueID(): String? {
        return null
    }

    override fun getAbSdkVersion(): String? {
        return null
    }

    override fun getSsid(): String? {
        return null
    }

    override fun getDid(): String? {
        return null
    }

    override fun getUserId(): String? {
        val uid = uidCall.invoke()
        crash?.config()?.setUID(uid)
        return uid
    }
}