package com.gh.gamecenter.common.utils

import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.core.provider.ISensorsProvider
import org.json.JSONObject

/**
 * 调用神策相关 API 的桥梁
 */
object SensorsBridge {

    private var mIsSensorsEnabled = false

    private val mSensor by lazy {
        ARouter.getInstance().build(RouteConsts.provider.sensors).navigation() as? ISensorsProvider
    }

    @JvmStatic
    fun init(context: Application, channel: String) {
        mIsSensorsEnabled = true

        // 初始化
        mSensor?.init(context, channel)
    }

    @JvmStatic
    fun login(loginId: String) {
        if (mIsSensorsEnabled) mSensor?.login(loginId)
    }

    @JvmStatic
    fun logout(loginId: String) {
        if (mIsSensorsEnabled) mSensor?.logout(loginId)
    }

    @JvmStatic
    fun setGid(gid: String) {
        if (mIsSensorsEnabled) mSensor?.setGid(gid)
    }

    @JvmStatic
    fun trackEvent(eventName: String, vararg kv: String) {
        if (mIsSensorsEnabled) mSensor?.trackEvent(eventName, *kv)
    }

    @JvmStatic
    fun trackEvent(eventName: String, jsonObject: JSONObject) {
        mSensor?.trackEvent(eventName, jsonObject)
    }

    @JvmStatic
    fun profileSet(key: String, value: String) {
        mSensor?.profileSet(key, value)
    }
}