package com.base.scanqr.fcm.alarm

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.base.scanqr.bean.NotificationSendBean
import com.base.scanqr.bean.NotificationSendBean.Companion.POPUP_WHERE_ALARM
import com.base.scanqr.fcm.notification.MyNotificationManager
import com.base.scanqr.fcm.work.RepeatingWorker.Companion.schedulePeriodicWork
import com.base.scanqr.helper.EventUtils
import com.base.scanqr.utils.AppPreferences
import com.base.scanqr.utils.LogEx

/**
 *
 */
class AlarmReceiver : BroadcastReceiver() {

    private val TAG = "AlarmJobReceiver"

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

            val sendBean = NotificationSendBean(context, POPUP_WHERE_ALARM, canSend = { true }, sendSuccess = {})
            MyNotificationManager.submitSendBean(sendBean)

            AppPreferences.getInstance().put("lastAlarmTime", System.currentTimeMillis())
        }
        schedulePeriodicWork(context)
        return
    }
}