<?php
declare( strict_types=1 );

use FiboFilters\Config\Env;
use FiboFilters\DI\ContainerWrapper;
use FiboFilters\Filters\FilterSources;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function fibofilters_is_debug_action( string $action ): bool {
	return ! empty( $_GET['fibofilters-action'] ) &&
			$_GET['fibofilters-action'] === $action &&
			! empty( $_REQUEST['_wpnonce'] ) &&
			wp_verify_nonce( $_REQUEST['_wpnonce'], 'fibofilters_debug_filters' );
}

?>
<div class="components-card__body components-card-body fibofilters-settings-card">
	<div class="fibofilters-settings-header">
		<h2>Raw filter settings</h2>
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="filters">
			<input type="hidden" name="fibofilters-action" value="fibofilters-raw-filters-settings">
			<?php wp_nonce_field( 'fibofilters_debug_filters', '_wpnonce', false ); ?>
			<p>
				<input type="submit" class="components-button is-secondary" value="Get raw filter settings">
			</p>
			<?php
			if ( fibofilters_is_debug_action( 'fibofilters-raw-filters-settings' ) ) {
				$fibofilters_filters_option = get_option( 'fibofilters_filters' );
				if ( empty( $fibofilters_filters_option ) ) {
					$fibofilters_filters_option = '[]';
				}
				$fibofilters_blocks = json_decode( $fibofilters_filters_option, true );
				if ( json_last_error() === JSON_ERROR_NONE && is_array( $fibofilters_blocks ) ) {
					?>
					<pre class="fibofilters-pre"><?php echo var_export( $fibofilters_blocks ); ?></pre>
					<?php
				} else {
					?>
					<div class="components-notice is-warning" style="margin-top: 1em;">
						<div class="components-notice__content">
							Unable to decode filters settings.
						</div>
					</div>
					<?php
				}
			}
			?>
		</form>

		<br/>

		<h2>Sources</h2>
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="filters">
			<input type="hidden" name="fibofilters-action" value="fibofilters-show-source-analysis-result">
			<?php wp_nonce_field( 'fibofilters_debug_filters', '_wpnonce', false ); ?>
			<p>
				<select name="fibofilters-source" class="components-select-control__input">
					<?php
					/** @var FilterSources $fibofilters_filter_sources */
					$fibofilters_filter_sources = ContainerWrapper::get_instance()->get( FiboFilters\Filters\FilterSources::class );
					$fibofilters_sources        = $fibofilters_filter_sources->get_sources();
					// phpcs:ignore WordPress.Security.NonceVerification.Recommended
					$fibofilters_selected_source = $_REQUEST['fibofilters-source'] ?? '';

					foreach ( $fibofilters_sources as $fibofilters_source ) {
						$fibofilters_value = $fibofilters_source->get_data_attr( 'value' );
						$fibofilters_label = $fibofilters_source->get_data_attr( 'label' );
						?>
						<option value="<?php echo esc_attr( $fibofilters_value ); ?>" <?php selected( $fibofilters_value, $fibofilters_selected_source ); ?>><?php echo esc_html( $fibofilters_label ); ?></option>
					<?php } ?>
				</select>
				<input type="submit" class="components-button is-secondary" value="Show data analysis result for selected source">
			</p>
			<?php
			if (
				fibofilters_is_debug_action( 'fibofilters-show-source-analysis-result' ) &&
				! empty( $_REQUEST['fibofilters-source'] )
			) {
				$fibofilters_source_analysis_data = get_transient( Env::$plugin_prefix . 'sources_analyse_data' );
				if ( empty( $fibofilters_source_analysis_data ) || ! isset( $fibofilters_source_analysis_data[ $_REQUEST['fibofilters-source'] ] ) ) {
					?>
					<div class="components-notice is-warning" style="margin-top: 1em;">
						<div class="components-notice__content">
							No data about sources. Go to FiboFilters settings → <a target="_blank" href="<?php echo esc_url( admin_url( 'admin.php?page=fibofilters#filters' ) ); ?>">Filters tab</a>, wait until you can add filters and then come back here to try again.
						</div>
					</div>
					<?php
				} else {
					?>
					<pre class="fibofilters-pre"><?php echo var_export( $fibofilters_source_analysis_data[ $_REQUEST['fibofilters-source'] ] ); ?></pre>
					<?php
				}
			}
			?>
		</form>

		<br/>
		<h2>Maintenance</h2>
		<?php
		if ( fibofilters_is_debug_action( 'fibofilters-debug-delete-source-analysis-results' ) ) {
			delete_transient( Env::$plugin_prefix . 'sources_analyse_data' )
			?>
			<script>
				window.fiboFiltersNotications.push({
					message: 'Transient with filter source analysis results has been deleted.'
				});
			</script>
			<?php
		}
		?>
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="filters">
			<input type="hidden" name="fibofilters-action" value="fibofilters-debug-delete-source-analysis-results">
			<?php wp_nonce_field( 'fibofilters_debug_filters', '_wpnonce', false ); ?>
			<p>
				<input type="submit" class="components-button is-secondary is-destructive" value="Remove transient with filter source analysis results">
			</p>
		</form>
		<?php
		if ( fibofilters_is_debug_action( 'fibofilters-debug-delete-option-disable-source-analyzer' ) ) {
			delete_option( Env::$plugin_prefix . 'disable_source_analyzer' )
			?>
			<script>
				window.fiboFiltersNotications.push({
					message: 'The option that blocks filter source analysis has been removed.'
				});
			</script>
			<?php
		}
		?>
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="filters">
			<input type="hidden" name="fibofilters-action" value="fibofilters-debug-delete-option-disable-source-analyzer">
			<?php wp_nonce_field( 'fibofilters_debug_filters', '_wpnonce', false ); ?>
			<p>
				<input type="submit" class="components-button is-secondary is-destructive" value="Remove the option that blocks filter source analysis">
			</p>
		</form>
	</div>
</div>
