<?php

namespace EasyCriticalCSS;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Notification {
	public static function init() {
		add_action( 'admin_enqueue_scripts', [ __CLASS__, 'register_assets' ] );
		add_action( 'admin_notices', [ __CLASS__, 'show_notice' ] );
		add_action( 'wp_ajax_ecc_subscription_notification_dismiss', [ __CLASS__, 'ajax_dismiss' ] );
	}

	private static function should_show_notice() {
		// User can manage options?
		if ( ! current_user_can( 'manage_options' ) ) {
			return false;
		}

		// Has the notification been dismissed?
		$notif_dismissed = Timed_Option::get( 'auto_mode_setup_notif' );
		if ( $notif_dismissed !== false ) {
			return false;
		}

		// auto_mode_status is all_ok?
		$auto_mode_status = Helpers::get_auto_mode_status();
		if ( empty( $auto_mode_status['all_ok'] ) ) {
			return false;
		}

		// Is global critical css mode auto?
		$mode = Settings::get_global_mode();
		if ( 'auto' === $mode ) {
			return false;
		}

		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen ) {
			return false;
		}

		$allowed_screens = [
			'dashboard',
			'dashboard-network',
			'edit-post',
			'edit-page',
			'plugins',
			'plugins-network',
			'settings_page_easy-critical-css-settings',
		];

		if ( ! in_array( $screen->id, $allowed_screens, true ) ) {
			return false;
		}

		return true;
	}

	public static function register_assets() {
		if ( ! is_admin() ) {
			return;
		}

		if ( ! self::should_show_notice() ) {
			return;
		}

		wp_enqueue_script(
			'easy-critical-css-admin',
			plugins_url( 'assets/admin.js', Plugin::get_plugin_file() ),
			[],
			Plugin::get_plugin_version(),
			true
		);

		wp_localize_script(
			'easy-critical-css-admin',
			'eccSubscriptionNotice',
			[
				'restUrl' => esc_url_raw( rest_url( 'easy-critical-css/v1/' ) ),
				'nonce'   => wp_create_nonce( 'wp_rest' ),
				'plugin'  => [
					'name'    => 'Easy Critical CSS',
					'version' => Plugin::get_plugin_version(),
				],
			]
		);
	}

	public static function show_notice() {
		if ( ! self::should_show_notice() ) {
			return;
		}

		$settings_url = admin_url( 'options-general.php?page=easy-critical-css-settings' );
		$docs_url     = 'https://criticalcss.net/docs?utm_source=easy-critical-css-plugin&utm_medium=notification&utm_campaign=v' . Plugin::get_plugin_version();
		$plugin_name  = 'Easy Critical CSS';
		$plugin_ver   = Plugin::get_plugin_version();

		?>
		<div class="notice notice-info is-dismissible ecc-subscription-notice" data-nonce="<?php echo esc_attr( wp_create_nonce( 'ecc_subscription_notification_nonce' ) ); ?>">
			<p style="font-weight:600;"><?php echo esc_html( sprintf( '%s v%s', $plugin_name, $plugin_ver ) ); ?></p>
			<p>
				<?php esc_html_e( 'Auto-generation is ready to use. Activate it to automatically generate Critical CSS when your site updates.', 'easy-critical-css' ); ?>
			</p>
			<p>
				<a href="<?php echo esc_url( $settings_url ); ?>" class="button button-primary" id="ecc-activate-auto-generation"><?php esc_html_e( 'Activate Auto Generation', 'easy-critical-css' ); ?></a>
				<a href="<?php echo esc_url( $docs_url ); ?>" class="button" target="_blank" rel="noopener noreferrer" style="margin-left:8px;"><?php esc_html_e( 'Learn More', 'easy-critical-css' ); ?></a>
			</p>
		</div>
		<?php
	}
}
