<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Renders a consistent admin topbar for all Sifency plugin pages.
 *
 * This class is designed to be reusable. To use it, simply instantiate it
 * and call the render() method, passing the slug of the current active page.
 *
 * Example Usage:
 * $topbar = new Sifency_Admin_Topbar();
 * $topbar->render( 'theme-builder' );
 */
final class Sifency_Addons_Admin_Topbar {

    /**
     * An array holding all the navigation menu items.
     * @var array
     */
    private $menu_items = [];

    /**
     * An array holding all the info link items.
     * @var array
     */
    private $info_links = [];

    /**
     * The slug of the currently active menu item.
     * @var string
     */
    private $active_slug = '';

    /**
     * Constructor. Populates the menu items and info links.
     */
    public function __construct() {
        $this->setup_menu_items();
        $this->setup_info_links();
    }

    /**
     * Defines the primary navigation menu items for the topbar.
     * Each item is an array with 'slug', 'label', 'url', and 'icon'.
     */
    private function setup_menu_items() {
            $base_page_slug = 'sifency-theme-builder';

            // Base menu items (default)
            $this->menu_items = [
                'setup' => [
                    'label' => __( 'Dashboard', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-addons' ),
                    'icon'  => 'dashicons-admin-tools',
                ],
                'widgets' => [
                    'label' => __( 'Widgets', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-addons-widgets' ),
                    'icon'  => 'dashicons-screenoptions',
                ],
                'settings' => [
                    'label' => __( 'Settings', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-settings' ),
                    'icon'  => 'dashicons-admin-generic',
                ],
                'theme-builder' => [
                    'label' => __( 'Theme Builder', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=' . $base_page_slug ),
                    'icon'  => 'dashicons-admin-customizer',
                ],
                'template-maker' => [
                    'label' => __( 'Template Maker', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-templates-shortcode' ),
                    'icon'  => 'dashicons-layout',
                ],
                'prebuild-website' => [
                    'label' => __( 'Prebuild Website', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-prebuild-website' ),
                    'icon'  => 'dashicons-admin-site-alt3',
                ],
                'extentions' => [
                    'label' => __( 'Plugins', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-extentions' ),
                    'icon'  => 'dashicons-admin-plugins',
                ],
                'themes' => [
                    'label' => __( 'Theme', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-themes' ),
                    'icon'  => 'dashicons-admin-appearance',
                ],
                'PRO' => [
                    'label' => __( 'Why Pro', 'sifency-addons' ),
                    'url'   => admin_url( 'admin.php?page=sifency-why-pro' ),
                    'icon'  => 'dashicons-info',
                ],
            ];

            /**
             * ------------------------------------------------------------------
             * Remove specific menu items if Sifency_Core exists
             * ------------------------------------------------------------------
             */
            if ( class_exists( 'Sifency_Core' ) ) {
                unset( $this->menu_items['template-maker'] );
                unset( $this->menu_items['themes'] );
                unset( $this->menu_items['prebuild-website'] );
                unset( $this->menu_items['PRO'] );
            }
    }


    /**
     * Defines the secondary info links for the topbar.
     */
    private function setup_info_links() {
        $this->info_links = [
            'features' => [
                'label' => __( '✨ New Features', 'sifency-addons' ),
                'url'   => 'https://sifency-addons.sifency.com/new-features',
                'target'=> '_blank',
            ],
            'docs' => [
                'label' => __( '📘 Docs', 'sifency-addons' ),
                'url'   => 'https://sifency-addons.sifency.com/doc',
                'target'=> '_blank',
            ],
            'support' => [
                'label' => __( '💬 Support', 'sifency-addons' ),
                'url'   => 'https://wordpress.org/support/plugin/sifency-addons',
                'target'=> '_blank',
            ],
        ];
    }

    /**
     * Renders the complete HTML for the topbar.
     *
     * @param string $active_slug The unique slug of the current page's menu item.
     */
    public function render( $active_slug = '' ) {
        $this->active_slug = $active_slug;

        $logo_url = defined('sifency_addons_URL') ? sifency_addons_URL . 'assets/images/logo.svg' : '';
        $version = defined('sifency_addons_VERSION') ? sifency_addons_VERSION : '1.0';
        $pro_link = 'https://sifency-addons.sifency.com';
        ?>
        <div class="sifency-admin-topbar">
            <div class="sifency-topbar-inner">
                <div class="sifency-topbar-left">
                    <div class="sifency-admin-header">
                        <div class="sifency-header-left">
                            <div class="sifency-logo">
                                <img src="<?php echo esc_url( SIFENCY_ADDONS_URL . 'assets/images/logo.png' ); ?>"
                                    alt="<?php echo esc_attr__( 'Sifency Logo', 'sifency-addons' ); ?>">
                            </div>
                            <h1 class="sifency-title"><?php esc_html_e( 'Sifency Addons', 'sifency-addons' ); ?></h1>
                        </div>
                        <div class="sifency-header-right">
                            <span class="sifency-version">
                                <?php
                                        printf(
                                            /* translators: %s: Plugin version number. */
                                            esc_html__( 'Version %s', 'sifency-addons' ),
                                            esc_html( SIFENCY_ADDONS_VERSION )
                                        );
                                    ?>
                            </span>
                        </div>
                    </div>
                    <?php if ($logo_url): ?>
                    <img src="<?php echo esc_url( $logo_url ); ?>" alt="Sifency" class="sifency-topbar-logo">
                    <?php endif; ?>
                </div>

                <div class="sifency-topbar-right">
                    <div class="sifency-topbar-info">
                        <?php foreach ( $this->info_links as $link ) : ?>
                        <a href="<?php echo esc_url( $link['url'] ); ?>" class="sifency-info-link sifency-admin-btn"
                            <?php if ( ! empty($link['target']) ) echo 'target="' . esc_attr($link['target']) . '" rel="noopener"'; ?>>
                            <?php echo esc_html( $link['label'] ); ?>
                        </a>
                        <?php endforeach; ?>

                        <?php if ( ! Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() ) : ?>
                        <a href="<?php echo esc_url( $pro_link ); ?>" class="sifency-upgrade-btn sifency-admin-btn"
                            target="_blank" rel="noopener">
                            <?php esc_html_e( 'Upgrade to Pro', 'sifency-addons' ); ?>
                        </a>
                        <?php endif; ?>

                    </div>

                    <nav class="sifency-topbar-menu">
                        <?php foreach ( $this->menu_items as $slug => $item ) : ?>
                        <?php $is_active = ( $slug === $this->active_slug ) ? ' is-active' : ''; ?>
                        <a href="<?php echo esc_url( $item['url'] ); ?>"
                            class="sifency-admin-btn menu-item<?php echo esc_attr( $is_active ); ?>">
                            <?php if ( ! empty( $item['icon'] ) ) : ?>
                            <span class="dashicons <?php echo esc_attr( $item['icon'] ); ?>"></span>
                            <?php endif; ?>
                            <span class="menu-label"><?php echo esc_html( $item['label'] ); ?></span>
                        </a>
                        <?php endforeach; ?>
                    </nav>

                </div>
            </div>
        </div>
        <button id="sifency-toggle-fullwidth" class="sifency-fullscreen-btn">
            <span class="icon-expand">⤢</span>
            <span class="icon-compress">⤡</span>
        </button>
        <?php
    }
}