<?php
/**
 * Class responsible for creating notices markup.
 *
 * Author:          Uriahs Victor
 *
 * @link    https://uriahsvictor.com
 * @since   1.0.0
 * @package Notices
 */

namespace Printus\Notices;

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

/**
 * Class Notice.
 */
class Notice {

	/**
	 * At what version the plugin was installed.
	 *
	 * @return mixed
	 * @since 1.1.0
	 */
	protected function pluginInstalledAt() {
		return get_option( 'printus_installed_at_version', '1.0.0' );
	}

	/**
	 * Get the current user id
	 *
	 * @return int
	 */
	protected function get_user_id() {
		return get_current_user_id();
	}

	/**
	 * Get the notice ids that have been dismissed by user.
	 *
	 * @return mixed
	 */
	protected function get_dismissed_notices() {
		return get_user_meta( $this->get_user_id(), 'printus_dismissed_notices', true );
	}

	/**
	 * Create the dismiss URL for a notice.
	 *
	 * @param string $notice_id The ID of the particular notice.
	 * @return string
	 */
	protected function createDismissUrl( string $notice_id ): string {

		if ( ! function_exists( 'wp_create_nonce' ) ) {
			require_once ABSPATH . 'wp-includes/pluggable.php';
		}
		$nonce = wp_create_nonce( 'printus_notice_nonce_value' );

		return admin_url( 'admin-ajax.php?action=printus_dismiss_notice&printus_notice_id=' . $notice_id . '&printus_notice_nonce=' . $nonce );
	}

	/**
	 * Create the markup for a notice
	 *
	 * @param string $notice_id The ID of the particular notice.
	 * @param array  $content   The content to add to the notice.
	 *
	 * @return void
	 */
	protected function create_notice_markup( string $notice_id, array $content ): void {

		// Only show the Notice to Admins.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$dismissed_notices = $this->get_dismissed_notices();

		// Bail if this notice has been dismissed.
		if ( is_array( $dismissed_notices ) && in_array( $notice_id, $dismissed_notices, true ) ) {
			return;
		}

		$title           = $content['title'] ?? '';
		$body            = $content['body'] ?? '';
		$cta_text        = $content['cta'] ?? __( 'Learn more', 'printus-cloud-printing-for-woocommerce' );
		$learn_more_link = $content['link'] ?? '';

		$dismiss_url  = $this->createDismissUrl( $notice_id );
		$dismiss_text = __( 'Dismiss', 'printus-cloud-printing-for-woocommerce' );
		?>

		<div class="printus-admin-notice" role="alert">
			<div class="printus-notice-logo" aria-hidden="true"></div>

			<div class="printus-notice-content">
				<p class="printus-notice-title"><?php echo esc_html( $title ); ?></p>

				<div class="printus-notice-body">
					<p><?php echo wp_kses_post( $body ); ?></p>

					<div class="printus-notice-actions">
						<?php if ( ! empty( $learn_more_link ) ) { ?>
							<a href="<?php echo esc_url( $learn_more_link ); ?>"
								target="_blank"
								rel="noopener noreferrer"
								class="printus-button printus-button-primary"
								id="printus-notice-cta">
								<?php echo esc_html( $cta_text ); ?>
								<span class="dashicons dashicons-external" aria-hidden="true"></span>
							</a>
						<?php } ?>

						<a href="<?php echo esc_url( $dismiss_url ); ?>"
							class="printus-button printus-button-dismiss"
							id="printus-notice-dismiss">
							<span class="dashicons dashicons-dismiss" aria-hidden="true"></span>
							<?php echo esc_html( $dismiss_text ); ?>
						</a>
					</div>
				</div>
			</div>
		</div>

		<?php
	}

	/**
	 * Get the ID of a notice from the URL.
	 *
	 * @return mixed
	 */
	protected function get_notice_id() {

		$nonce = sanitize_text_field( wp_unslash( $_REQUEST['printus_notice_nonce'] ?? '' ) );

		if ( ! wp_verify_nonce( $nonce, 'printus_notice_nonce_value' ) ) {
			exit( __( 'Failed to verify nonce. Please try going back and refreshing the page to try again.', 'printus-cloud-printing-for-woocommerce' ) );
		}

		$notice_id = sanitize_text_field( wp_unslash( $_REQUEST['printus_notice_id'] ?? '' ) );

		if ( empty( $notice_id ) ) {
			return;
		}

		return $notice_id;
	}

	/**
	 * Dismiss a notice so it doesn't show again.
	 *
	 * @return void
	 */
	public function dismiss_notice() {

		$nonce = sanitize_text_field( wp_unslash( $_REQUEST['printus_notice_nonce'] ?? '' ) );

		if ( ! wp_verify_nonce( $nonce, 'printus_notice_nonce_value' ) ) {
			exit( __( 'Failed to verify nonce. Please try going back and refreshing the page to try again.', 'printus-cloud-printing-for-woocommerce' ) );
		}

		$notice_id = $this->get_notice_id();

		if ( ! empty( $notice_id ) ) {

			$dismissed_notices = $this->get_dismissed_notices();

			if ( empty( $dismissed_notices ) ) {
				$dismissed_notices = array();
			}

			// Add our new notice ID to the currently dismissed ones.
			array_push( $dismissed_notices, $notice_id );

			$dismissed_notices = array_unique( $dismissed_notices );

			update_user_meta( $this->get_user_id(), 'printus_dismissed_notices', $dismissed_notices );

			wp_safe_redirect( sanitize_text_field( wp_unslash( $_SERVER['HTTP_REFERER'] ?? get_admin_url() ) ) );
			exit;

		}

		return;
	}
}
