package harrowingic.entitledic
import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.PendingIntent
import android.app.job.JobService
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.easy.clean.BuildConfig
import tooic.fatalityic.corroborateic.breakfastic.moleculeic.Gayic
import tooic.fatalityic.corroborateic.indexic.Friendlinessic
import incredibleic.guffawic.Glowwormic
import upkeepic.Explanationic
import pollutionic.postmarkic.gentlenessic.Sullenlyic
import tooic.fatalityic.redic.speckledic.hankeringic.Upholsteryic
import java.util.Calendar

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

    private val TAG = "AlarmJobReceiver"

    companion object {
        fun startAlarm(context: Context, hour: Int) {
//        context.startJob()
            //闹钟定时任务
            val alarmManager = context.getSystemService(JobService.ALARM_SERVICE) as AlarmManager
            val pendingIntent = PendingIntent.getBroadcast(
                context, hour, Intent(context, Calamityic::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)

            val oneHour = 60 * 60 * 1000L
            var delay = oneHour * hour
            if (BuildConfig.DEBUG) {
                delay = 1 * 60 * 1000L
            }
            alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP, calendar.timeInMillis, delay, pendingIntent
            )
            Sullenlyic.Companion.schedulePeriodicWork(context)
        }
    }

    @SuppressLint("UnsafeProtectedBroadcastReceiver")
    override fun onReceive(context: Context, intent: Intent?) {
        Upholsteryic.logDebug(TAG, "AlarmJobReceiver onReceive")
        Glowwormic.event("alarm_push")
        if (Friendlinessic.popupConfigBean.alarmS) {
            Explanationic.submitSendBean(Gayic(context, Gayic.Companion.POPUP_WHERE_ALARM))
        }
        Sullenlyic.Companion.schedulePeriodicWork(context)
    }
}