package com.base.locationsharewhite.fcm.alarm

import android.app.AlarmManager
import android.app.PendingIntent
import android.app.job.JobService
import android.content.Context
import android.content.Intent
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.fcm.work.RepeatingWorker.Companion.schedulePeriodicWork
import com.base.locationsharewhite.service.StayJobService.Companion.startJob
import com.base.pdfreaderallpdfreader.fcm.alarm.AlarmJobReceiver
import java.util.Calendar

object AlarmUtils {

    fun startAlarm(context: Context) {
        context.startJob()
        //闹钟定时任务
        val alarmManager = context.getSystemService(JobService.ALARM_SERVICE) as AlarmManager
        val pendingIntent = PendingIntent.getBroadcast(
            context, 1, Intent(context, AlarmJobReceiver::class.java),
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 6)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        var delay = 1800000L
        if (BuildConfig.DEBUG) {
            delay = 1 * 60 * 1000L
        }
        alarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP, calendar.timeInMillis, delay, pendingIntent
        )
        schedulePeriodicWork(context)
    }
}