<?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 1 - General
 * Variables: $post, $meta, $schema, $global
 *
 * Layout:
 *   Page Layout: Default View Mode, Page Mode, Default Zoom, Force Single Page Mobile
 *   Behavior:    Language, Deep Linking, Link Target, PDF Link Overlay
 */

$p = TNC_FLIPBOOK_3D_PREFIX;

// Helper: get effective value (override check)
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 ];
        }
        if ( isset( $global[ $key ] ) ) {
            return $global[ $key ];
        }
        return $fallback;
    }
}
?>
<div class="tncfb-general-tab">

    <!-- ═══════════════════════════════════════════
         PAGE LAYOUT
         ═══════════════════════════════════════════ -->

    <!-- Default View Mode -->
    <?php
    $key      = 'defaultViewMode';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = ( $override && isset( $meta[ $key ] ) ) ? $meta[ $key ] : ( $global[ $key ] ?? 'flipbook' );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Default View Mode', 'tnc-flipbook-3d-lite' ), 'tip' => 'defaultViewMode' );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
    $vm_options = array(
        'flipbook'   => __( 'FlipBook (3D/2D Page Flip)',     'tnc-flipbook-3d-lite' ),
        'single'     => __( 'Single Page Flip',               'tnc-flipbook-3d-lite' ),
        'vertical'   => __( 'Vertical Scroll (PDF Viewer)',   'tnc-flipbook-3d-lite' ),
        'horizontal' => __( 'Horizontal Scroll (PDF Viewer)', 'tnc-flipbook-3d-lite' ),
    );
    ?>
    <div class="tncfb-layout-cards">
        <?php foreach ( $vm_options as $vm_key => $vm_label ) : ?>
            <label class="tncfb-layout-card <?php echo $value === $vm_key ? 'is-active' : ''; ?>"
                   data-value="<?php echo esc_attr( $vm_key ); ?>">
                <input type="radio"
                       name="<?php echo esc_attr( $p . $key ); ?>"
                       value="<?php echo esc_attr( $vm_key ); ?>"
                       <?php checked( $value, $vm_key ); ?>
                       <?php disabled( ! $override, true ); ?> />
                <?php TNC_Flipbook_3D_Helpers::render_view_mode_svg( $vm_key ); ?>
                <span class="tncfb-layout-card-label"><?php echo esc_html( $vm_label ); ?></span>
            </label>
        <?php endforeach; ?>
    </div>
    <p class="description"><?php esc_html_e( 'The view mode shown when the FlipBook first loads.', 'tnc-flipbook-3d-lite' ); ?></p>
    </div>
    </div>

    <!-- Page Mode -->
    <?php
    $key      = 'pageMode';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = ( $override && isset( $meta[ $key ] ) ) ? $meta[ $key ] : ( $global[ $key ] ?? 'odd' );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Page Mode', 'tnc-flipbook-3d-lite' ), 'tip' => 'pageMode' );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
    $args = array(
        'id'      => $p . $key,
        'value'   => $value,
        'options' => array(
            'odd'  => __( 'Cover First (1, 2-3, 4-5...)', 'tnc-flipbook-3d-lite' ),
            'even' => __( 'Spreads First (1-2, 3-4, 5-6...)', 'tnc-flipbook-3d-lite' ),
        ),
        'description' => __( 'How pages are paired into spreads. Cover First shows page 1 alone as a cover.', 'tnc-flipbook-3d-lite' ),
        'disabled' => ! $override,
    );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-select.php';
    ?>
    </div>
    </div>

    <!-- Default Zoom -->
    <?php
    $key      = 'defaultZoom';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = ( $override && isset( $meta[ $key ] ) ) ? $meta[ $key ] : ( $global[ $key ] ?? 'auto' );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Default Zoom', 'tnc-flipbook-3d-lite' ), 'tip' => 'defaultZoom' );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
    $args = array(
        'id'      => $p . $key,
        'value'   => $value,
        'options' => array(
            'auto'       => __( 'Automatic Zoom', 'tnc-flipbook-3d-lite' ),
            'page-fit'   => __( 'Page Fit', 'tnc-flipbook-3d-lite' ),
            'page-width' => __( 'Page Width', 'tnc-flipbook-3d-lite' ),
            '50'         => '50%',
            '75'         => '75%',
            '100'        => '100%',
            '125'        => '125%',
            '150'        => '150%',
            '200'        => '200%',
            '250'        => '250%',
            '300'        => '300%',
            '400'        => '400%',
        ),
        'description' => __( 'The zoom level when the FlipBook first loads.', 'tnc-flipbook-3d-lite' ),
        'disabled' => ! $override,
    );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-select.php';
    ?>
    </div>
    </div>

    <!-- Force Single Page on Mobile -->
    <?php
    $key      = 'forceSinglePageMobile';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = tncfb3d_field( $meta, $global, $key, true );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Mobile Single Page', 'tnc-flipbook-3d-lite' ), 'tip' => 'forceSinglePageMobile' );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
    $args = array(
        'id'          => $p . $key,
        'label'       => __( 'Force single page mode on mobile', 'tnc-flipbook-3d-lite' ),
        'value'       => ! empty( $value ),
        'description' => __( 'Automatically switch to single page view on screens narrower than 768px.', 'tnc-flipbook-3d-lite' ),
        'disabled'    => ! $override,
    );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-switcher.php';
    ?>
    </div>
    </div>

    <!-- ═══════════════════════════════════════════
         BEHAVIOR
         ═══════════════════════════════════════════ -->

    <!-- Language -->
    <?php
    $key      = 'language';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = ( $override && isset( $meta[ $key ] ) ) ? $meta[ $key ] : ( $global[ $key ] ?? 'en' );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Language', 'tnc-flipbook-3d-lite' ), 'tip' => 'language' );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';

    $lang_groups = TNC_Flipbook_3D_Helpers::get_languages();
    $flat_options = array();
    foreach ( $lang_groups as $langs ) {
        foreach ( $langs as $code => $name ) {
            $flat_options[ $code ] = $name . ' (' . $code . ')';
        }
    }
    $args = array(
        'id'          => $p . $key,
        'value'       => $value,
        'options'     => $flat_options,
        'description' => __( 'Language for all flipbook UI text.', 'tnc-flipbook-3d-lite' ),
        'disabled'    => ! $override,
    );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-select.php';
    ?>
    </div>
    </div>

    <!-- Deep Linking -->
    <?php
    $key      = 'deepLinking';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = $override && isset( $meta[ $key ] )
        ? (bool) $meta[ $key ]
        : ( isset( $global[ $key ] ) ? (bool) $global[ $key ] : true );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Deep Linking', 'tnc-flipbook-3d-lite' ) );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
    $args = array(
        'id'          => $p . $key,
        'value'       => $value,
        'description' => __( 'Update URL hash on page change (#page/N) for shareable page links.', 'tnc-flipbook-3d-lite' ),
        'disabled'    => ! $override,
    );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-switcher.php';
    ?>
    </div>
    </div>

    <!-- Link Target -->
    <?php
    $key      = 'linkTarget';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = ( $override && isset( $meta[ $key ] ) ) ? $meta[ $key ] : ( $global[ $key ] ?? 'new' );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'Link Target', 'tnc-flipbook-3d-lite' ) );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
    $args = array(
        'id'          => $p . $key,
        'value'       => $value,
        'options'     => array( 'new' => __( 'New tab', 'tnc-flipbook-3d-lite' ), 'same' => __( 'Same tab', 'tnc-flipbook-3d-lite' ) ),
        'description' => __( 'Whether PDF links open in a new browser tab or the current one.', 'tnc-flipbook-3d-lite' ),
        'disabled'    => ! $override,
    );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-select.php';
    ?>
    </div>
    </div>

    <!-- PDF Link Overlay -->
    <?php
    $key      = 'pdfLinkOverlay';
    $override = ! empty( $meta[ $key . '_override' ] );
    $value    = $override && isset( $meta[ $key ] )
        ? (bool) $meta[ $key ]
        : ( isset( $global[ $key ] ) ? (bool) $global[ $key ] : true );
    $args     = array( 'meta_key' => $p . $key, 'override_val' => $override, 'label' => __( 'PDF Link Overlay', 'tnc-flipbook-3d-lite' ) );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-global-override.php';
    $args = array(
        'id'          => $p . $key,
        'value'       => $value,
        'description' => __( 'Show clickable links and annotations embedded in the PDF.', 'tnc-flipbook-3d-lite' ),
        'disabled'    => ! $override,
    );
    include TNC_FLIPBOOK_3D_DIR . 'admin/partials/field-switcher.php';
    ?>
    </div>
    </div>

</div><!-- .tncfb-general-tab -->
