package com.base.filerecoveryrecyclebin.work

import android.content.Context
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.base.filerecoveryrecyclebin.fcm.NotificationUtil
import com.base.filerecoveryrecyclebin.utils.EventUtils
import java.util.concurrent.TimeUnit

fun schedulePeriodicWork(context: Context) {
    val request = PeriodicWorkRequestBuilder<RepeatingWorker>(15, TimeUnit.MINUTES)
        .setConstraints(
            Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
        )
        .build()

    WorkManager.getInstance(context).enqueueUniquePeriodicWork(
        "uniqueWorkName",
        ExistingPeriodicWorkPolicy.KEEP,
        request
    )
}

class RepeatingWorker(val appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {

    override fun doWork(): Result {
        // 这里执行你的任务
        // 例如，更新UI，发送网络请求等
        EventUtils.event("workmanager_live")
        try {
            NotificationUtil.sendNotification(appContext, "WorkManager");
        } catch (e: Exception) {
            EventUtils.event("WorkManager Error")
        }
        return Result.success()
    }
}