<?php
/**
 * Static local previews of features implemented only in separate packages.
 *
 * The disabled controls have no setting names, actions, nonces, scripts or
 * runtime handlers in CAS Free and are never submitted or saved.
 */
if (!defined('ABSPATH')) exit;

if (!function_exists('custausu_admin_render_webauthn_section')) {
  function custausu_admin_render_webauthn_section(
    array $opt,
    callable $T,
    callable $get_field_box_class,
    callable $get_field_note_html
  ): string {
    unset($opt, $get_field_box_class, $get_field_note_html);

    $custausu_home_url = home_url('/');
    $custausu_home_parts = wp_parse_url($custausu_home_url);
    $custausu_origin_scheme = !empty($custausu_home_parts['scheme']) ? (string)$custausu_home_parts['scheme'] : 'https';
    $custausu_origin_host = !empty($custausu_home_parts['host']) ? (string)$custausu_home_parts['host'] : 'example.com';
    $custausu_origin_port = !empty($custausu_home_parts['port']) ? ':' . (int)$custausu_home_parts['port'] : '';
    $custausu_origin_example = $custausu_origin_scheme . '://' . $custausu_origin_host . $custausu_origin_port;
    $custausu_rp_name_example = (string)get_bloginfo('name');

    ob_start();

    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_start('webauthn_passkeys', $T), custausu_admin_allowed_output_html() );
    echo '<table id="custausu-section-webauthn" class="widefat striped custausu-webauthn-settings custausu-webauthn-settings--preview">';
    echo '<thead class="custausu-head"><tr><th colspan="2" class="custausu-webauthn-settings__head-cell"><h2 class="custausu-webauthn-settings__title"><span class="dashicons dashicons-shield"></span> | WebAuthn / Passkeys</h2></th></tr></thead>';
    echo '<tbody>';

    echo '<tr><th scope="row">' . esc_html($T('Overview', 'Panoramica')) . '</th><td>';
    echo '<p class="description custausu-webauthn-settings__description">'
      . esc_html($T(
        'This preview shows the WebAuthn / Passkeys settings available in Premium, including site identity, device history and the login spinner.',
        'Questa anteprima mostra le impostazioni WebAuthn / Passkeys disponibili con Premium, incluse l’identità del sito, lo storico del dispositivo e lo spinner di login.'
      ))
      . '</p>';
    echo '<p class="description custausu-webauthn-settings__description custausu-webauthn-settings__description--spaced">'
      . esc_html($T(
        'With Premium, these controls become available here. Passkey options are shown to eligible users after email verification.',
        'Con Premium, questi controlli diventano disponibili qui. Le opzioni Passkey vengono mostrate agli utenti idonei dopo la verifica email.'
      ))
      . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row"><label for="custausu_webauthn_rp_id">RP ID</label></th><td>';
    echo '<input id="custausu_webauthn_rp_id" type="text" class="regular-text" value="' . esc_attr($custausu_origin_host) . '" disabled aria-disabled="true">';
    echo '<p class="description">' . esc_html(sprintf(
      $T(
        'Recommended: the exact host configured for this site in WordPress, without protocol. Current example: %s. If your site is served with www, keep www here too.',
        'Consigliato: l’host esatto configurato per questo sito in WordPress, senza protocollo. Esempio attuale: %s. Se il sito è servito con www, mantieni anche il www qui.'
      ),
      $custausu_origin_host
    )) . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row"><label for="custausu_webauthn_origin">Origin</label></th><td>';
    echo '<input id="custausu_webauthn_origin" type="url" class="regular-text" value="' . esc_attr($custausu_origin_example) . '" disabled aria-disabled="true">';
    echo '<p class="description">' . esc_html(sprintf(
      $T(
        'Recommended: the exact HTTPS origin configured for this site in WordPress. Current example: %s. If your site is served with www, keep www here too.',
        'Consigliato: l’origin HTTPS esatto configurato per questo sito in WordPress. Esempio attuale: %s. Se il sito è servito con www, mantieni anche il www qui.'
      ),
      $custausu_origin_example
    )) . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row"><label for="custausu_webauthn_rp_name">RP Name</label></th><td>';
    echo '<input id="custausu_webauthn_rp_name" type="text" class="regular-text" value="' . esc_attr($custausu_rp_name_example) . '" disabled aria-disabled="true">';
    echo '<p class="description">' . esc_html($T(
      'Display name shown by authenticators during Passkey registration/login.',
      'Nome visualizzato dagli autenticatori durante registrazione/login con Passkey.'
    )) . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row"><label for="custausu_webauthn_max_device_aliases">' . esc_html($T('Max device aliases', 'Max alias device')) . '</label></th><td>';
    echo '<input id="custausu_webauthn_max_device_aliases" type="number" min="1" max="10" step="1" class="custausu-webauthn-settings__number-input" value="3" disabled aria-disabled="true">';
    echo '<p class="description">' . esc_html($T(
      'How many historical device IDs CAS can retain for the same device.',
      'Quanti ID device storici CAS può mantenere per lo stesso dispositivo.'
    )) . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row"><label for="custausu_webauthn_max_label_aliases">' . esc_html($T('Max label aliases', 'Max alias label')) . '</label></th><td>';
    echo '<input id="custausu_webauthn_max_label_aliases" type="number" min="1" max="10" step="1" class="custausu-webauthn-settings__number-input" value="3" disabled aria-disabled="true">';
    echo '<p class="description">' . esc_html($T(
      'How many historical labels CAS can retain while improving device naming over time.',
      'Quante label storiche CAS può mantenere mentre migliora il nome del dispositivo nel tempo.'
    )) . '</p>';
    echo '</td></tr>';

    echo '<tr><th scope="row"><label for="custausu_webauthn_spinner_preview_url">' . esc_html($T('Passkey Spinner URL', 'URL spinner Passkey')) . '</label></th><td>';
    echo '<div class="custausu-input-row">';
    echo '<input id="custausu_webauthn_spinner_preview_url" type="url" class="regular-text" value="" placeholder="https://..." disabled aria-disabled="true">';
    echo '<button type="button" class="button" disabled aria-disabled="true">' . esc_html($T('Select…', 'Seleziona…')) . '</button>';
    echo '<button type="button" class="button" disabled aria-disabled="true">' . esc_html($T('Clear', 'Svuota')) . '</button>';
    echo '</div>';
    echo '<p class="description">' . esc_html($T(
      'Optional visual used only by Premium WebAuthn/Passkey login. If empty, CAS uses the built-in CSS spinner.',
      'Elemento visuale opzionale usato solo dal login WebAuthn/Passkey Premium. Se vuoto, CAS usa lo spinner CSS integrato.'
    )) . '</p>';
    echo '</td></tr>';

    echo '</tbody></table>';
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_end(), custausu_admin_allowed_output_html() );

    return (string)ob_get_clean();
  }
}

