<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Template-scope variables and single-post admin meta reads, not globals or slow queries.
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Tab 3 - Toolbar & Features
 * Variables: $post, $meta, $schema, $global
 */

$p = TNC_FLIPBOOK_3D_PREFIX;
$is_pro = true; // WordPress.org build: toolbar customization is fully available.

// Pro-only toolbar items (7 items locked in Lite)
$pro_toolbar_items = array();

// Groups of toolbar items
$toolbar_groups = array(
    __( 'Navigation', 'tnc-flipbook-3d-lite' ) => array(
        'pageIndicator'=> __( 'Page Indicator',    'tnc-flipbook-3d-lite' ),
        'navigation'   => __( 'Prev/Next Arrows',  'tnc-flipbook-3d-lite' ),
        'firstLast'    => __( 'First/Last Arrows', 'tnc-flipbook-3d-lite' ),
        'singlePage'   => __( 'Single Page Flip',  'tnc-flipbook-3d-lite' ),
        'autoPlay'     => __( 'Auto-Play',         'tnc-flipbook-3d-lite' ),
    ),
    __( 'Content Tools', 'tnc-flipbook-3d-lite' ) => array(
        'search'       => __( 'Search',           'tnc-flipbook-3d-lite' ),
        'zoom'         => __( 'Zoom',             'tnc-flipbook-3d-lite' ),
        'thumbnails'   => __( 'Thumbnails',       'tnc-flipbook-3d-lite' ),
        'bookmarks'    => __( 'Bookmarks',        'tnc-flipbook-3d-lite' ),
    ),
    __( 'Actions', 'tnc-flipbook-3d-lite' ) => array(
        'share'        => __( 'Share',            'tnc-flipbook-3d-lite' ),
        'download'     => __( 'Download',         'tnc-flipbook-3d-lite' ),
        'print'        => __( 'Print',            'tnc-flipbook-3d-lite' ),
        'sound'        => __( 'Sound',            'tnc-flipbook-3d-lite' ),
        'fullscreen'   => __( 'Fullscreen',       'tnc-flipbook-3d-lite' ),
        'readingMode'  => __( 'Reading Mode',     'tnc-flipbook-3d-lite' ),
    ),
    __( 'Interface', 'tnc-flipbook-3d-lite' ) => array(
        'themeToggle'    => __( 'Theme Toggle',     'tnc-flipbook-3d-lite' ),
        'logo'           => __( 'Logo',             'tnc-flipbook-3d-lite' ),
    ),
);

// Helper
if ( ! function_exists( 'tncfb3d_field' ) ) {
    function tncfb3d_field( $meta, $global, $key, $fallback = '' ) {
        $override = ! empty( $meta[ $key . '_override' ] );
        if ( $override && isset( $meta[ $key ] ) ) { return $meta[ $key ]; }
        return isset( $global[ $key ] ) ? $global[ $key ] : $fallback;
    }
}

// Toolbar Layout (free: bottom-center only)
$key      = 'toolbarLayout';
$override = ! empty( $meta[ $key . '_override' ] );
$value    = tncfb3d_field( $meta, $global, $key, 'bottom-center' );
$args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Toolbar Layout', 'tnc-flipbook-3d-lite' ), 'tip' => 'toolbarLayout' );
include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
$tl_options = array(
    'bottom-center'    => array( 'label' => __( 'Bottom Center',    'tnc-flipbook-3d-lite' ), 'pro' => false ),
    'top-center'       => array( 'label' => __( 'Top Center',       'tnc-flipbook-3d-lite' ), 'pro' => false ),
    'bottom-fullwidth' => array( 'label' => __( 'Bottom Full Width','tnc-flipbook-3d-lite' ), 'pro' => false ),
    'top-fullwidth'    => array( 'label' => __( 'Top Full Width',   'tnc-flipbook-3d-lite' ), 'pro' => false ),
);
?>
<div class="tncfb-layout-cards">
    <?php foreach ( $tl_options as $tl_key => $tl_opt ) :
        $is_pro_locked = $tl_opt['pro'];
        $is_disabled   = ! $override || $is_pro_locked;
        ?>
        <label class="tncfb-layout-card <?php echo ( $value === $tl_key && ! $is_pro_locked ) ? 'is-active' : ''; ?> <?php echo $is_pro_locked ? 'is-pro-locked' : ''; ?>"
               data-value="<?php echo esc_attr( $tl_key ); ?>">
            <input type="radio"
                   name="<?php echo esc_attr( $p . $key ); ?>"
                   value="<?php echo esc_attr( $tl_key ); ?>"
                   <?php checked( $value, $tl_key ); ?>
                   <?php disabled( $is_disabled, true ); ?> />
            <?php TNC_Flipbook_3D_Helpers::render_toolbar_layout_svg( $tl_key ); ?>
            <span class="tncfb-layout-card-label">
                <?php echo esc_html( $tl_opt['label'] ); ?>
                <?php if ( $is_pro_locked ) : ?>
                    
                <?php endif; ?>
            </span>
        </label>
    <?php endforeach; ?>
</div>
<?php
?>
</div>
</div>

