package com.swiftcleaner.chovey.business.push

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import com.swiftcleaner.chovey.CleanApplication
import com.swiftcleaner.chovey.business.EventUtils
import java.util.concurrent.TimeUnit

class Alarm2Receiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        EventUtils.event("alarm_push")
        NotificationUtils.sendNotification(CleanApplication.context, "alarm")
        initWorkManager()
    }

    private fun initWorkManager() {
        val tag = "${CleanApplication.context.packageName}_unique_periodic_work"
        val periodicRequest = PeriodicWorkRequestBuilder<MyWorker>(15, TimeUnit.MINUTES)
            .setConstraints(
                Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
            )
            .addTag(tag)
            .build()
        WorkManager.getInstance(CleanApplication.context).enqueueUniquePeriodicWork(
            tag,
            ExistingPeriodicWorkPolicy.KEEP,
            periodicRequest)
    }
}