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

/**
 * Shared admin UI helpers for settings pages.
 */
trait NNAXFUF_Settings_Admin_UI_Trait {
    private function get_admin_sections() {
        return array(
            'general' => array(
                'label' => __( 'General', 'nnax-frontend-user-files' ),
                'url'   => admin_url( 'admin.php?page=nnax-user-files-general' ),
            ),
            'design' => array(
                'label' => __( 'Design', 'nnax-frontend-user-files' ),
                'url'   => admin_url( 'admin.php?page=nnax-user-files-design' ),
            ),
            'upload' => array(
                'label' => __( 'Upload', 'nnax-frontend-user-files' ),
                'url'   => admin_url( 'admin.php?page=nnax-user-files-upload' ),
            ),
            'sharing' => array(
                'label' => __( 'Sharing', 'nnax-frontend-user-files' ),
                'url'   => admin_url( 'admin.php?page=nnax-user-files-sharing' ),
            ),
            'licenses' => array(
                'label' => __( 'Licenses', 'nnax-frontend-user-files' ),
                'url'   => admin_url( 'admin.php?page=nnax-user-files-licenses' ),
            ),
        );
    }

    private function render_page_header( $title, $subtitle = '', $active_section = 'general' ) {
        $sections = $this->get_admin_sections();
        $logo_icon_base64 = NNAXFUF_Helpers::get_img_base64( NNAXFUF_PLUGIN_PATH . 'assets/images/logoicon-base64.txt' );
        ?>
        <div class="wrap nnaxadmin-wrap nnax-container nnax-admin nnax-user-files-page">
            <h1 class="nnax-page-title"><?php echo esc_html( $title ); ?></h1>

            <div class="nnax-header">
                <div class="nnax-header-left">
                    <h2><?php echo esc_html( $title ); ?></h2>
                    <?php if ( '' !== $subtitle ) : ?>
                        <h3><?php echo esc_html( $subtitle ); ?></h3>
                    <?php endif; ?>
                    <nav class="nnax-nav" aria-label="<?php echo esc_attr__( 'nnax Settings', 'nnax-frontend-user-files' ); ?>">
                        <?php foreach ( $sections as $section_key => $section ) : ?>
                            <?php $classes = 'nnax-btn nnax-btn--outline nnax-btn--small'; ?>
                            <?php if ( $section_key === $active_section ) : ?>
                                <?php $classes = 'nnax-btn nnax-btn--secondary nnax-btn--small'; ?>
                            <?php endif; ?>
                            <?php 
                            $label_html = esc_html( $section['label'] );
                            if ( $section_key === 'sharing' ) {
                                $is_sharing_unlocked = false;
                                if ( class_exists( 'NNAXFUF_Sharing_Addon_Admin' ) ) {
                                    $license_manager = new NNAXFUF_License_Manager();
                                    if ( $license_manager->is_product_active( 'nnax-frontend-user-files-sharing-addon' ) ) {
                                        $is_sharing_unlocked = true;
                                    }
                                }
                                if ( ! $is_sharing_unlocked ) {
                                    $label_html = '<span class="dashicons dashicons-lock" style="font-size: 14px; width: 14px; height: 14px; line-height: 14px;"></span>' . $label_html;
                                }
                            }
                            ?>
                            <a class="<?php echo esc_attr( $classes ); ?>" href="<?php echo esc_url( $section['url'] ); ?>"<?php echo $section_key === $active_section ? ' aria-current="page"' : ''; ?>>
                                <?php echo $label_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML generated safely in loop with esc_html fallback. ?>
                            </a>
                        <?php endforeach; ?>
                    </nav>
                </div>
                <div class="nnax-header-right">
                    <?php if ( $logo_icon_base64 !== '' ) : ?>
                        <a href="https://www.nnax.de" target="_blank" rel="noopener noreferrer">
                            <img class="adminlogo-base64" title="<?php echo esc_attr__( 'nnax Icon', 'nnax-frontend-user-files' ); ?>" alt="<?php echo esc_attr__( 'nnax Icon', 'nnax-frontend-user-files' ); ?>" src="data:image/png;base64,<?php echo esc_attr( $logo_icon_base64 ); ?>" />
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        <?php
    }

    private function render_page_footer() {
        echo '</div>';
    }

    private function render_card_start( $title, $description = '', $card_class = '' ) {
        $classes = 'nnax-card';
        if ( is_string( $card_class ) && $card_class !== '' ) {
            $classes .= ' ' . trim( $card_class );
        }
        ?>
        <section class="<?php echo esc_attr( $classes ); ?>">
            <h2><?php echo esc_html( $title ); ?></h2>
            <?php if ( $description !== '' ) : ?>
                <p><?php echo esc_html( $description ); ?></p>
            <?php endif; ?>
        <?php
    }

    private function render_card_end() {
        echo '</section>';
    }
}
