<?php
namespace DisableDashboardWidgets;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

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

if ( ! isset( $module_id ) ) {
	$module_id = 'disable-dashboard-widgets';
}

$default_settings = ( new Module() )->getDefaultSettings();

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

if ( ! isset( $settings['custom_widgets'] ) || ! is_array( $settings['custom_widgets'] ) ) {
	$settings['custom_widgets'] = [];
}

$known_widgets   = Module::getKnownWidgets();
$known_labels    = Module::getKnownWidgetLabels();
$extra_widgets   = Module::getDiscoveredWidgets();
$disable_all     = ! empty( $settings['disable_all'] );

// Keep previously selected custom widgets visible even if rediscovery missed them.
$extra_by_id = [];
foreach ( $extra_widgets as $widget ) {
	$extra_by_id[ $widget['id'] ] = $widget;
}
foreach ( $settings['custom_widgets'] as $widget_id ) {
	if ( ! is_string( $widget_id ) || $widget_id === '' || isset( $extra_by_id[ $widget_id ] ) ) {
		continue;
	}
	$extra_by_id[ $widget_id ] = [
		'id'    => $widget_id,
		'title' => $widget_id,
	];
}
$extra_widgets = array_values( $extra_by_id );

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Clean up and speed up the dashboard by completely disabling some or all widgets. Disabled widgets won\'t load any assets nor show up under Screen Options.', '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( 'Widgets to Disable:', 'wp-powersuite' ); ?></th>
					<td>
						<fieldset class="wpps-ddw-fieldset">
							<div class="wpps-ddw-individual">
								<?php foreach ( $known_labels as $setting_key => $label ) : ?>
									<label class="wpps-ddw-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 disabled( $disable_all ); ?>
										/>
										<?php echo esc_html( $label ); ?>
										<?php if ( isset( $known_widgets[ $setting_key ] ) && $known_widgets[ $setting_key ] !== 'welcome' ) : ?>
											<span class="wpps-ddw-id"><?php echo esc_html( $known_widgets[ $setting_key ] ); ?></span>
										<?php endif; ?>
									</label>
								<?php endforeach; ?>

								<?php if ( $extra_widgets !== [] ) : ?>
									<p class="wpps-ddw-extra-heading"><strong><?php esc_html_e( 'Other Dashboard Widgets', 'wp-powersuite' ); ?></strong></p>
									<p class="description wpps-ddw-extra-note">
										<?php esc_html_e( 'Automatically detected from your dashboard (plugins, themes, and other sources). Check any to hide them.', 'wp-powersuite' ); ?>
									</p>
									<?php foreach ( $extra_widgets as $widget ) : ?>
										<label class="wpps-ddw-label">
											<input type="checkbox"
												name="custom_widgets[]"
												class="wp-powersuite-advanced-setting"
												data-setting-key="custom_widgets"
												value="<?php echo esc_attr( $widget['id'] ); ?>"
												<?php checked( in_array( $widget['id'], $settings['custom_widgets'], true ) ); ?>
												<?php disabled( $disable_all ); ?>
											/>
											<?php echo esc_html( $widget['title'] ); ?>
											<span class="wpps-ddw-id"><?php echo esc_html( $widget['id'] ); ?></span>
										</label>
									<?php endforeach; ?>
								<?php else : ?>
									<p class="description wpps-ddw-extra-note">
										<?php esc_html_e( 'Open the WordPress Dashboard once to auto-detect widgets from other plugins, then return here to hide them.', 'wp-powersuite' ); ?>
									</p>
								<?php endif; ?>
							</div>

							<label class="wpps-ddw-label wpps-ddw-label--divider">
								<input type="checkbox"
									name="disable_all"
									class="wp-powersuite-advanced-setting"
									data-setting-key="disable_all"
									value="1"
									<?php checked( $disable_all ); ?>
								/>
								<strong><?php esc_html_e( 'Disable All Widgets', 'wp-powersuite' ); ?></strong>
							</label>
							<p class="description wpps-ddw-disable-all-note">
								<?php esc_html_e( 'Removes every dashboard widget except WP PowerSuite widgets (IDs starting with wpps_). Individual selections above are ignored while this is enabled.', 'wp-powersuite' ); ?>
							</p>
						</fieldset>
					</td>
				</tr>
			</tbody>
		</table>
	</form>

	<style>
		.wpps-ddw-fieldset .wpps-ddw-label { display: block; margin-bottom: 15px; }
		.wpps-ddw-fieldset .wpps-ddw-label--divider { margin-top: 20px; padding-top: 15px; border-top: 1px solid #ddd; }
		.wpps-ddw-id { color: #646970; font-size: 12px; margin-left: 4px; }
		.wpps-ddw-extra-heading { margin: 20px 0 4px; }
		.wpps-ddw-extra-note { margin: 0 0 12px; }
		.wpps-ddw-disable-all-note { margin-top: 8px; }
	</style>
</div>
