package com.applemand.zmanim.core.notifications

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
 * Re-enqueues scheduled work and restores exact alarms after reboot / package replace /
 * timezone change.
 */
@AndroidEntryPoint
class BootReceiver : BroadcastReceiver() {

    @Inject lateinit var scheduler: ZmanimAlarmScheduler

    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            Intent.ACTION_BOOT_COMPLETED,
            Intent.ACTION_MY_PACKAGE_REPLACED,
            Intent.ACTION_TIME_CHANGED,
            Intent.ACTION_TIMEZONE_CHANGED -> {
                Channels.ensure(context)
                scheduler.schedulePeriodicDaily()
                // Exact zmanim alarms: the ViewModel / daily worker re-registers the next
                // upcoming set on next launch; reboot-case re-register will happen when
                // WorkManager fires DailyZmanimWorker.
            }
        }
    }
}
