<?php
/**
 * Template for ElasticProbe settings page
 *
 * @since  2.1
 * @package elasticprobe
 */

use ElasticProbe\Dashboard;
use ElasticProbe\Elasticsearch;
use ElasticProbe\IndexHelper;
use ElasticProbe\Utils;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$index_meta = IndexHelper::factory()->get_index_meta();

$version = Elasticsearch::factory()->get_elasticsearch_version();

$host        = Utils\get_host();
$is_epio     = Utils\is_epio();
$credentials = Utils\get_epio_credentials();
$sid         = Utils\get_subscription_id();
$wpconfig    = defined( 'EPROBE_HOST' ) && EPROBE_HOST;

$bulk_setting = Utils\get_option( 'eprobe_bulk_setting', 350 );
?>

<?php require_once __DIR__ . '/header.php'; ?>

<div class="error-overlay <?php if ( ! empty( $index_meta ) ) : ?>syncing<?php endif; ?>"></div>
<div class="wrap">
	<h1><?php esc_html_e( 'Settings', 'elasticprobe' ); ?></h1>

	<form action="" method="post" class="ep-settings">
		<?php wp_nonce_field( 'elasticpress_settings', 'ep_settings_nonce' ); ?>

		<div class="ep-credentials">
			<?php if ( ! $wpconfig ) : ?>
				<h2 class="nav-tab-wrapper ep-credentials-tabs">
					<button class="nav-tab ep-credentials-tab <?php if ( ! $host || $is_epio ) { ?>nav-tab-active initial<?php } ?>" data-epio type="button">
						<img src="<?php echo esc_url( plugins_url( '/images/logo-icon.svg', dirname( __DIR__ ) ) ); ?>" width="16" height="16" alt="ElasticProbe.com" />
						<span>ElasticProbe.com</span>
					</button>
					<button class="nav-tab ep-credentials-tab <?php if ( $host && ! $is_epio ) { ?>nav-tab-active initial<?php } ?>" type="button">
						<span>Third-Party/Self-Hosted</span>
					</button>
				</h2>
			<?php endif; ?>

			<fieldset class="<?php if ( $wpconfig ) { ?>predefined<?php } ?>">
				<?php if ( $is_epio || ! $wpconfig ) : ?>
					<p class="ep-legend ep-additional-fields <?php if ( $host && ! $is_epio ) { ?>hidden<?php } ?>" aria-hidden="<?php if ( $host && ! $is_epio ) { ?>true<?php } else { ?>false<?php } ?>">
						<a href="https://elasticprobe.com/" target="_blank" rel="noreferrer noopener">ElasticProbe.com</a> is a hosted Elasticsearch service built for ElasticProbe.
					</p>
				<?php endif; ?>
				<table class="form-table">
					<tbody>
						<tr class="ep-host-row">
							<th scope="row">
								<label for="ep_host">
									<?php if ( $is_epio ) : ?>
										<?php esc_html_e( 'ElasticProbe.com Host URL', 'elasticprobe' ); ?>
									<?php else : ?>
										<?php esc_html_e( 'Elasticsearch Host URL', 'elasticprobe' ); ?>
									<?php endif; ?>
								</label>
							</th>
							<td>
								<?php
								/**
								 * Filter whether to show host field in admin UI or not
								 *
								 * @hook eprobe_admin_show_host
								 * @param  {boolean} $show True to show
								 * @return {boolean} New value
								 */
								$show_host = apply_filters( 'eprobe_admin_show_host', true );
								$disabled  = $wpconfig || ! $show_host;
								$value     = $show_host ? esc_url( $host ) : __( '••••••••••••••••', 'elasticprobe' );
								?>
								<input <?php disabled( $disabled, true, true ); ?> placeholder="https://" type="text" value="<?php echo esc_attr( $value ); ?>" name="ep_host" id="ep_host">
								<?php if ( $show_host ) : ?>
									<?php if ( $wpconfig ) : ?>
										<p class="description ep-host-legend"><?php esc_html_e( 'Host already defined in wp-config.php.', 'elasticprobe' ); ?></p>
									<?php elseif ( $is_epio ) : ?>
										<p class="description ep-host-legend"><?php esc_html_e( 'Plug in your ElasticProbe.com server here.', 'elasticprobe' ); ?></p>
									<?php else : ?>
										<p class="description ep-host-legend"><?php esc_html_e( 'Plug in your Elasticsearch server here.', 'elasticprobe' ); ?></p>
									<?php endif; ?>
								<?php endif; ?>
							</td>
						</tr>
						<?php if ( $is_epio || ! $wpconfig ) : ?>

							<tr class="ep-additional-fields <?php if ( $host && ! $is_epio ) { ?>hidden<?php } ?>" aria-hidden="<?php if ( $host && ! $is_epio ) { ?>true<?php } else { ?>false<?php } ?>">
								<th scope="row">
									<label for="ep_username"><?php esc_html_e( 'API key ID', 'elasticprobe' ); ?></label>
								</th>
								<td>
									<?php
									/**
									 * Filter whether to show epio credentials fields in admin UI or not
									 *
									 * @hook eprobe_admin_show_credentials
									 * @param  {boolean} $show True to show
									 * @return {boolean} New value
									 */
									if ( apply_filters( 'eprobe_admin_show_credentials', true ) ) :
										?>
										<input <?php if ( defined( 'EPROBE_CREDENTIALS' ) && EPROBE_CREDENTIALS ) : ?>disabled<?php endif; ?> type="text" value="<?php echo esc_attr( $credentials['username'] ); ?>" name="eprobe_credentials[username]" id="ep_username">
									<?php endif ?>
									<?php if ( defined( 'EPROBE_CREDENTIALS' ) && EPROBE_CREDENTIALS ) : ?>
										<p class="description"><?php esc_html_e( 'Your API key ID is set in wp-config.php', 'elasticprobe' ); ?></p>
									<?php else : ?>
										<p class="description"><?php esc_html_e( 'Plug in your API key ID here.', 'elasticprobe' ); ?></p>
									<?php endif; ?>
								</td>
							</tr>

							<tr class="ep-additional-fields <?php if ( $host && ! $is_epio ) { ?>hidden<?php } ?>" aria-hidden="<?php if ( $host && ! $is_epio ) { ?>true<?php } else { ?>false<?php } ?>">
								<th scope="row">
									<label for="ep_token"><?php esc_html_e( 'API key', 'elasticprobe' ); ?></label>
								</th>
								<td>
									<?php
									/**
									 * Filter whether to show epio credentials fields in admin UI or not
									 *
									 * @hook eprobe_admin_show_credentials
									 * @param  {boolean} $show True to show
									 * @return {boolean} New value
									 */
									if ( apply_filters( 'eprobe_admin_show_credentials', true ) ) :
										?>
										<input <?php if ( defined( 'EPROBE_CREDENTIALS' ) && EPROBE_CREDENTIALS ) : ?>disabled<?php endif; ?> type="text" value="<?php echo esc_attr( $credentials['token'] ); ?>" name="eprobe_credentials[token]" id="ep_token">
									<?php endif ?>
									<?php if ( defined( 'EPROBE_CREDENTIALS' ) && EPROBE_CREDENTIALS ) : ?>
										<p class="description"><?php esc_html_e( 'Your API key is set in wp-config.php', 'elasticprobe' ); ?></p>
									<?php else : ?>
										<p class="description"><?php esc_html_e( 'Plug in your API key here.', 'elasticprobe' ); ?></p>
									<?php endif; ?>
								</td>
							</tr>

							<tr class="ep-additional-fields <?php if ( $host && ! $is_epio ) { ?>hidden<?php } ?>" aria-hidden="<?php if ( $host && ! $is_epio ) { ?>true<?php } else { ?>false<?php } ?>">
								<th scope="row">
									<label for="probe_sid"><?php esc_html_e( 'Subscription ID', 'elasticprobe' ); ?></label>
								</th>
								<td>
									<?php
									/**
									 * Filter whether to show epio credentials fields in admin UI or not
									 *
									 * @hook eprobe_admin_show_credentials
									 * @param  {boolean} $show True to show
									 * @return {boolean} New value
									 */
									if ( apply_filters( 'eprobe_admin_show_credentials', true ) ) :
										?>
										<input <?php if ( defined( 'EPROBE_SID' ) && EPROBE_SID ) : ?>disabled<?php endif; ?> type="text" value="<?php echo esc_attr( $sid ); ?>" name="sid" id="probe_sid">
									<?php endif ?>
									<?php if ( defined( 'EPROBE_SID' ) && EPROBE_SID ) : ?>
										<p class="description"><?php esc_html_e( 'Your Subscription ID is set in wp-config.php', 'elasticprobe' ); ?></p>
									<?php else : ?>
										<p class="description"><?php esc_html_e( 'Plug in your subscription ID here.', 'elasticprobe' ); ?></p>
									<?php endif; ?>
								</td>
							</tr>
						<?php endif; ?>
					</tbody>
				</table>
			</fieldset>
		</div>

		<div class="ep-credentials-general">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row">
							<label for="eprobe_language"><?php esc_html_e( 'Elasticsearch Language', 'elasticprobe' ); ?></label>
						</th>
						<td>
							<?php
							$eprobe_language = Utils\get_language();

							wp_dropdown_languages(
								[
									'id'        => 'eprobe_language',
									'name'      => 'eprobe_language',
									'selected'  => $eprobe_language,
									'languages' => Dashboard\get_available_languages( 'locales' ),
									'show_option_site_default' => true,
									'explicit_option_en_us' => true,
									'show_available_translations' => false,
								]
							);
							?>
							<p class="description"><?php esc_html_e( 'Default language for your Elasticsearch mapping.', 'elasticprobe' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label><?php esc_html_e( 'Elasticsearch Version', 'elasticprobe' ); ?></label>
						</th>
						<td>
							<?php if ( $is_epio ) : ?>
								<?php esc_html_e( 'ElasticProbe.com Managed Platform', 'elasticprobe' ); ?>
							<?php else : ?>
								<?php if ( ! empty( $version ) ) : ?>
									<?php echo esc_html( $version ); ?>
								<?php else : ?>
									&mdash;
								<?php endif; ?>
							<?php endif; ?>
						</td>
					</tr>
					<?php if ( ! empty( $host ) && ! has_filter( 'eprobe_index_posts_per_page' ) ) : ?>
						<tr>
							<th scope="row">
								<label for="eprobe_bulk_setting"><?php esc_html_e( 'Content Items per Index Cycle ', 'elasticprobe' ); ?></label>
							</th>
							<td>
								<input type="text" name="eprobe_bulk_setting" id="eprobe_bulk_setting" value="<?php echo absint( $bulk_setting ); ?>">
							</td>
						</tr>
					<?php endif; ?>
				</tbody>
			</table>
		</div>

		<?php
		/**
		 * Fires after settings table is displayed for inserting custom settings.
		 *
		 * @hook eprobe_settings_custom
		 */
		do_action( 'eprobe_settings_custom' );
		?>

		<input type="submit" <?php if ( ! empty( $index_meta ) ) : ?>disabled<?php endif; ?> name="submit" id="submit" class="button button-primary" value="<?php esc_attr_e( 'Save Changes', 'elasticprobe' ); ?>">
	</form>
</div>