<?php
/**
 * Adds a "Text to Speech" quick-access menu to the WordPress admin bar
 * (top toolbar, visible on both backend and frontend).
 *
 * Only rendered for users with `manage_options`. The menu is always shown
 * regardless of SaaS connection status — disconnected users still benefit
 * from quick access to Overview (to connect) and Documentation.
 *
 * Extracted from Mementor_TTS_Admin to keep that class focused.
 *
 * @package TTSWP
 */

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

class Mementor_TTS_Admin_Bar {

    /**
     * Register hooks. Called once from Mementor_TTS_Admin::__construct.
     */
    public function register_hooks() {
        add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu' ), 80 );
        // Print the Upgrade-link styling on backend + frontend.
        add_action( 'admin_head', array( $this, 'print_menu_css' ) );
        add_action( 'wp_head',    array( $this, 'print_menu_css' ) );
    }

    /**
     * Print styling for the Upgrade CTA menu item so it stands out from
     * other submenu links (purple + bold, hover flips to white on purple).
     */
    public function print_menu_css() {
        if ( ! is_admin_bar_showing() || ! current_user_can( 'manage_options' ) ) {
            return;
        }
        ?>
<style id="mementor-tts-admin-bar-css">
#wpadminbar #wp-admin-bar-mementor-tts > .ab-item > img.mementor-tts-ab-icon {
    width: 18px;
    height: 18px;
    vertical-align: middle;
    margin: -3px 6px 0 0;
    border-radius: 3px;
}
#wpadminbar #wp-admin-bar-mementor-tts-upgrade > .ab-item,
#wpadminbar #wp-admin-bar-mementor-tts-upgrade > .ab-item:hover {
    color: #c084fc;
    font-weight: 700;
}
#wpadminbar #wp-admin-bar-mementor-tts-upgrade:hover > .ab-item {
    color: #fff;
    background-color: #6B45D4;
}
</style>
        <?php
    }

    /**
     * Build the admin bar menu node and its children.
     *
     * @param WP_Admin_Bar $wp_admin_bar
     */
    public function add_admin_bar_menu( $wp_admin_bar ) {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        // Icon rendered as an inline <img> inside the title — more reliable than
        // a ::before pseudo-element given the admin bar's default `.ab-icon`
        // dashicon slot interferes with background-image approaches.
        $icon_url  = MEMENTOR_TTS_PLUGIN_URL . 'admin/images/ttswp-icon.svg';
        $icon_html = '<img class="mementor-tts-ab-icon" src="' . esc_url( $icon_url ) . '" alt="" width="18" height="18" />';

        $wp_admin_bar->add_node( array(
            'id'    => 'mementor-tts',
            'title' => $icon_html . esc_html__( 'Text to Speech', 'text-to-speech-tts' ),
            'href'  => admin_url( 'admin.php?page=text-to-speech-tts-settings' ),
        ) );

        $children = array(
            array(
                'id'    => 'mementor-tts-voices',
                'title' => esc_html__( 'Language & Voices', 'text-to-speech-tts' ),
                'href'  => admin_url( 'admin.php?page=text-to-speech-tts-voices' ),
            ),
            array(
                'id'    => 'mementor-tts-player',
                'title' => esc_html__( 'Customize Audio Player', 'text-to-speech-tts' ),
                'href'  => admin_url( 'admin.php?page=text-to-speech-tts-player' ),
            ),
            array(
                'id'    => 'mementor-tts-generator',
                'title' => esc_html__( 'Shortcode Generator', 'text-to-speech-tts' ),
                'href'  => admin_url( 'admin.php?page=text-to-speech-tts-generator' ),
            ),
            array(
                'id'    => 'mementor-tts-statistics',
                'title' => esc_html__( 'Audio Statistics', 'text-to-speech-tts' ),
                'href'  => admin_url( 'admin.php?page=text-to-speech-tts-statistics' ),
            ),
            array(
                'id'    => 'mementor-tts-account',
                'title' => esc_html__( 'My Plan & Credits', 'text-to-speech-tts' ),
                'href'  => admin_url( 'admin.php?page=text-to-speech-tts-account' ),
            ),
            array(
                'id'    => 'mementor-tts-documentation',
                'title' => esc_html__( 'Documentation', 'text-to-speech-tts' ),
                'href'  => 'https://ttswp.com/docs',
                'meta'  => array(
                    'target' => '_blank',
                    'rel'    => 'noopener',
                ),
            ),
        );

        foreach ( $children as $child ) {
            $child['parent'] = 'mementor-tts';
            $wp_admin_bar->add_node( $child );
        }

        // Upgrade CTA — only for connected free users. Disconnected users see the
        // Connect flow via the onboarding banner; paid users don't need an upsell.
        $is_connected = ( 'connected' === Mementor_TTS_Connect::get_connection_status() );
        if ( $is_connected && function_exists( 'ttswp_has_paid_plan' ) && ! ttswp_has_paid_plan() ) {
            $wp_admin_bar->add_node( array(
                'id'     => 'mementor-tts-upgrade',
                'parent' => 'mementor-tts',
                'title'  => esc_html__( 'Upgrade to PRO', 'text-to-speech-tts' ),
                'href'   => admin_url( 'admin.php?page=text-to-speech-tts-account#upgrade' ),
                'meta'   => array(
                    'class' => 'mementor-tts-upgrade-menu',
                ),
            ) );
        }
    }
}
