<?php
declare(strict_types=1);

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

global $wp_filter;

/** @var FiboFilters\Integrations\Themes $fibofilters_theme_integration */
$fibofilters_theme_integration = FiboFilters\DI\ContainerWrapper::get_instance()->get( FiboFilters\Integrations\Themes::class );
$fibofilters_theme_context     = $fibofilters_theme_integration->get_integration_context();

$fibofilters_theme_name    = $fibofilters_theme_context['active']['name'] ?? '';
$fibofilters_theme_version = $fibofilters_theme_context['active']['version'] ?? '';
$fibofilters_theme_slug    = $fibofilters_theme_context['active']['slug'] ?? '';

$fibofilters_theme_is_child = $fibofilters_theme_integration->is_child_theme()
	? sprintf( 'Yes. Parent (%s v%s)', $fibofilters_theme_context['parent']['name'], $fibofilters_theme_context['parent']['version'] )
	: 'No';

/** @var FiboFilters\Integrations\Plugins $fibofilters_plugins_integration */
$fibofilters_plugins_integration = FiboFilters\DI\ContainerWrapper::get_instance()->get( FiboFilters\Integrations\Plugins::class );
$fibofilters_plugins_active      = $fibofilters_plugins_integration->get_active_plugins();

// Hooks.
$fibofilters_hooks = [];
foreach ( $wp_filter as $fibofilters_hook_name => $fibofilters_hook_obj ) {
	if ( strpos( $fibofilters_hook_name, 'fibofilters/' ) !== 0 ) {
		continue;
	}
	if ( ! $fibofilters_hook_obj instanceof WP_Hook ) {
		continue;
	}

	$fibofilters_hook_callbacks = [];

	foreach ( $fibofilters_hook_obj->callbacks as $fibofilters_priority => $fibofilters_callbacks ) {
		foreach ( $fibofilters_callbacks as $fibofilters_callback ) {
			$fibofilters_fn   = $fibofilters_callback['function'];
			$fibofilters_file = null;
			$fibofilters_line = null;
			try {
				if ( is_string( $fibofilters_fn ) ) {
					// Named function.
					$fibofilters_ref  = new ReflectionFunction( $fibofilters_fn );
					$fibofilters_file = $fibofilters_ref->getFileName();
					$fibofilters_line = $fibofilters_ref->getStartLine();
				} elseif ( is_array( $fibofilters_fn ) && count( $fibofilters_fn ) === 2 ) {
					// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
					// [Object/Class, 'method'].
					$fibofilters_ref  = new ReflectionMethod( $fibofilters_fn[0], $fibofilters_fn[1] );
					$fibofilters_file = $fibofilters_ref->getFileName();
					$fibofilters_line = $fibofilters_ref->getStartLine();
				} elseif ( $fibofilters_fn instanceof Closure ) {
					// Anonymous function.
					$fibofilters_ref  = new ReflectionFunction( $fibofilters_fn );
					$fibofilters_file = $fibofilters_ref->getFileName();
					$fibofilters_line = $fibofilters_ref->getStartLine();
				}

				$fibofilters_callback_type = '';
				if ( is_array( $fibofilters_fn ) ) {
					$fibofilters_callback_type = is_object( $fibofilters_fn[0] )
						? get_class( $fibofilters_fn[0] ) . '->' . $fibofilters_fn[1]
						: $fibofilters_fn[0] . '::' . $fibofilters_fn[1];
				} elseif ( is_string( $fibofilters_fn ) ) {
					$fibofilters_callback_type = $fibofilters_fn;
				} else {
					$fibofilters_callback_type = 'Closure';
				}

				$fibofilters_hook_callbacks[] = [
					'priority' => $fibofilters_priority,
					'callback' => $fibofilters_callback_type,
					'file'     => ( ! empty( $fibofilters_file ) ? $fibofilters_file : 'n/a' ) . ':' . ( ! empty( $fibofilters_line ) ? $fibofilters_line : 'n/a' ),
				];
			} catch ( ReflectionException $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
			}
		}
	}
	if ( ! empty( $fibofilters_hook_callbacks ) ) {
		$fibofilters_hooks[] = [
			'hook'      => $fibofilters_hook_name,
			'callbacks' => $fibofilters_hook_callbacks,
		];
	}
}
?>
<div class="components-card__body components-card-body fibofilters-settings-card fibofilters-debug-integrations">

	<div class="fibofilters-settings-header fibofilters-debug-integrations__wrapper">
		<h3 class="fibofilters-debug-integrations__title">Active integrations with the plugins</h3>

		<?php if ( empty( $fibofilters_plugins_integration->get_active_plugins() ) ) : ?>
			<div class="components-notice is-warning fibofilters-debug-integrations__notice">
				<div class="components-notice__content">No integrations enabled.</div>
			</div>
		<?php else : ?>
			<table class="widefat striped fibofilters-debug-integrations__table fibofilters-debug-integrations__table--plugins">
				<colgroup>
					<col/><col/>
				</colgroup>
				<thead>
				<tr>
					<th>Plugin name</th>
					<th>Version</th>
				</tr>
				</thead>
				<tbody>
				<?php foreach ( $fibofilters_plugins_active as $fibofilters_plugin ) : ?>
					<?php
					$fibofilters_plugin_name    = $fibofilters_plugin['name'] ?? '';
					$fibofilters_plugin_version = ( ! empty( $fibofilters_plugin['version_const'] ) && defined( $fibofilters_plugin['version_const'] ) )
						? (string) constant( $fibofilters_plugin['version_const'] )
						: '';
					?>
					<tr>
						<td><?php echo esc_html( $fibofilters_plugin_name ); ?></td>
						<td><?php echo esc_html( 'v' . $fibofilters_plugin_version ); ?></td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>

	<div class="fibofilters-settings-header fibofilters-debug-integrations__wrapper">
		<h3 class="fibofilters-debug-integrations__title">Active theme integration</h3>

		<?php if ( ! $fibofilters_theme_integration->is_current_integration_enabled() ) : ?>
			<div class="components-notice is-warning fibofilters-debug-integrations__notice">
				<div class="components-notice__content">No integration enabled.</div>
			</div>
		<?php else : ?>
			<table class="widefat striped fibofilters-debug-integrations__table fibofilters-debug-integrations__table--theme">
				<colgroup>
					<col /><col/><col/><col/>
				</colgroup>
				<thead>
				<tr>
					<th>Theme name</th>
					<th>Slug</th>
					<th>Version</th>
					<th>Is child theme?</th>
				</tr>
				</thead>
				<tbody>
				<tr>
					<td><?php echo esc_html( $fibofilters_theme_name ); ?></td>
					<td><code class="fibofilters-debug-integrations__slug"><?php echo esc_html( $fibofilters_theme_slug ); ?></code></td>
					<td><?php echo esc_html( 'v' . $fibofilters_theme_version ); ?></td>
					<td><?php echo esc_html( $fibofilters_theme_is_child ); ?></td>
				</tr>
				</tbody>
			</table>
		<?php endif; ?>
	</div>

	<div class="fibofilters-settings-header fibofilters-debug-integrations__wrapper">
		<h3 class="fibofilters-debug-integrations__title">Hooks</h3>

		<table class="widefat fibofilters-debug-integrations__table">
			<colgroup>
				<col /><col/><col/>
			</colgroup>
			<thead>
			<tr>
				<th>Hook name (action or filter)</th>
				<th style="width: 60px;">Priority</th>
				<th>Callback</th>
				<th>File:line</th>
			</tr>
			</thead>
			<tbody>
			<?php
			foreach ( $fibofilters_hooks as $fibofilters_hooks_row ) {
				foreach ( $fibofilters_hooks_row['callbacks'] as $fibofilters_row_index => $fibofilters_callback ) {
					?>
					<tr>
						<?php
						if ( $fibofilters_row_index === 0 ) {
							// First callback for this hook, print the hook name with rowspan.
							?>
							<td style="border-bottom: 1px solid #c3c4c7;" rowspan="<?php echo count( $fibofilters_hooks_row['callbacks'] ); ?>"><strong><?php echo esc_html( $fibofilters_hooks_row['hook'] ); ?></strong></td>
							<?php
						}
						$fibofilters_td_styles = [];
						if ( $fibofilters_row_index === count( $fibofilters_hooks_row['callbacks'] ) - 1 ) {
							$fibofilters_td_styles[] = 'border-bottom: 1px solid #c3c4c7;';
						}
						if ( strpos( $fibofilters_callback['file'], 'plugins/fibofilters' ) === false ) {
							$fibofilters_td_styles[] = 'font-weight: bold;';
						}
						?>
						<td style="<?php echo esc_attr( join( ' ', $fibofilters_td_styles ) ); ?>"><?php echo esc_html( $fibofilters_callback['priority'] ); ?></td>
						<td style="<?php echo esc_attr( join( ' ', $fibofilters_td_styles ) ); ?>"><?php echo esc_html( $fibofilters_callback['callback'] ); ?></td>
						<td style="<?php echo esc_attr( join( ' ', $fibofilters_td_styles ) ); ?>"><?php echo esc_html( $fibofilters_callback['file'] ); ?></td>
					</tr>
					<?php
				}
			}
			?>
			</tbody>
		</table>
	</div>
</div>
