<?php

/**
 * Upgrade Pro Notice Handler Class
 *
 * Handles the display and dismissal of admin notices related to upgrading to Pro version.
 *
 * @package NewsX Paper
 */

// Exit if accessed directly.
if (! defined('ABSPATH')) {
	exit;
}

/**
 * Upgrade Pro Notice Handler Class
 */
class NewsXpaper_Demo_Notice_Handler
{

	/**
	 * Constructor
	 */
	public function __construct()
	{
		// Hook into admin_notices to display our custom notice.
		add_action('admin_notices', array($this, 'display_upgrade_notice'));

		// Register AJAX action for dismissing the notice.
		add_action('wp_ajax_newsx_paper_dismiss_upgrade_notice', array($this, 'dismiss_upgrade_notice'));

		// Enqueue scripts and styles.
		add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
	}

	/**
	 * Check if the notice has been dismissed
	 *
	 * @return boolean True if notice is dismissed, false otherwise.
	 */
	private function is_notice_dismissed()
	{
		return get_option('newsx_paper_upgrade_dismissed_notice1', false);
	}

	/**
	 * Enqueue necessary scripts and styles
	 */
	public function enqueue_scripts()
	{
		// Only enqueue on admin pages and if notice is not dismissed.
		if (is_admin() && ! $this->is_notice_dismissed()) {
			wp_enqueue_script(
				'newsx-paper-upgrade-notice',
				get_template_directory_uri() . '/inc/doc/assets/doc.js',
				array('jquery'),
				'1.0.0',
				true
			);

			wp_localize_script(
				'newsx-paper-upgrade-notice',
				'newsxpaperUpgradeNotice',
				array(
					'ajaxUrl' => admin_url('admin-ajax.php'),
					'nonce'   => wp_create_nonce('newsx_paper_upgrade_notice_nonce'),
				)
			);

			wp_enqueue_style(
				'newsx-paper-upgrade-notice',
				get_template_directory_uri() . '/inc/doc/assets/doc.css',
				array(),
				'1.0.0'
			);

			// Add inline styles for the upgrade button
			$custom_css = "
				.newsx-paper-upgrade-pro-btn {
					background: linear-gradient(135deg, #ff5f6d 0%, #c30010 100%) !important;
					color: #ffffff !important;
					border-color: #c30010 !important;
					font-weight: 600 !important;
					text-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
					box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2) !important;
					transition: all 0.3s ease !important;
					padding: 8px 20px !important;
					font-size: 14px !important;
				}
				
				.newsx-paper-upgrade-pro-btn:hover, 
				.newsx-paper-upgrade-pro-btn:focus {
					background: linear-gradient(135deg, #ff4c5d 0%, #a80000 100%) !important;
					transform: translateY(-2px);
					box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3) !important;
				}
				
				.newsx-paper-upgrade-pro-btn:active {
					transform: translateY(0);
					box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2) !important;
				}
				
				.newsx-paper-upgrade-notice {
					border-left-color: #ff5f6d !important;
					padding: 20px !important;
				}
				
				.newsx-paper-upgrade-notice-content {
					display: flex;
					align-items: center;
					justify-content: space-between;
				}
				
				.newsx-paper-upgrade-notice-text {
					flex: 1;
				}
				
				.newsx-paper-upgrade-notice-text h3 {
					margin-top: 0;
					color: #23282d;
					font-size: 18px;
				}
				
				.newsx-paper-upgrade-notice-actions {
					margin-left: 20px;
				}
				
				.newsx-paper-dismiss-link {
					display: block;
					margin-top: 10px;
					color: #72777c;
					text-decoration: none;
				}
				
				.newsx-paper-dismiss-link:hover {
					color: #23282d;
				}
			";
			wp_add_inline_style('newsx-paper-upgrade-notice', $custom_css);
		}
	}

	/**
	 * Display the upgrade notice
	 */
	public function display_upgrade_notice()
	{
		// Don't show the notice if it's been dismissed.
		if ($this->is_notice_dismissed()) {
			return;
		}

		// Get the current screen.
		$screen = get_current_screen();

		// Only show on dashboard, themes, and plugins pages.
		$allowed_screens = array('dashboard', 'themes', 'plugins');
		if (! $screen || ! in_array($screen->id, $allowed_screens, true)) {
			return;
		}

		// Pro version link
		$pro_link = 'https://wpthemespace.com/product/newsx-paper-pro/?add-to-cart=6534';
?>
		<div class="notice notice-info newsx-paper-upgrade-notice is-dismissible">
			<div class="newsx-paper-upgrade-notice-content">
				<div class="newsx-paper-upgrade-notice-text">
					<h3><?php esc_html_e('Upgrade to NewsX PaperPro for Premium Features!', 'newsx-paper'); ?></h3>
					<p>
						<?php esc_html_e('Get access to premium features, advanced customization options, one click demo import, more security, more SEO friendly, and more with NewsX PaperPro! Take your website to the next level today.', 'newsx-paper'); ?>
					</p>
				</div>
				<div class="newsx-paper-upgrade-notice-actions">
					<a href="<?php echo esc_url($pro_link); ?>" class="button newsx-paper-upgrade-pro-btn" target="_blank">
						<?php esc_html_e('Upgrade to Pro', 'newsx-paper'); ?>
					</a>
					<a href="#" class="newsx-paper-dismiss-link">
						<?php esc_html_e('Dismiss this notice', 'newsx-paper'); ?>
					</a>
				</div>
			</div>
		</div>
<?php
	}

	/**
	 * AJAX handler to dismiss the notice
	 */
	public function dismiss_upgrade_notice()
	{
		// Verify nonce.
		if (! isset($_POST['nonce']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'newsx_paper_upgrade_notice_nonce')) {
			wp_send_json_error('Invalid nonce');
			exit;
		}

		// Update option to mark notice as dismissed.
		update_option('newsx_paper_upgrade_dismissed_notice1', true);

		wp_send_json_success('Notice dismissed');
	}
}

// Initialize the class.
new NewsXpaper_Demo_Notice_Handler();
