package com.base.locationsharewhite.fcm.alarm

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.base.locationsharewhite.fcm.NotificationUiUtil.sendNotificationIfCan
import com.base.locationsharewhite.fcm.PopupConstObject
import com.base.locationsharewhite.fcm.work.RepeatingWorker.Companion.schedulePeriodicWork
import com.base.locationsharewhite.helper.EventUtils
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.LogEx

class AlarmReceiver : BroadcastReceiver() {

    private val TAG = "AlarmJobReceiver"

    @SuppressLint("UnsafeProtectedBroadcastReceiver")
    override fun onReceive(context: Context, intent: Intent?) {
        LogEx.logDebug(TAG, "AlarmJobReceiver onReceive")
        EventUtils.event("alarm_push")
        val lastAlarmTime = AppPreferences.getInstance().getLong("lastAlarmTime", 0L)
        val currentTime = System.currentTimeMillis()
        if (lastAlarmTime == 0L || currentTime - lastAlarmTime >= 1000 * 30 * 60) {
            sendNotificationIfCan(context, PopupConstObject.POPUP_WHERE_ALARM)
            AppPreferences.getInstance().put("lastAlarmTime", System.currentTimeMillis())
        }
        schedulePeriodicWork(context)
        return
    }
}