<?php

namespace Dev4Press\Plugin\SweepPress\Basic;

use Dev4Press\Plugin\SweepPress\Expand\REST;
use Dev4Press\Plugin\SweepPress\Meta\Tracker;
use Dev4Press\Plugin\SweepPress\Pro\Expand\Backup;
use Dev4Press\Plugin\SweepPress\Pro\Expand\CRON;
use Dev4Press\Plugin\SweepPress\Pro\Expand\Monitor;
use Dev4Press\Plugin\SweepPress\Pro\Expand\Scheduler;
use Dev4Press\v54\Core\Plugins\Core;
use Dev4Press\v54\Core\Quick\BBP;
use Dev4Press\v54\Core\Quick\Sanitize;
use Dev4Press\v54\Core\Quick\WPR;
use Dev4Press\v54\Core\Scope;
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class Plugin extends Core {
    public string $plugin = 'sweeppress';

    public string $svg_icon = 'data:image/svg+xml;base64, ';

    public bool $license = true;

    public bool $debugpress = false;

    public bool $active = false;

    protected int $_plugins_loaded_priority = 1;

    public function __construct() {
        $this->url = SWEEPPRESS_URL;
        $this->path = SWEEPPRESS_PATH;
        parent::__construct();
    }

    public function run() {
        do_action( 'sweeppress_load_settings' );
        do_action( 'sweeppress_plugin_init' );
        $this->active = true;
        Tracker::instance();
        if ( $this->is_cli_enabled() ) {
            add_action( 'cli_init', array($this, 'register_cli_command') );
        }
        if ( $this->is_rest_enabled() ) {
            REST::instance();
        }
        if ( BBP::is_active() ) {
            add_filter( 'sweeppress_db_query_revisions_post_statuses', array($this, 'bbpress_add_post_statuses') );
        }
        if ( $this->s()->get( 'expand_admin_bar' ) ) {
            AdminBar::instance();
            add_action( 'init', array($this, 'admin_bar_cmd_init') );
        }
        add_action( 'debugpress-tracker-plugins-call', array($this, 'debugpress') );
    }

    public function debugpress() {
        if ( function_exists( 'debugpress_store_for_plugin' ) ) {
            debugpress_store_for_plugin( SWEEPPRESS_FILE, array(
                'storage' => sweeppress_settings()->group_get( 'storage' ),
            ) );
        }
    }

    public function after_setup_theme() {
        if ( sweeppress_settings()->get( 'serialized_debugpress_view' ) && WPR::is_plugin_active( 'debugpress/debugpress.php' ) && function_exists( 'debugpress_rx' ) ) {
            $this->debugpress = true;
        }
    }

    public function s() {
        return sweeppress_settings();
    }

    public function f() {
        return null;
    }

    public function b() {
        return null;
    }

    public function l() {
        return License::instance();
    }

    public function fs() {
        return sweeppress_fs();
    }

    public function backup() : ?Backup {
        return ( class_exists( '\\Dev4Press\\Plugin\\SweepPress\\Pro\\Expand\\Backup' ) ? Backup::instance() : null );
    }

    public function log( string $msg ) {
        if ( $this->is_log_enabled() ) {
            sweeppress_log( $msg );
        }
    }

    public function admin_bar_cmd_init() {
        if ( isset( $_GET['sweep-action'] ) && isset( $_GET['sweep-nonce'] ) ) {
            $full = sweeppress()->data_quick_sweepers();
            $items = sweeppress_settings()->get( 'admin_bar_quick_sweepers' );
            $allowed = array('auto');
            foreach ( $items as $item ) {
                if ( isset( $full[$item] ) ) {
                    $allowed[] = 'quick-' . $item;
                }
            }
            $action = Sanitize::key( $_GET['sweep-action'] );
            if ( in_array( $action, $allowed ) && wp_verify_nonce( $_GET['sweep-nonce'], 'sweeppress-action-' . $action ) ) {
                if ( $action == 'auto' ) {
                    sweeppress_core()->auto_sweep( 'adminbar' );
                } else {
                    if ( str_starts_with( $action, 'quick' ) ) {
                        $sweeper = substr( $action, 6 );
                        sweeppress_core()->sweep( array(
                            $sweeper => true,
                        ), 'adminbar' );
                    }
                }
            }
            $url = remove_query_arg( array('sweep-nonce', 'sweep-action') );
            wp_redirect( $url );
            exit;
        }
    }

    public function register_cli_command() {
        \WP_CLI::add_command( 'sweeppress', '\\Dev4Press\\Plugin\\SweepPress\\Expand\\CLI' );
    }

    public function is_log_enabled() : bool {
        return sweeppress_settings()->get( 'log_removal_queries' ) === true;
    }

    public function is_backup_enabled() : bool {
        return sweeppress_settings()->get( 'expand_backup' ) === true && class_exists( '\\Dev4Press\\Plugin\\SweepPress\\Pro\\Expand\\Backup' );
    }

    public function is_estimates_cache_enabled() : bool {
        return sweeppress_settings()->get( 'estimated_cache', 'sweepers' ) === true;
    }

    public function is_cli_enabled() : bool {
        if ( defined( 'SWEEPPRESS_CLI_ENABLED' ) ) {
            return SWEEPPRESS_CLI_ENABLED === true;
        }
        return sweeppress_settings()->get( 'expand_cli' ) === true;
    }

    public function is_rest_enabled() : bool {
        if ( defined( 'SWEEPPRESS_REST_ENABLED' ) ) {
            return SWEEPPRESS_REST_ENABLED === true;
        }
        return sweeppress_settings()->get( 'expand_rest' ) === true;
    }

    public function bbpress_add_post_statuses( array $statuses ) : array {
        $statuses[] = bbp_get_closed_status_id();
        $statuses[] = bbp_get_spam_status_id();
        $statuses[] = bbp_get_trash_status_id();
        $statuses[] = bbp_get_hidden_status_id();
        $statuses[] = bbp_get_private_status_id();
        return $statuses;
    }

    public function data_quick_sweepers() : array {
        $list = array(
            'all-transients'        => __( 'All Transients', 'sweeppress' ),
            'expired-transients'    => __( 'Expired Transients', 'sweeppress' ),
            'rss-feeds'             => __( 'RSS Feeds', 'sweeppress' ),
            'posts-auto-draft'      => __( 'Posts Auto Drafts', 'sweeppress' ),
            'posts-draft-revisions' => __( 'Posts Draft Revisions', 'sweeppress' ),
            'postmeta-locks'        => __( 'Postmeta Locks', 'sweeppress' ),
            'postmeta-edits'        => __( 'Postmeta Edits', 'sweeppress' ),
            'postmeta-oembeds'      => __( 'Postmeta Oembeds', 'sweeppress' ),
        );
        if ( Scope::instance()->is_multisite() ) {
            $list['all-network-transients'] = __( 'All Network Transients', 'sweeppress' );
            $list['network-expired-transients'] = __( 'Network Network Transients', 'sweeppress' );
        }
        return $list;
    }

}
