<?php
namespace RemoveAdminBarItems;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! current_user_can( 'manage_options' ) ) {
	return;
}

if ( ! isset( $module_id ) ) {
	$module_id = 'remove-admin-bar-items';
}

$settings = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}
$settings = Module::normalizeSettings( $settings );

$known_items   = Module::getKnownBarItems();
$extra_items   = Module::getDiscoveredBarItems();
$boolean_items = [
	'remove_howdy'          => __( "Remove 'Howdy' text", 'wp-powersuite' ),
	'remove_help_tab'       => __( 'Remove Help tab and drawer', 'wp-powersuite' ),
	'remove_screen_options' => __( 'Remove Screen Options tab', 'wp-powersuite' ),
];

// Keep previously selected custom items visible even if rediscovery missed them.
$extra_by_id = [];
foreach ( $extra_items as $item ) {
	$extra_by_id[ $item['id'] ] = $item;
}
foreach ( $settings['items_to_remove'] as $item_id ) {
	if ( isset( $known_items[ $item_id ] ) || isset( $extra_by_id[ $item_id ] ) ) {
		continue;
	}
	$extra_by_id[ $item_id ] = [
		'id'    => $item_id,
		'title' => $item_id,
	];
}
$extra_items = array_values( $extra_by_id );

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Remove specific items from the WordPress admin bar to create a cleaner interface. Select which items you want to remove.', 'wp-powersuite' ); ?>
	</p>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<table class="form-table">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'Items to Remove:', 'wp-powersuite' ); ?></th>
					<td>
						<fieldset class="wpps-rabi-fieldset">
							<?php foreach ( $known_items as $item_id => $item_name ) : ?>
								<label class="wpps-rabi-label">
									<input type="checkbox"
										name="items_to_remove[]"
										class="wp-powersuite-advanced-setting"
										data-setting-key="items_to_remove"
										value="<?php echo esc_attr( $item_id ); ?>"
										<?php checked( in_array( $item_id, $settings['items_to_remove'], true ) ); ?>
									/>
									<?php echo esc_html( $item_name ); ?>
									<span class="wpps-rabi-id"><?php echo esc_html( $item_id ); ?></span>
								</label>
							<?php endforeach; ?>

							<?php if ( $extra_items !== [] ) : ?>
								<p class="wpps-rabi-extra-heading"><strong><?php esc_html_e( 'Other Admin Bar Items', 'wp-powersuite' ); ?></strong></p>
								<p class="description wpps-rabi-extra-note">
									<?php esc_html_e( 'Automatically detected from the toolbar (plugins, themes, and other sources). Check any to hide them.', 'wp-powersuite' ); ?>
								</p>
								<?php foreach ( $extra_items as $item ) : ?>
									<label class="wpps-rabi-label">
										<input type="checkbox"
											name="items_to_remove[]"
											class="wp-powersuite-advanced-setting"
											data-setting-key="items_to_remove"
											value="<?php echo esc_attr( $item['id'] ); ?>"
											<?php checked( in_array( $item['id'], $settings['items_to_remove'], true ) ); ?>
										/>
										<?php echo esc_html( $item['title'] ); ?>
										<span class="wpps-rabi-id"><?php echo esc_html( $item['id'] ); ?></span>
									</label>
								<?php endforeach; ?>
							<?php else : ?>
								<p class="description wpps-rabi-extra-note">
									<?php esc_html_e( 'Browse any admin page once (with the toolbar visible) to auto-detect items from other plugins, then return here to hide them.', 'wp-powersuite' ); ?>
								</p>
							<?php endif; ?>

							<p class="wpps-rabi-extra-heading"><strong><?php esc_html_e( 'Other Options', 'wp-powersuite' ); ?></strong></p>
							<?php foreach ( $boolean_items as $setting_key => $label ) : ?>
								<label class="wpps-rabi-label">
									<input type="checkbox"
										name="<?php echo esc_attr( $setting_key ); ?>"
										class="wp-powersuite-advanced-setting"
										data-setting-key="<?php echo esc_attr( $setting_key ); ?>"
										value="1"
										<?php checked( ! empty( $settings[ $setting_key ] ) ); ?>
									/>
									<?php echo esc_html( $label ); ?>
								</label>
							<?php endforeach; ?>

							<p class="description" style="margin-top: 10px;">
								<?php esc_html_e( 'Changes apply in wp-admin and on the frontend toolbar when logged in. Help and Screen Options only affect admin screens.', 'wp-powersuite' ); ?>
							</p>
						</fieldset>
					</td>
				</tr>
			</tbody>
		</table>
	</form>

	<style>
		.wpps-rabi-fieldset .wpps-rabi-label { display: block; margin-bottom: 12px; }
		.wpps-rabi-id { color: #646970; font-size: 12px; margin-left: 4px; }
		.wpps-rabi-extra-heading { margin: 20px 0 4px; }
		.wpps-rabi-extra-note { margin: 0 0 12px; }
	</style>
</div>