if (!function_exists('custausu_admin_render_recaptcha_section')) {
  function custausu_admin_render_recaptcha_section(array $opt, callable $T, callable $sw): string {
    unset($opt, $sw);

    ob_start();

    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_start('recaptcha_enterprise', $T), custausu_admin_allowed_output_html() );
    echo '<table id="custausu-section-recaptcha" class="widefat striped custausu-recaptcha-settings custausu-recaptcha-settings--preview">';
    echo '<thead class="custausu-head"><tr><th colspan="2" class="custausu-recaptcha-settings__head-cell"><h2 class="custausu-recaptcha-settings__title"><span class="dashicons dashicons-google"></span> | reCAPTCHA Enterprise</h2></th></tr></thead>';
    echo '<tbody>';

    echo '<tr><th scope="row">' . esc_html($T('Enable', 'Abilita')) . '</th><td>';
    echo '<div class="custausu-input-row custausu-recaptcha-box">';
    foreach ([
      $T('Enable reCAPTCHA Enterprise', 'Abilita reCAPTCHA Enterprise'),
      $T('Protect CAS auth forms (login/register/lostpassword)', 'Proteggi i form CAS (login/registrazione/recupero password)'),
      $T('Protect Contact Form 7', 'Proteggi Contact Form 7'),
      $T('Prefer wp-config.php constants when available (auto-lock inputs below)', 'Preferisci le costanti in wp-config.php quando disponibili (blocca i campi sotto)'),
    ] as $label) {
      echo '<label class="custausu-switch custausu-switch--preview"><input type="checkbox" value="1" disabled aria-disabled="true"><span class="custausu-switch__slider" aria-hidden="true"></span><span class="custausu-switch__label">' . esc_html($label) . '</span></label>';
    }
    echo '<p class="description"><strong>' . esc_html($T('Tip:', 'Suggerimento:')) . '</strong> ' . esc_html($T(
      'Advanced can use reCAPTCHA Enterprise credentials saved in CAS or explicit wp-config.php constants.',
      'Advanced può usare credenziali reCAPTCHA Enterprise salvate in CAS oppure costanti esplicite in wp-config.php.'
    )) . '</p>';
    echo '</div></td></tr>';

    foreach ([
      'Project ID' => '',
      'Site Key' => '',
      'API Key' => '',
    ] as $label => $value) {
      $type = $label === 'API Key' ? 'password' : 'text';
      echo '<tr><th scope="row">' . esc_html($label) . '</th><td><div class="custausu-input-row">';
      echo '<input type="' . esc_attr($type) . '" class="regular-text" value="' . esc_attr($value) . '" disabled aria-disabled="true" autocomplete="off" spellcheck="false">';
      echo '<button type="button" class="button custausu-copy-btn" disabled aria-disabled="true">' . esc_html($T('Copy', 'Copia')) . '</button>';
      echo '</div></td></tr>';
    }

    echo '<tr><th scope="row">Thresholds';
    echo '<p class="description">' . esc_html($T(
      'Tip: values below 0.5 are less permissive.',
      'Tip: valori inferiori a 0.5 sono meno permissivi.'
    )) . '</p>';
    echo '</th><td class="custausu-recaptcha-settings__threshold-cell"><div class="custausu-threshold-grid">';
    foreach ([
      'login' => '0.5',
      'register' => '0.6',
      'lostpassword' => '0.7',
      'cf7' => '0.5',
    ] as $label => $value) {
      echo '<label>' . esc_html($label) . '</label><input type="text" value="' . esc_attr($value) . '" class="custausu-recaptcha-settings__threshold-input" disabled aria-disabled="true">';
    }
    echo '</div><p class="description">' . esc_html($T(
      'Examples: login 0.5, register 0.6, lostpassword 0.7, cf7 about 0.4–0.5.',
      'Esempi: login 0.5, register 0.6, lostpassword 0.7, cf7 circa 0.4–0.5.'
    )) . '</p></td></tr>';

    echo '</tbody></table>';
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_end(), custausu_admin_allowed_output_html() );

    return (string)ob_get_clean();
  }
}

