<?php

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

class Shop_Zen_Rating_Notice {
	private $past_date;

	public function __construct() {
		$this->past_date = false == get_option('shop_zen_maybe_later_time') ? strtotime( '-5 days' ) : strtotime('-15 days');

		if ( current_user_can('administrator') ) {
			if ( empty(get_option('shop_zen_rating_dismiss_notice')) && empty(get_option('shop_zen_rating_already_rated')) ) {
				add_action( 'admin_init', [$this, 'shop_zen_check_theme_install_time'] );
			}
		}

		if ( is_admin() ) {
			add_action( 'admin_enqueue_scripts', [$this, 'shop_zen_enqueue_scripts'] );
		}

		add_action( 'wp_ajax_shop_zen_rating_dismiss_notice', [$this, 'shop_zen_rating_dismiss_notice'] );
		add_action( 'wp_ajax_shop_zen_rating_already_rated', [$this, 'shop_zen_rating_already_rated'] );
		add_action( 'wp_ajax_shop_zen_rating_maybe_later', [$this, 'shop_zen_rating_maybe_later'] );
	}

	public function shop_zen_check_theme_install_time() {   
		$install_date = get_option('shop_zen_activation_time');

		if ( false !== $install_date && $this->past_date >= $install_date ) {
			add_action( 'admin_notices', [$this, 'shop_zen_render_rating_notice' ]);
		}
	}

	public function shop_zen_rating_maybe_later() {
		check_ajax_referer( 'shop_zen_rating_nonce', 'nonce' );
		update_option('shop_zen_maybe_later_time', true);
		update_option('shop_zen_activation_time', strtotime('now'));
	}
	
	public function shop_zen_rating_dismiss_notice() {
		check_ajax_referer( 'shop_zen_rating_nonce', 'nonce' );
		update_option( 'shop_zen_rating_dismiss_notice', true );
	}

	public function shop_zen_rating_already_rated() {
		check_ajax_referer( 'shop_zen_rating_nonce', 'nonce' );
		update_option( 'shop_zen_rating_already_rated' , true );
	}

	public function shop_zen_render_rating_notice() {
		if ( is_admin() ) {

			echo '<div class="notice shop-zen-rating-notice is-dismissible" style="border-left-color: #0073aa!important; display: flex; align-items: center;">
						<div class="shop-zen-rating-notice-logo">
						<img class="shop-zen-logo" src="' . esc_url( get_theme_file_uri() . '/inc/activation/img/logo-spiracle.png' ) . '" alt="' . esc_attr__( 'Spiracle Themes', 'shop-zen' ) . '">
						</div>
						<div>
							<h3>' . esc_html__( 'Thank you for using Shop Zen WordPress Theme to build this website!', 'shop-zen' ) . '</h3>
							<p>' . esc_html__( 'Could you please do us a BIG favor and give it a 5-star rating on WordPress? Just to help us spread the word and boost our motivation.', 'shop-zen' ) . '</p>
							<p>
								<a href="https://wordpress.org/support/theme/shop-zen/reviews/?filter=5" target="_blank" class="shop-zen-you-deserve-it button button-primary">' . esc_html__( 'OK, you deserve it!', 'shop-zen' ) . '</a>
								<a class="shop-zen-maybe-later"><span class="dashicons dashicons-clock"></span> ' . esc_html__( 'Maybe Later', 'shop-zen' ) . '</a>
								<a class="shop-zen-already-rated"><span class="dashicons dashicons-yes"></span> ' . esc_html__( 'I Already did', 'shop-zen' ) . '</a>
							</p>
						</div>
				</div>';
		}
	}

	public function shop_zen_enqueue_scripts( $hook ) {
		// Only load on admin pages where the notice might appear.
		wp_enqueue_style(
			'shop-zen-rating-notice',
			get_template_directory_uri() . '/inc/activation/css/rating-notice.css',
			array(),
			wp_get_theme()->get( 'Version' )
		);

		wp_enqueue_script(
			'shop-zen-rating-notice',
			get_template_directory_uri() . '/inc/activation/js/rating-notice.js',
			array( 'jquery' ),
			wp_get_theme()->get( 'Version' ),
			true
		);

		wp_localize_script( 'shop-zen-rating-notice', 'shop_zen_rating', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'shop_zen_rating_nonce' ),
		) );
	}
}

new Shop_Zen_Rating_Notice();
