<?php

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

class ST_Seo_Agency_Rating_Notice {
    private $past_date;

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

        if ( current_user_can('administrator') ) {
            if ( empty(get_option('st_seo_agency_rating_dismiss_notice')) && empty(get_option('st_seo_agency_rating_already_rated')) ) {
                add_action( 'admin_init', [$this, 'st_seo_agency_check_theme_install_time'] );
            }
        }

        // if ( is_admin() ) {
        //     add_action( 'admin_head', [$this, 'st_seo_agency_enqueue_scripts' ] );
        // }
		add_action( 'admin_enqueue_scripts', [$this, 'st_seo_agency_enqueue_scripts'], 5 );

        add_action( 'wp_ajax_st_seo_agency_rating_dismiss_notice', [$this, 'st_seo_agency_rating_dismiss_notice'] );
        add_action( 'wp_ajax_st_seo_agency_rating_already_rated', [$this, 'st_seo_agency_rating_already_rated'] );
        add_action( 'wp_ajax_st_seo_agency_rating_maybe_later', [$this, 'st_seo_agency_rating_maybe_later'] );
    }

    public function st_seo_agency_check_theme_install_time() {   
        $st_seo_agency_install_date = get_option('st_seo_agency_activation_time');

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

    public function st_seo_agency_rating_maybe_later() {
        update_option('st_seo_agency_maybe_later_time', true);
        update_option('st_seo_agency_activation_time', strtotime('now'));
    }
    
    public function st_seo_agency_rating_dismiss_notice() {
        update_option( 'st_seo_agency_rating_dismiss_notice', true );
    }

    function st_seo_agency_rating_already_rated() {    
        update_option( 'st_seo_agency_rating_already_rated' , true );
    }

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

    public function st_seo_agency_enqueue_scripts() { 
		// Enqueue Scripts
        wp_enqueue_script( 'st-seo-agency-rating-notic-js', get_template_directory_uri() . '/inc/activation/js/rating-notice.js', ['jquery'], false, true );
    }
}

new ST_Seo_Agency_Rating_Notice();