<?php

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

class Shopbar_Rating_Notice {
	private $past_date;

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

		if ( current_user_can('administrator') ) {
			if ( empty(get_option('shopbar_rating_dismiss_notice')) && empty(get_option('shopbar_rating_already_rated')) ) {
				add_action( 'admin_init', [$this, 'shopbar_check_theme_install_time'] );
			}
		}

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

		add_action( 'wp_ajax_shopbar_rating_dismiss_notice', [$this, 'shopbar_rating_dismiss_notice'] );
		add_action( 'wp_ajax_shopbar_rating_already_rated', [$this, 'shopbar_rating_already_rated'] );
		add_action( 'wp_ajax_shopbar_rating_maybe_later', [$this, 'shopbar_rating_maybe_later'] );
	}

	public function shopbar_check_theme_install_time() {   
		$install_date = get_option('shopbar_activation_time');

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

	public function shopbar_rating_maybe_later() {
		check_ajax_referer( 'shopbar_rating_nonce', 'nonce' );
		update_option('shopbar_maybe_later_time', true);
		update_option('shopbar_activation_time', strtotime('now'));
	}
	
	public function shopbar_rating_dismiss_notice() {
		check_ajax_referer( 'shopbar_rating_nonce', 'nonce' );
		update_option( 'shopbar_rating_dismiss_notice', true );
	}

	public function shopbar_rating_already_rated() {
		check_ajax_referer( 'shopbar_rating_nonce', 'nonce' );
		update_option( 'shopbar_rating_already_rated' , true );
	}

	public function shopbar_render_rating_notice() {
		if ( is_admin() ) {

			echo '<div class="notice shopbar-rating-notice is-dismissible" style="border-left-color: #0073aa!important; display: flex; align-items: center;">
						<div class="shopbar-rating-notice-logo">
						<img class="shopbar-logo" src="' . esc_url( get_theme_file_uri() . '/inc/activation/img/logo-spiracle.png' ) . '" alt="' . esc_attr__( 'Spiracle Themes', 'shopbar' ) . '">
						</div>
						<div>
							<h3>' . esc_html__( 'Thank you for using shopbar WordPress Theme to build this website!', 'shopbar' ) . '</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.', 'shopbar' ) . '</p>
							<p>
								<a href="https://wordpress.org/support/theme/shopbar/reviews/?filter=5" target="_blank" class="shopbar-you-deserve-it button button-primary">' . esc_html__( 'OK, you deserve it!', 'shopbar' ) . '</a>
								<a class="shopbar-maybe-later"><span class="dashicons dashicons-clock"></span> ' . esc_html__( 'Maybe Later', 'shopbar' ) . '</a>
								<a class="shopbar-already-rated"><span class="dashicons dashicons-yes"></span> ' . esc_html__( 'I Already did', 'shopbar' ) . '</a>
							</p>
						</div>
				</div>';
		}
	}

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

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

		wp_localize_script( 'shopbar-rating-notice', 'shopbar_rating', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'shopbar_rating_nonce' ),
		) );
	}
}

new Shopbar_Rating_Notice();
