<?php
namespace WPUltimateSecurity\UltimateSecurity\Bootstrap;
// Your code starts here.
if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly.
}
use WPUltimateSecurity\UltimateSecurity\Support\Helpers\Utils;
class Initializer
{

	private static $options;

	/**
	 * Plugin basename
	 */
	const PLUGIN_BASENAME = 'ultimate-security/ultimate-security.php';
	public function __construct()
	{
		self::$options = Utils::ultimate_security_get_option();
		// Register database lifecycle hooks
		\WPUltimateSecurity\UltimateSecurity\Bootstrap\Lifecycle\DatabaseUpdater::register();

		// Load translations, bootstrap plugin, and register action links
		add_action('init', static function (): void {
			load_plugin_textdomain(
				'ultimate-security',
				false,
				dirname(plugin_basename(__FILE__)) . '/languages'
			);

			// Plugin action links (must be registered after translations are loaded)
			add_filter(
				'plugin_action_links_' . self::PLUGIN_BASENAME,
				[self::class, 'add_action_links']
			);

			\WPUltimateSecurity\UltimateSecurity\Bootstrap\Plugin::init();
		});
		// Insert login nonce
		add_action('login_form', [self::class, 'ultimate_security_login_form_nonce']);
		self::ultimate_security_apply_update_settings();
	}

	// Apply settings
	public static function ultimate_security_apply_update_settings()
	{
		$options = self::$options;

		// Plugin updates //manual_updates, auto, disable
		if ($options['security']['updates']['plugin_updates'] === 'auto') {
			add_filter('auto_update_plugin', '__return_true');
		} elseif ($options['security']['updates']['plugin_updates'] === 'disable') {

			// remove_action('load-update-core.php', 'wp_update_plugins');
			// wp_clear_scheduled_hook('wp_update_plugins');
			// add_filter('auto_update_plugin', '__return_false');

			// // Hide "update available" notification in plugin list
			// add_filter('pre_site_transient_update_plugins', '__return_null');

			add_filter( 'auto_update_plugin', '__return_false' );
			add_filter( 'pre_site_transient_update_plugins', '__return_null' );
		}
		// Theme updates
		if ($options['security']['updates']['theme_updates'] === 'auto') {
			add_filter('auto_update_theme', '__return_true');
		} elseif ($options['security']['updates']['theme_updates'] === 'disable') {

			// remove_action('load-update-core.php', 'wp_update_themes');
			// wp_clear_scheduled_hook('wp_update_themes');
			// add_filter('auto_update_theme', '__return_false');

			// // Hide "update available" notification in themes list
			// add_filter('pre_site_transient_update_themes', '__return_null');

			add_filter( 'auto_update_theme', '__return_false' );
			add_filter( 'pre_site_transient_update_themes', '__return_null' );
		}

		// Disable translation updates
		if ($options['security']['updates']['disable_translation_updates']) {
			add_filter('auto_update_translation', '__return_false');
		}

		// WordPress core updates
		$core_updates = $options['security']['updates']['core_updates'];
		if ($core_updates === 'disable') {
			add_filter('auto_update_core', '__return_false');
		} elseif ($core_updates === 'disable_auto') {
			add_filter('allow_minor_auto_core_updates', '__return_false');
			add_filter('allow_major_auto_core_updates', '__return_false');
		} elseif ($core_updates === 'minor') {
			add_filter('allow_minor_auto_core_updates', '__return_true');
		} elseif ($core_updates === 'major') {
			add_filter('allow_major_auto_core_updates', '__return_true');
		} elseif ($core_updates === 'development') {
			add_filter('allow_dev_auto_core_updates', '__return_true');
		}

		// Enable updates for VCS installations
		if ($options['security']['updates']['vcs_updates']) {
			add_filter('automatic_updates_is_vcs_checkout', '__return_false', 10, 2);
		}

		// Updates nags only for Admin
		if ($options['security']['updates']['admin_update_nags']) {
			add_filter('admin_init', function () {
				if (!current_user_can('manage_options')) {
					remove_action('admin_notices', 'update_nag', 3);
					remove_action('admin_notices', 'maintenance_nag', 10);
					remove_action('network_admin_notices', 'maintenance_nag', 10);
				}
			});
		}

		
	}

	public static function ultimate_security_login_form_nonce()
	{
		wp_nonce_field('ultimate_security_login_form_action', 'ultimate_security_login_form_field');
	}


	/**
	 * Add settings action link to the plugins page.
	 *
	 * @since    1.0.0
	 */
	public static function add_action_links($links)
	{

		/**
		 * Documentation : https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
		 * The "plugins.php" must match with the previously added add_submenu_page first option.
		 * For custom post type you have to change 'plugins.php?page=' to 'edit.php?post_type=your_custom_post_type&page='
		 * 
		 */
		$settings_link = array(
			'<a href="' . admin_url('admin.php?page=ultimate-security') . '">' . esc_html__('Dashboard', 'ultimate-security') . '</a>',
			'<a href="' . admin_url('admin.php?page=ultimate-security#/settings/login/2fa/overview') . '">' . esc_html__('Settings', 'ultimate-security') . '</a>',
			'<a href="https://wpultimatesecurity.com/" target="_blank">' . esc_html__('PRO (Coming Soon)', 'ultimate-security') . '</a>',
		);
		return array_merge($settings_link, $links);
	}
}