<?php
/**
 * Admin Notice Management
 *
 * Handles suppression and display of admin notices on plugin pages.
 *
 * @package AIPT
 * @since 2.4.1
 */

namespace AIPT\Admin;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * NoticeManager Class
 *
 * Controls visibility of admin notices across plugin pages:
 * - Toolkit page: Shows only Freemius notices
 * - Other plugin pages: Hides all notices
 * - Non-plugin pages: Normal WordPress behavior
 */
class NoticeManager {
    /**
     * Singleton instance
     *
     * @var NoticeManager|null
     */
    private static $instance = null;

    /**
     * Get singleton instance
     *
     * @return NoticeManager
     */
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor
     *
     * Registers admin_head hook for CSS injection
     */
    private function __construct() {
        add_action('admin_head', [$this, 'hide_notices_on_plugin_pages'], 999);
    }

    /**
     * Check if current page is any AIPT plugin page
     *
     * @return bool True if on any plugin page
     */
    private function is_plugin_page(): bool {

        $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';

        if (empty($page)) {
            return false;
        }

        if ($page === 'ai-product-tools') {
            return true;
        }

        if (strpos($page, 'aipt-') === 0) {
            return true;
        }

        return false;
    }

    /**
     * Check if current page is specifically the Toolkit page
     *
     * @return bool True if on Toolkit main page
     */
    private function is_toolkit_page(): bool {

        $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';

        return $page === 'ai-product-tools';
    }

    /**
     * Inject CSS to hide admin notices on plugin pages
     *
     * Behavior:
     * - Toolkit page: Hide all notices except Freemius (.fs-notice)
     * - Other plugin pages: Hide ALL notices
     * - Non-plugin pages: No CSS injection (normal behavior)
     *
     * @return void
     */
    public function hide_notices_on_plugin_pages(): void {
        if (!$this->is_plugin_page()) {
            return;
        }

        if ($this->is_toolkit_page()) {

            ?>
            <style type="text/css">
                /* Hide all standard WordPress admin notices (including nested in .wrap) */
                #wpbody-content .notice:not(.fs-notice),
                #wpbody-content .updated:not(.fs-notice),
                #wpbody-content .error:not(.fs-notice),
                #wpbody-content .update-nag,
                #wpbody-content .notice-info:not(.fs-notice),
                #wpbody-content .notice-warning:not(.fs-notice),
                #wpbody-content .notice-success:not(.fs-notice),
                #wpbody-content .notice-error:not(.fs-notice) {
                    display: none !important;
                }

                /* Explicitly show only our plugin's Freemius notices with custom styling */
                #wpbody-content .fs-notice.fs-slug-ai-product-tools {
                    display: block !important;
                    position: relative !important;
                    box-sizing: border-box !important;
                    overflow: hidden !important;
                    word-wrap: break-word !important;
                    overflow-wrap: break-word !important;

                    /* Match plugin's card design */
                    background: linear-gradient(to bottom right, rgba(24, 24, 27, 0.9), rgba(24, 24, 27, 0.8), rgba(24, 24, 27, 0.9)) !important;
                    backdrop-filter: blur(4px) !important;
                    border: 1px solid rgba(39, 39, 42, 0.5) !important;
                    border-radius: 0.5rem !important;
                    padding: 1rem !important;
                }

                /* Remove default background colors */
                #wpbody-content .fs-notice.fs-slug-ai-product-tools.updated,
                #wpbody-content .fs-notice.fs-slug-ai-product-tools.success {
                    background: linear-gradient(to bottom right, rgba(24, 24, 27, 0.9), rgba(24, 24, 27, 0.8), rgba(24, 24, 27, 0.9)) !important;
                    border-left-color: rgba(39, 39, 42, 0.5) !important;
                }

                /* Freemius notice body text wrapping */
                #wpbody-content .fs-notice.fs-slug-ai-product-tools .fs-notice-body {
                    max-width: 100% !important;
                    word-wrap: break-word !important;
                    overflow-wrap: break-word !important;
                    white-space: normal !important;
                }

                /* Freemius notice long text handling */
                #wpbody-content .fs-notice.fs-slug-ai-product-tools p,
                #wpbody-content .fs-notice.fs-slug-ai-product-tools em,
                #wpbody-content .fs-notice.fs-slug-ai-product-tools strong,
                #wpbody-content .fs-notice.fs-slug-ai-product-tools a {
                    max-width: 100% !important;
                    word-wrap: break-word !important;
                    overflow-wrap: break-word !important;
                }

                /* Plugin title styling */
                #wpbody-content .fs-notice.fs-slug-ai-product-tools .fs-plugin-title {
                    color: rgba(228, 228, 231, 0.9) !important;
                }

                /* Close button styling */
                #wpbody-content .fs-notice.fs-slug-ai-product-tools .fs-close {
                    color: rgba(228, 228, 231, 0.7) !important;
                }

                #wpbody-content .fs-notice.fs-slug-ai-product-tools .fs-close:hover {
                    color: rgba(228, 228, 231, 1) !important;
                }
            </style>
            <?php
        } else {

            ?>
            <style type="text/css">
                /* Hide all admin notices on AIPT plugin pages (including nested in .wrap) */
                #wpbody-content .notice,
                #wpbody-content .updated,
                #wpbody-content .error,
                #wpbody-content .update-nag,
                #wpbody-content .notice-info,
                #wpbody-content .notice-warning,
                #wpbody-content .notice-success,
                #wpbody-content .notice-error,
                #wpbody-content .fs-notice {
                    display: none !important;
                }
            </style>
            <?php
        }
    }
}
