<?php

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

class Pawwell_Rating_Notice {
	private $past_date;

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

		if ( current_user_can('administrator') ) {
			if ( empty(get_option('pawwell_rating_dismiss_notice')) && empty(get_option('pawwell_rating_already_rated')) ) {
				add_action( 'admin_init', [$this, 'pawwell_check_theme_install_time'] );
			}
		}

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

		add_action( 'wp_ajax_pawwell_rating_dismiss_notice', [$this, 'pawwell_rating_dismiss_notice'] );
		add_action( 'wp_ajax_pawwell_rating_already_rated', [$this, 'pawwell_rating_already_rated'] );
		add_action( 'wp_ajax_pawwell_rating_maybe_later', [$this, 'pawwell_rating_maybe_later'] );
	}

	public function pawwell_check_theme_install_time() {   
		$install_date = get_option('pawwell_activation_time');

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

	public function pawwell_rating_maybe_later() {
		check_ajax_referer( 'pawwell_rating_nonce', 'nonce' );
		update_option('pawwell_maybe_later_time', true);
		update_option('pawwell_activation_time', strtotime('now'));
	}
	
	public function pawwell_rating_dismiss_notice() {
		check_ajax_referer( 'pawwell_rating_nonce', 'nonce' );
		update_option( 'pawwell_rating_dismiss_notice', true );
	}

	public function pawwell_rating_already_rated() {
		check_ajax_referer( 'pawwell_rating_nonce', 'nonce' );
		update_option( 'pawwell_rating_already_rated' , true );
	}

	public function pawwell_render_rating_notice() {
		if ( is_admin() ) {

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

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

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

		wp_localize_script( 'pawwell-rating-notice', 'pawwell_rating', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'pawwell_rating_nonce' ),
		) );
	}
}

new Pawwell_Rating_Notice();
