<?php

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


class ISTKR_ReviewController
{
    public static function istkr_is_review_delaying_expired()
    {
        $user_id = wp_get_current_user()->ID;

        return time() - ISTKR_Constants::WEEK_IN_SECONDS > (int) get_user_meta($user_id, 'istkr_next_schedule_review_notice_time', true);
    }

    public static function istkr_is_plugin_page()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '';

        return strpos($page, 'issues-tracker') !== false;
    }

    public static function istkr_ask_to_leave_review_handler()
    {
        if (!self::istkr_is_plugin_page()) {
            return;
        }

        $url = add_query_arg($_GET, admin_url('admin.php'));
        ?>

        <div class="alert alert-info review" role="alert">
            <a data-rate-action="do-rate" href="<?php echo esc_url(add_query_arg('istkr_review', 'do', $url)); ?>">
                <div class="d-flex justify-content-between">
                    <div>
                        <?php _e('It would help us a great deal if you could give us your feedback on WP plugin directory. We are hoping we earned your ', 'istkr'); ?>
                        <i class="fa-solid fa-star fa-fade fa-2sm"></i>
                        <i class="fa-solid fa-star fa-fade fa-2sm"></i>
                        <i class="fa-solid fa-star fa-fade fa-2sm"></i>
                        <i class="fa-solid fa-star fa-fade fa-2sm"></i>
                        <i class="fa-solid fa-star fa-fade fa-2sm"></i>
                    </div>

                    <span aria-hidden="true">
                        <a data-rate-action="later" href="<?php echo esc_url(add_query_arg('istkr_review', 'later', $url)); ?>">
                            <i class="fa fa-close"></i>
                        </a>
                    </span>
                </div>
            </a>
        </div>
    <?php }

    public static function istkr_review_handler()
    {
        $user_id = wp_get_current_user()->ID;

        if ($user_id && isset($_GET['istkr_review'])) {
            $is_next_schedule_review_time_exists = (bool) get_user_meta($user_id, 'istkr_next_schedule_review_notice_time');

            if ($_GET['istkr_review'] === 'later') {
                if ($is_next_schedule_review_time_exists) {
                    update_user_meta($user_id, 'istkr_next_schedule_review_notice_time', time() + ISTKR_Constants::TWO_MONTH_IN_SECONDS);
                } else {
                    add_user_meta($user_id, 'istkr_next_schedule_review_notice_time', time() + ISTKR_Constants::TWO_MONTH_IN_SECONDS, true);
                }
            } elseif ($_GET['istkr_review'] === 'do') {
                if ($is_next_schedule_review_time_exists) {
                    update_user_meta($user_id, 'istkr_next_schedule_review_notice_time', time() + ISTKR_Constants::SIX_MONTH_IN_SECONDS);
                } else {
                    add_user_meta($user_id, 'istkr_next_schedule_review_notice_time', time() + ISTKR_Constants::SIX_MONTH_IN_SECONDS, true);
                }
                wp_redirect('https://wordpress.org/support/plugin/issues-tracker/reviews/#new-post?filter=5');
            }
        }
    }
}
