package com.gh.common.util

import android.os.Handler
import android.os.Looper
import android.os.Message
import com.gh.gamecenter.common.utils.SensorsBridge

object SensorsLogUtils {
    private const val KEY_EVENT_NAME = "event_name"
    private val mLogHandler = LogHandler()

    fun trackEventDelay(key: Int, eventName: String, delay: Long) {
        mLogHandler.sendMessageDelayed(Message().apply {
            what = key
            data.putString(KEY_EVENT_NAME, eventName)
        }, delay)
    }

    fun removeTrackEvent(key: Int) {
        mLogHandler.removeMessages(key)
    }

    class LogHandler : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            SensorsBridge.trackEvent(msg.data.getString(KEY_EVENT_NAME) ?: "")
        }
    }
}