<?php

use DisableGutenberg\Module;
use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

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

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

if ( ! class_exists( Module::class ) ) {
	$module_file = __DIR__ . '/module.php';
	if ( is_readable( $module_file ) ) {
		require_once $module_file;
	}
}

$defaults = class_exists( Module::class ) ? ( new Module() )->getDefaultSettings() : [
	'mode'                => 'all',
	'disabled_post_types' => [],
	'remove_frontend_css' => true,
];

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

$settings = class_exists( Module::class )
	? Module::normalizeSettings( $settings )
	: array_merge( $defaults, $settings );

$mode                = $settings['mode'];
$disabled_post_types = $settings['disabled_post_types'];
$post_types          = class_exists( Module::class ) ? Module::getApplicablePostTypes() : [];

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Switch the block editor back to the classic editor site-wide or only for selected post types.', '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" role="presentation">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'Disable Mode', 'wp-powersuite' ); ?></th>
					<td>
						<fieldset>
							<label>
								<input type="radio"
									name="mode"
									class="wp-powersuite-advanced-setting wpps-disable-gutenberg-mode"
									data-setting-key="mode"
									value="all"
									<?php checked( $mode, 'all' ); ?>
								/>
								<strong><?php esc_html_e( 'Disable Everywhere', 'wp-powersuite' ); ?></strong>
								<p class="description"><?php esc_html_e( 'Use the classic editor for all post types that support it.', 'wp-powersuite' ); ?></p>
							</label>
						</fieldset>
						<fieldset style="margin-top: 15px;">
							<label>
								<input type="radio"
									name="mode"
									class="wp-powersuite-advanced-setting wpps-disable-gutenberg-mode"
									data-setting-key="mode"
									value="specific"
									<?php checked( $mode, 'specific' ); ?>
								/>
								<strong><?php esc_html_e( 'Disable for Specific Post Types', 'wp-powersuite' ); ?></strong>
								<p class="description"><?php esc_html_e( 'Keep Gutenberg for some types and use the classic editor only where you choose below.', 'wp-powersuite' ); ?></p>
							</label>
						</fieldset>
					</td>
				</tr>
				<tr id="wpps-disable-gutenberg-post-types-row" <?php echo $mode === 'specific' ? '' : 'style="display:none;"'; ?>>
					<th scope="row">
						<?php esc_html_e( 'Disable Gutenberg For', 'wp-powersuite' ); ?>
					</th>
					<td>
						<p class="description" style="margin-bottom: 8px;"><?php esc_html_e( 'Selected post types will open in the classic editor. Unselected types keep the block editor.', 'wp-powersuite' ); ?></p>
						<?php if ( empty( $post_types ) ) : ?>
							<p class="description"><?php esc_html_e( 'No post types available.', 'wp-powersuite' ); ?></p>
						<?php else : ?>
							<div class="wpps-ct-multiselect" id="<?php echo esc_attr( $module_id ); ?>_post_types_ms"
								data-label-none="<?php echo esc_attr( __( 'Select post types...', 'wp-powersuite' ) ); ?>"
								data-label-single="<?php echo esc_attr( __( '1 selected', 'wp-powersuite' ) ); ?>"
								data-label-multi="<?php echo esc_attr( __( '%d selected', 'wp-powersuite' ) ); ?>">
								<button type="button" class="wpps-ct-multiselect__trigger" aria-expanded="false" aria-haspopup="listbox" aria-controls="<?php echo esc_attr( $module_id ); ?>_post_types_list">
									<span class="wpps-ct-multiselect__trigger-text"><?php esc_html_e( 'Select post types...', 'wp-powersuite' ); ?></span>
									<span class="dashicons dashicons-arrow-down-alt2 wpps-ct-multiselect__chevron"></span>
								</button>
								<div class="wpps-ct-multiselect__dropdown" id="<?php echo esc_attr( $module_id ); ?>_post_types_list" role="listbox" hidden>
									<input type="text" class="wpps-ct-multiselect__search" placeholder="<?php esc_attr_e( 'Search post types...', 'wp-powersuite' ); ?>" autocomplete="off" />
									<div class="wpps-ct-multiselect__actions">
										<button type="button" class="wpps-ct-multiselect__action" data-action="select-all"><?php esc_html_e( 'Select all', 'wp-powersuite' ); ?></button>
										<button type="button" class="wpps-ct-multiselect__action" data-action="clear"><?php esc_html_e( 'Clear', 'wp-powersuite' ); ?></button>
									</div>
									<div class="wpps-ct-multiselect__options">
										<?php foreach ( $post_types as $post_type_name => $label ) : ?>
											<?php $is_checked = in_array( $post_type_name, $disabled_post_types, true ); ?>
											<label class="wpps-ct-multiselect__option" data-slug="<?php echo esc_attr( $post_type_name ); ?>" data-label="<?php echo esc_attr( $label ); ?>">
												<input type="checkbox"
													name="disabled_post_types[]"
													class="wp-powersuite-advanced-setting"
													data-setting-key="disabled_post_types"
													value="<?php echo esc_attr( $post_type_name ); ?>"
													<?php checked( $is_checked ); ?>
												/>
												<span class="wpps-ct-multiselect__option-label"><?php echo esc_html( $label ); ?></span>
												<span class="wpps-ct-multiselect__option-slug">(<?php echo esc_html( $post_type_name ); ?>)</span>
											</label>
										<?php endforeach; ?>
									</div>
								</div>
							</div>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Frontend Performance', 'wp-powersuite' ); ?></th>
					<td>
						<label>
							<input type="checkbox"
								name="remove_frontend_css"
								class="wp-powersuite-advanced-setting"
								data-setting-key="remove_frontend_css"
								value="1"
								<?php checked( ! empty( $settings['remove_frontend_css'] ) ); ?>
							/>
							<?php esc_html_e( 'Remove block editor CSS on the frontend for views where Gutenberg is disabled', 'wp-powersuite' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Existing block content still renders; this only drops unused stylesheets to speed up page loads.', 'wp-powersuite' ); ?></p>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
