package com.base.datarecovery.fcm;

import android.util.Log;

import com.base.datarecovery.MyApplication;

import java.util.Timer;
import java.util.TimerTask;

public class RecoveryTimerManager {
    private static RecoveryTimerManager instance;
    private Timer taskTimer;
    private boolean isTimerActive;

    private RecoveryTimerManager() {
        // 将构造方法设置为私有，确保通过 getInstance 方法获取实例
    }

    public static synchronized RecoveryTimerManager getInstance() {
        if (instance == null) {
            instance = new RecoveryTimerManager();
        }
        return instance;
    }

    public void scheduleTask(long delay, long period) {
        ensureTimerIsStopped(); // 确保在启动新定时任务前停止旧的定时器
        taskTimer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Log.d("glc", "Scheduled task is running");
                if (ScreenStatusReceiver.isDeviceInteractive() && !ScreenStatusReceiver.isSecureLockActive() && MyApplication.PAUSED_VALUE!=1) {
                    Log.d("glc", "Scheduled task conditions are met");
                    // NotificationUtil.notifyUser(AppContext.getContext());
                }
            }
        };
        taskTimer.schedule(task, delay, period);
        isTimerActive = true;
    }

    private void ensureTimerIsStopped() {
        if (isTimerActive) {
            taskTimer.cancel();
            taskTimer = null;
            isTimerActive = false;
        }
    }

    public void stopTaskTimer() {
        ensureTimerIsStopped();
        // 清除所有取消的任务
        if (taskTimer != null) {
            taskTimer.purge();
        }
    }

    public boolean isTaskTimerActive() {
        return isTimerActive;
    }
}