<?php

namespace Linksy\SearchAndReplace\Plugli\Standard\Helpers;

class WP_Cron_Manager
{
    protected static $_events = [];
    protected static $_schedules = [];
    protected static $_prefix = 'plugli_';
    public static function get_events()
    {
        return static::$_events;
    }
    public static function get_schedules()
    {
        return static::$_schedules;
    }
    public static function add_event($event, $one_time = \false)
    {
        $hook_name = $event->get_name();
        $interval = $event->get_interval();
        // schedule cron event
        if ($one_time) {
            wp_schedule_single_event(\time() + rand(60, 180), $hook_name);
        } else if (!wp_next_scheduled($hook_name)) {
            wp_schedule_event(\time() + rand(0, 5), $interval, $hook_name);
        }
        static::$_events[] = $hook_name;
    }
    public static function add_schedule($name, $interval, $display_name = null)
    {
        $schedule_name = static::$_prefix . $name;
        if (!$display_name) {
            $display_name = 'Every ' . round($interval / 60) . ' Minutes';
        }
        if (!isset(static::$_schedules[$schedule_name])) {
            static::$_schedules[$schedule_name] = ['interval' => $interval, 'display' => __($display_name)];
        }
    }
    public static function unschedule_all()
    {
        Utils::error_log(static::$_events, 'all linksy cron events');
        foreach (static::$_events as $event) {
            if (strpos($event, static::$_prefix) === 0) {
                $timestamp = wp_next_scheduled($event);
                wp_unschedule_event($timestamp, $event);
            }
        }
    }
    public static function on_complete($data)
    {
    }
}
