<?php

declare(strict_types=1);

namespace FleekDash\Traits;

/**
 * Trait RemoveAdminBar
 *
 * Provides functionality to completely remove the WordPress admin bar from specific pages
 * without flash/flicker on page load and without leaving empty space.
 *
 * Usage:
 * 1. Use this trait in your class
 * 2. Call $this->remove_admin_bar_on_page() in your bootstrap method
 *
 * @package FleekDash\Traits
 */
trait RemoveAdminBar
{
    /**
     * Remove admin bar from current page
     *
     * This method:
     * - Disables admin bar completely
     * - Removes the space it occupies
     * - Prevents flash on page load with inline CSS
     * - Adds body class for additional styling control
     *
     * @return void
     */
    public function remove_admin_bar_on_page(): void
    {
        // Disable admin bar for this page
        \add_filter('show_admin_bar', '__return_false');

        // Remove admin bar CSS and inline styles
        \add_action('wp_enqueue_scripts', [$this, 'remove_admin_bar_assets'], 999);

        // Add inline CSS to prevent flash and remove space
        \add_action('wp_head', [$this, 'add_admin_bar_removal_css'], 1);

        // Add body class for additional control
        \add_filter('body_class', [$this, 'add_no_admin_bar_class']);

        // Remove admin bar initialization
        \remove_action('init', '_wp_admin_bar_init');
        \remove_action('wp_footer', 'wp_admin_bar_render', 1000);
    }

    /**
     * Remove admin bar CSS and scripts
     *
     * @return void
     */
    public function remove_admin_bar_assets(): void
    {
        // Dequeue admin bar styles
        wp_dequeue_style('admin-bar');
        wp_deregister_style('admin-bar');

        // Dequeue admin bar scripts
        wp_dequeue_script('admin-bar');
        wp_deregister_script('admin-bar');

        // Remove dashicons if not needed
        wp_dequeue_style('dashicons');
    }

    /**
     * Add inline CSS to prevent flash and remove admin bar space
     * This runs very early in wp_head to prevent any flash
     *
     * @return void
     */
    public function add_admin_bar_removal_css(): void
    {
        ?>
        <style id="fleekdash-remove-admin-bar">
            /* Prevent admin bar flash and remove space */
            html,
            html.admin-bar {
                margin-top: 0 !important;
                padding-top: 0 !important;
            }

            * html body {
                margin-top: 0 !important;
            }

            body.admin-bar,
            body {
                margin-top: 0 !important;
                padding-top: 0 !important;
            }

            #wpadminbar {
                display: none !important;
                visibility: hidden !important;
                opacity: 0 !important;
                height: 0 !important;
                min-height: 0 !important;
                max-height: 0 !important;
                position: absolute !important;
                top: -999999px !important;
                overflow: hidden !important;
            }

            /* Ensure no space is reserved */
            body.no-admin-bar {
                margin-top: 0 !important;
                padding-top: 0 !important;
            }

            /* Remove admin bar space for all elements */
            html.admin-bar body,
            html body.admin-bar {
                margin-top: 0 !important;
                padding-top: 0 !important;
            }
        </style>
        <?php
    }

    /**
     * Add body class for no admin bar
     *
     * @param array $classes Existing body classes
     * @return array Modified body classes
     */
    public function add_no_admin_bar_class($classes): array
    {
        $classes[] = 'no-admin-bar';

        // Remove admin-bar class if present
        $classes = array_diff($classes, ['admin-bar']);

        return $classes;
    }
}