<?php
?>
<?php if ( $is_pro ) : ?>
<div class="tncfb-toolbar-guide">
    <p class="tncfb-toolbar-guide-text">
        <?php esc_html_e( 'Toolbar items use global Settings by default. Enable the section override toggle to customize items for this flipbook.', 'tnc-flipbook-3d-lite' ); ?>
    </p>
    <div class="tncfb-toolbar-guide-legend">
        <div class="tncfb-toolbar-guide-legend-item">
            <span class="tncfb-guide-toggle tncfb-guide-toggle-sm"></span>
            <span>
                <strong><?php esc_html_e( 'Section Override', 'tnc-flipbook-3d-lite' ); ?></strong>
                &mdash; <?php esc_html_e( 'customize all items in this section for this flipbook', 'tnc-flipbook-3d-lite' ); ?>
            </span>
        </div>
        <div class="tncfb-toolbar-guide-legend-item">
            <span class="tncfb-guide-toggle tncfb-guide-toggle-lg"></span>
            <span>
                <strong><?php esc_html_e( 'Show / Hide', 'tnc-flipbook-3d-lite' ); ?></strong>
                &mdash; <?php esc_html_e( 'enable or disable this item in the toolbar', 'tnc-flipbook-3d-lite' ); ?>
            </span>
        </div>
    </div>
</div>
<?php else : ?>
<div class="tncfb-toolbar-guide">
    <p class="tncfb-toolbar-guide-text">
        
    </p>
</div>
<?php endif; ?>
<?php
foreach ( $toolbar_groups as $group_label => $items ) :
    // Check if ANY non-locked item in this group has an override
    $section_override = false;
    foreach ( $items as $ik => $il ) {
        $is_locked = ! $is_pro && in_array( $ik, $pro_toolbar_items, true );
        if ( ! $is_locked && ! empty( $meta[ 'toolbar_' . $ik . '_override' ] ) ) {
            $section_override = true;
            break;
        }
    }
?>
    <div class="tncfb-toolbar-group">
        <div class="tncfb-group-heading-row">
            <h4 class="tncfb-group-heading"><?php echo esc_html( $group_label ); ?></h4>
            <?php if ( $is_pro ) : ?>
            <label class="tncfb-override-toggle tncfb-override-toggle-sm tncfb-section-override"
                   title="<?php esc_attr_e( 'Override all items in this section', 'tnc-flipbook-3d-lite' ); ?>">
                <span class="tncfb-section-override-label"><?php echo $section_override ? esc_html__( 'Custom', 'tnc-flipbook-3d-lite' ) : esc_html__( 'Global', 'tnc-flipbook-3d-lite' ); ?></span>
                <input type="checkbox"
                       class="tncfb-section-override-checkbox"
                       value="1"
                       <?php checked( $section_override, true ); ?> />
                <span class="tncfb-switcher-slider tncfb-switcher-xs"></span>
            </label>
            <?php endif; ?>
        </div>
        <div class="tncfb-toolbar-items">
            <?php foreach ( $items as $item_key => $item_label ) :
                // Lite: toolbar item visibility is a Pro-only control — every
                // item is locked (shown with a disabled toggle + Pro badge).
                $is_locked_item = ! $is_pro;
                $meta_key = $p . 'toolbar_' . $item_key;
                $override = $section_override && ! $is_locked_item;
                $value    = $override && isset( $meta[ 'toolbar_' . $item_key ] )
                    ? (bool) $meta[ 'toolbar_' . $item_key ]
                    : ( isset( $global[ 'toolbar_' . $item_key ] ) ? (bool) $global[ 'toolbar_' . $item_key ] : true );

                // For a locked item, show the actual default the viewer uses:
                // Pro-only items are off in Lite; the rest follow the schema default.
                if ( $is_locked_item ) {
                    $value = in_array( $item_key, $pro_toolbar_items, true )
                        ? false
                        : ( isset( $schema[ $meta_key ]['default'] ) ? ! empty( $schema[ $meta_key ]['default'] ) : true );
                }
            ?>
                <div class="tncfb-toolbar-item<?php echo $is_locked_item ? ' tncfb-pro-locked' : ''; ?>">
                    <div class="tncfb-toolbar-item-header">
                        <span class="tncfb-toolbar-item-label">
                            <?php echo esc_html( $item_label ); ?>
                            <?php if ( $is_locked_item ) : ?>
                                
                            <?php endif; ?>
                        </span>
                    </div>
                    <?php if ( ! $is_locked_item ) : ?>
                        <input type="hidden"
                               class="tncfb-item-override-hidden"
                               name="<?php echo esc_attr( $meta_key ); ?>_override"
                               value="<?php echo $override ? '1' : ''; ?>" />
                        <?php
                        $args = array(
                            'id'       => $meta_key,
                            'value'    => $value,
                            'disabled' => ! $override,
                        );
                        include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-switcher.php';
                    else :
                        // Visual-only disabled toggle: empty id => no name attr =>
                        // never submitted, so the shared settings option stays clean.
                        $args = array(
                            'id'       => '',
                            'value'    => $value,
                            'disabled' => true,
                        );
                        include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-switcher.php';
                    endif; ?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
<?php endforeach; ?>
