<?php //phpcs:ignore
/**
 * Plugin Notice.
 *
 * @package PRECO
 */

namespace PRECO\Includes\Admin;

use PRECO\Includes\Durbin\DurbinClient;
use PRECO\Includes\Preco;

defined( 'ABSPATH' ) || exit;

/**
 * Plugin Notice
 */
class Notice {

	/**
	 * Only recognized durbin notice key. Anything else is rejected instead of
	 * being written verbatim into an option name.
	 */
	const DURBIN_KEY = 'preco_durbin_check';

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_notices', array( $this, 'admin_notices_callback' ) );
		add_action( 'admin_init', array( $this, 'set_dismiss_notice_callback' ) );
	}

	/**
	 * Admin Notices Callback
	 *
	 * @return void
	 */
	public function admin_notices_callback() {
		$this->dashboard_durbin_notice_callback();
	}

	/**
	 * Set Notice Dismiss Callback
	 *
	 * @return void
	 */
	public function set_dismiss_notice_callback() {
		$preco_db_nonce = sanitize_text_field( wp_unslash( $_GET['preco_db_nonce'] ?? '' ) );

		if ( ! ( ! empty( $preco_db_nonce ) && wp_verify_nonce( $preco_db_nonce, 'preco-nonce' ) ) ) {
			return;
		}

		$durbin_key = sanitize_text_field( wp_unslash( $_GET['preco_durbin_key'] ?? '' ) );

		// Durbin notice dismiss. Reject anything but the known key instead of
		// writing an arbitrary caller-supplied string into an option name.
		if ( self::DURBIN_KEY === $durbin_key ) {
			Preco::set_transient_without_cache( 'preco_durbin_notice_' . $durbin_key, 'off' );

			if ( 'get' === sanitize_text_field( wp_unslash( $_GET['preco_get_durbin'] ?? '' ) ) ) {
				DurbinClient::send( DurbinClient::ACTIVATE_ACTION );
			}
		}
	}

	/**
	 * The Durbin Html
	 *
	 * @return void
	 */
	public function dashboard_durbin_notice_callback() {
		$durbin_key = self::DURBIN_KEY;

		if (
			isset( $_GET['preco_durbin_key'] ) || // phpcs:ignore
			'off' === Preco::get_transient_without_cache( 'preco_durbin_notice_' . $durbin_key )
		) {
			return;
		}

		$preco_db_nonce = wp_create_nonce( 'preco-nonce' );

		?>
		<style>
				.preco-consent-box {
					width: 656px;
					padding: 16px !important;
					border: 1px solid #070707;
					border-left-width: 4px;
					border-radius: 4px;
					background-color: #fff;
					position: relative;
					width: 100%;
					box-sizing: border-box;
				}
				.preco-consent-content {
					display: flex;
					justify-content: flex-start;
					align-items: flex-end;
					gap: 26px;
				}

				.preco-consent-text-first {
					font-size: 14px;
					font-weight: 600;
					color: #070707;
				}
				.preco-consent-text-last {
					margin: 4px 0 0;
					font-size: 14px;
					color: #070707;
				}

				.preco-consent-accept {
					background-color: #070707;
					color: #fff ! important;
					border: none;
					padding: 6px 10px;
					border-radius: 4px;
					cursor: pointer;
					font-size: 12px;
					font-weight: 600;
					text-decoration: none;
				}
				.preco-consent-accept:hover {
					background-color:rgb(38, 38, 38);
					color: #fff;
				}
			</style>
			<div class="preco-consent-box preco-notice-wrapper notice data_collection_notice">
			<div class="preco-consent-content">
			<div class="preco-consent-text">
			<div class="preco-consent-text-first"><?php esc_html_e( 'Want to help make WowRecommend even more awesome?', 'wow-ai-product-recommendations' ); ?></div>
			<div class="preco-consent-text-last">
					<?php esc_html_e( 'Allow us to collect diagnostic data and usage information. see ', 'wow-ai-product-recommendations' ); ?>
			<a href="https://www.wpxpo.com/data-collection-policy/" target="_blank" ><?php esc_html_e( 'what we collect.', 'wow-ai-product-recommendations' ); ?></a>
			</div>
			</div>
			<a
					class="preco-consent-accept"
					href=
					<?php
									echo esc_url(
										add_query_arg(
											array(
												'preco_durbin_key' => $durbin_key,
												'preco_get_durbin' => 'get',
												'preco_db_nonce'   => $preco_db_nonce,
											)
										)
									);
					?>
									class="preco-notice-close"
			><?php esc_html_e( 'Accept & Close', 'wow-ai-product-recommendations' ); ?></a>
			</div>
			<a href=
				<?php
							echo esc_url(
								add_query_arg(
									array(
										'preco_durbin_key' => $durbin_key,
										'preco_db_nonce'   => $preco_db_nonce,
									)
								)
							);
				?>
				class="preco-notice-close"
				style="
					position: absolute;
					right: 2px;
					top: 5px;
					text-decoration: unset;
					color: #b6b6b6;
					font-family: dashicons;
					font-size: 16px;
					font-style: normal;
					font-weight: 400;
					line-height: 20px;
				"
			>
				<span
				style="font-size: 14px;"
				class="preco-notice-close-icon dashicons dashicons-dismiss"> </span></a>
			</div>
		<?php
	}
}
