<?php
/**
 * Rich text editor settings tab.
 *
 * @package Rsmspark_Content_Widgets
 *
 * @var array<string, mixed> $settings Plugin settings.
 */

defined( 'ABSPATH' ) || exit;

$rsm_media_active   = class_exists( 'RSM_Media_Picker', false ) && RSM_Media_Picker::is_enabled();
$calendar_active    = class_exists( 'RSM_Calendar_Classic_Editor_Settings', false );
$standards          = class_exists( 'Rsmspark_Rich_Text_Standards', false ) ? Rsmspark_Rich_Text_Standards::get_saved() : array();
$can_edit_standards = class_exists( 'Rsmspark_Rich_Text_Standards', false ) && Rsmspark_Rich_Text_Standards::can_configure();
$is_pro_feature     = class_exists( 'Rsmspark_Rich_Text_Standards', false ) && Rsmspark_Rich_Text_Standards::is_pro_feature();
$slot_count         = class_exists( 'Rsmspark_Rich_Text_Standards', false ) ? Rsmspark_Rich_Text_Standards::SLOT_COUNT : 12;

?>
<form method="post" action="<?php echo esc_url( Rsmspark_Content_Widgets_Admin::settings_url( 'editor' ) ); ?>">
	<?php wp_nonce_field( 'rsmspark_content_widgets_save_settings', 'rsmspark_content_widgets_settings_nonce' ); ?>
	<input type="hidden" name="rsm_settings_tab" value="editor" />

	<?php
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin notice flag; no state change.
	$settings_updated = isset( $_GET['updated'] ) ? sanitize_text_field( wp_unslash( (string) $_GET['updated'] ) ) : '';
	?>
	<?php if ( '1' === $settings_updated ) : ?>
		<?php rsmspark_suite_render_admin_notice( __( 'Editor settings saved.', 'rsm-content-widgets' ) ); ?>
	<?php endif; ?>

	<?php
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin notice flag; no state change.
	$settings_reset = isset( $_GET['reset'] ) ? sanitize_text_field( wp_unslash( (string) $_GET['reset'] ) ) : '';
	?>
	<?php if ( '1' === $settings_reset ) : ?>
		<?php rsmspark_suite_render_admin_notice( __( 'Site standards reset to defaults.', 'rsm-content-widgets' ) ); ?>
	<?php endif; ?>

	<h2 class="rsm-settings-section-title"><?php esc_html_e( 'RSM Editor (TinyMCE)', 'rsm-content-widgets' ); ?></h2>
	<p class="description"><?php esc_html_e( 'Applies to Headline and other RSM profile rich-text fields (SiteOrigin widget forms and related admin editors).', 'rsm-content-widgets' ); ?></p>

	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'Media row', 'rsm-content-widgets' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="rsmspark_content_widgets_editor_media_row" value="1" <?php checked( ! empty( $settings['editor_media_row'] ) ); ?> />
					<?php esc_html_e( 'Show media buttons above profile editors', 'rsm-content-widgets' ); ?>
				</label>
				<p class="description"><?php esc_html_e( 'Off by default — keeps Headline fields focused on typography tokens. Enable when you need inline images in body-style fields.', 'rsm-content-widgets' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Image picker', 'rsm-content-widgets' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="rsmspark_content_widgets_editor_use_rsm_media" value="1" <?php checked( ! empty( $settings['editor_use_rsm_media'] ) ); ?> <?php disabled( ! $rsm_media_active ); ?> />
					<?php esc_html_e( 'Use RSM Media picker instead of WordPress “Add Media”', 'rsm-content-widgets' ); ?>
				</label>
				<?php if ( ! $rsm_media_active ) : ?>
					<p class="description"><?php esc_html_e( 'Install and enable RSM Media → Media picker to use this option.', 'rsm-content-widgets' ); ?></p>
				<?php else : ?>
					<p class="description"><?php esc_html_e( 'Replaces the core library modal with the RSM Media modal on profile editors only.', 'rsm-content-widgets' ); ?></p>
				<?php endif; ?>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Calendar button', 'rsm-content-widgets' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="rsmspark_content_widgets_editor_calendar_button" value="1" <?php checked( ! empty( $settings['editor_calendar_button'] ) ); ?> <?php disabled( ! $calendar_active ); ?> />
					<?php esc_html_e( 'Show “Add RSM Calendar” in profile editor media row', 'rsm-content-widgets' ); ?>
				</label>
				<?php if ( ! $calendar_active ) : ?>
					<p class="description"><?php esc_html_e( 'Requires the RSM Calendar plugin.', 'rsm-content-widgets' ); ?></p>
				<?php else : ?>
					<p class="description">
						<?php
						esc_html_e(
							'Calendar also has a global toggle under RSM → Calendar → Settings → Classic editor.',
							'rsm-content-widgets'
						);
						?>
					</p>
				<?php endif; ?>
			</td>
		</tr>
	</table>

	<h2 class="rsm-settings-section-title"><?php esc_html_e( 'Site standards', 'rsm-content-widgets' ); ?></h2>
	<p class="description">
		<?php esc_html_e( 'Configure up to 12 named colors and 12 named font sizes for the Size, Color, and Highlight toolbar menus. When you save any rows here, those become the editor palette (replacing suite defaults until reset). Custom px and hex in the toolbar is always available.', 'rsm-content-widgets' ); ?>
	</p>

	<?php if ( $is_pro_feature ) : ?>
		<p class="description">
			<?php esc_html_e( 'Site standards editing ships with Content Widgets Pro (separate plugin from rsmspark.com).', 'rsm-content-widgets' ); ?>
		</p>
	<?php endif; ?>

	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'Site standards only', 'rsm-content-widgets' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="rsmspark_editor_site_standards_only" value="1" <?php checked( ! empty( $standards['site_standards_only'] ) ); ?> <?php disabled( ! $can_edit_standards ); ?> />
					<?php esc_html_e( 'Use only configured site colors and sizes (skip theme detection and suite defaults)', 'rsm-content-widgets' ); ?>
				</label>
			</td>
		</tr>
	</table>

	<h3><?php esc_html_e( 'Colors', 'rsm-content-widgets' ); ?></h3>
	<table class="widefat striped rsmspark-editor-standards-table" style="max-width:720px;margin-bottom:1.5em;">
		<thead>
			<tr>
				<th scope="col"><?php esc_html_e( 'Name', 'rsm-content-widgets' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Hex', 'rsm-content-widgets' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php for ( $i = 0; $i < $slot_count; $i++ ) : ?>
				<?php
				$row = $standards['colors'][ $i ] ?? array( 'name' => '', 'hex' => '' );
				?>
				<tr>
					<td>
						<input type="text" class="regular-text" name="rsmspark_editor_standard_color_name_<?php echo esc_attr( (string) $i ); ?>" value="<?php echo esc_attr( (string) ( $row['name'] ?? '' ) ); ?>" <?php disabled( ! $can_edit_standards ); ?> />
					</td>
					<td>
						<input type="text" class="regular-text code" name="rsmspark_editor_standard_color_hex_<?php echo esc_attr( (string) $i ); ?>" value="<?php echo esc_attr( (string) ( $row['hex'] ?? '' ) ); ?>" placeholder="#2563eb" <?php disabled( ! $can_edit_standards ); ?> />
					</td>
				</tr>
			<?php endfor; ?>
		</tbody>
	</table>

	<h3><?php esc_html_e( 'Font sizes', 'rsm-content-widgets' ); ?></h3>
	<table class="widefat striped rsmspark-editor-standards-table" style="max-width:720px;margin-bottom:1.5em;">
		<thead>
			<tr>
				<th scope="col"><?php esc_html_e( 'Name', 'rsm-content-widgets' ); ?></th>
				<th scope="col"><?php esc_html_e( 'Size (px)', 'rsm-content-widgets' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php for ( $i = 0; $i < $slot_count; $i++ ) : ?>
				<?php
				$row = $standards['sizes'][ $i ] ?? array( 'name' => '', 'px' => 0 );
				?>
				<tr>
					<td>
						<input type="text" class="regular-text" name="rsmspark_editor_standard_size_name_<?php echo esc_attr( (string) $i ); ?>" value="<?php echo esc_attr( (string) ( $row['name'] ?? '' ) ); ?>" <?php disabled( ! $can_edit_standards ); ?> />
					</td>
					<td>
						<input type="number" class="small-text" min="8" max="120" step="1" name="rsmspark_editor_standard_size_px_<?php echo esc_attr( (string) $i ); ?>" value="<?php echo esc_attr( (string) ( $row['px'] ?? '' ) ); ?>" <?php disabled( ! $can_edit_standards ); ?> />
					</td>
				</tr>
			<?php endfor; ?>
		</tbody>
	</table>

	<p class="description">
		<?php esc_html_e( 'Font families come from your theme via the rsmspark_editor_font_families filter. Use the Font toolbar control for custom stacks.', 'rsm-content-widgets' ); ?>
	</p>

	<?php submit_button( __( 'Save editor settings', 'rsm-content-widgets' ) ); ?>

	<?php if ( $can_edit_standards ) : ?>
		<p style="margin-top:1.5em;">
			<button
				type="submit"
				name="rsmspark_editor_reset_standards"
				value="1"
				class="button button-secondary"
				onclick="return window.confirm('<?php echo esc_js( __( 'Reset all site color and font-size standards to defaults? This cannot be undone.', 'rsm-content-widgets' ) ); ?>');"
			>
				<?php esc_html_e( 'Reset site standards to defaults', 'rsm-content-widgets' ); ?>
			</button>
		</p>
	<?php endif; ?>
</form>
