package com.swiftcleaner.chovey.business.push

import android.app.AlarmManager
import android.app.PendingIntent
import android.app.job.JobService
import android.content.Context
import android.content.Intent
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.swiftcleaner.chovey.CleanApplication
import com.swiftcleaner.chovey.business.EventUtils
import java.util.Calendar

class MyWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
    override fun doWork(): Result {
        EventUtils.event("workmanager")
        NotificationUtils.sendNotification(CleanApplication.context, "workmanager")
        initAlarm()
        return Result.success()
    }

    private fun initAlarm() {
        val alarmManager = CleanApplication.context.getSystemService(JobService.ALARM_SERVICE) as AlarmManager
        val pendingIntent = PendingIntent.getBroadcast(
            CleanApplication.context, 1,
            Intent(CleanApplication.context, AlarmReceiver::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)
        alarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP,
            calendar.timeInMillis,
            1800000,
            pendingIntent
        )
    }
}