if (!function_exists('custausu_admin_render_cf7_section')) {
  function custausu_admin_render_cf7_section(array $opt, callable $T, callable $sw): string {
    unset($opt, $sw);

    ob_start();

    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_start('cf7_mathcheck', $T), custausu_admin_allowed_output_html() );
    echo '<table id="custausu-section-cf7" class="widefat striped custausu-cf7-antibot custausu-cf7-antibot--preview">';
    echo '<thead class="custausu-head"><tr><th colspan="2" class="custausu-cf7-antibot__head-cell"><h2 class="custausu-cf7-antibot__title"><span class="dashicons dashicons-calculator"></span> | CF7 AntiBot (Math Check)</h2></th></tr></thead>';
    echo '<tbody>';

    echo '<tr><th scope="row">CF7 AntiBot</th><td>';
    echo '<label class="custausu-switch custausu-switch--preview"><input type="checkbox" value="1" disabled aria-disabled="true"><span class="custausu-switch__slider" aria-hidden="true"></span><span class="custausu-switch__label">' . esc_html($T('Enable CF7 Math Check', 'Abilita Math Check in CF7')) . '</span></label>';
    echo '<div class="left"><p class="description custausu-cf7-antibot__status"><strong>CF7:</strong> ' . esc_html($T(
      'Contact Form 7 is required to use Math Check in Advanced.',
      'Contact Form 7 è richiesto per usare Math Check in Advanced.'
    )) . '</p></div>';
    echo '</td></tr>';

    echo '<tr><th scope="row">' . esc_html($T('CF7 Math Check field', 'Campo Math Check CF7')) . '</th><td>';
    echo '<p class="description custausu-cf7-antibot__field-intro">' . esc_html($T(
      'Advanced adds this tag to the Contact Form 7 form where the Math Check should appear:',
      'Advanced usa questo tag nel form Contact Form 7 in cui deve comparire il Math Check:'
    )) . '</p>';
    echo '<div class="custausu-input-row custausu-cf7-antibot__shortcode-row">';
    echo '<code class="custausu-cf7-antibot__shortcode">[custausu_math* custausu_math]</code>';
    echo '<button type="button" class="button custausu-copy-btn" disabled aria-disabled="true">' . esc_html($T('Copy', 'Copia')) . '</button>';
    echo '</div>';
    echo '<p class="description custausu-cf7-antibot__field-note">' . esc_html($T(
      'This is an informational preview. The Free package does not register the CF7 tag or validation runtime.',
      'Questa è un’anteprima informativa. Il pacchetto Free non registra il tag CF7 né il runtime di validazione.'
    )) . '</p>';
    echo '</td></tr>';

    echo '</tbody></table>';
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Internal admin gate renderer returns controlled admin HTML.
    echo wp_kses( custausu_admin_gate_wrap_end(), custausu_admin_allowed_output_html() );

    return (string)ob_get_clean();
  }
}

