<?php
/**
 * Buttons settings tab.
 *
 * @package Rsmspark_Content_Widgets
 *
 * @var array<string, mixed> $settings
 */

defined( 'ABSPATH' ) || exit;

$effective    = Rsmspark_Buttons_Settings::get_effective_new_button_defaults();
$theme_summary = Rsmspark_Buttons_Theme_Defaults::get_summary();
$theme_colors  = $theme_summary['colors'];
?>
<?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( __( 'Settings saved.', 'rsm-content-widgets' ) ); ?>
<?php endif; ?>

<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
	<?php wp_nonce_field( 'rsmspark_buttons_settings_save' ); ?>
	<input type="hidden" name="action" value="rsmspark_buttons_settings_save" />

	<h3 class="rsm-settings-section-title"><?php esc_html_e( 'General', 'rsm-content-widgets' ); ?></h3>
	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'List table', 'rsm-content-widgets' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="rsmspark_buttons_settings[show_list_preview]" value="1" <?php checked( ! empty( $settings['show_list_preview'] ) ); ?> />
					<?php esc_html_e( 'Show live preview column on the All Buttons tab', 'rsm-content-widgets' ); ?>
				</label>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Theme colors', 'rsm-content-widgets' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="rsmspark_buttons_settings[use_theme_defaults]" value="1" <?php checked( ! empty( $settings['use_theme_defaults'] ) ); ?> />
					<?php esc_html_e( 'Use active theme button colors for new-button defaults when available', 'rsm-content-widgets' ); ?>
				</label>
				<?php if ( ! empty( $theme_colors ) ) : ?>
					<p class="description">
						<?php
						printf(
							/* translators: 1: theme source label, 2: comma-separated hex colors */
							esc_html__( 'Detected from %1$s: %2$s. Theme.json is used when the theme defines button element colors; otherwise a few common customizer keys are tried. Classic themes without those hooks keep plugin defaults — use the fields below to override.', 'rsm-content-widgets' ),
							esc_html( (string) $theme_summary['source'] ),
							esc_html( implode( ', ', array_values( $theme_colors ) ) )
						);
						?>
					</p>
				<?php else : ?>
					<p class="description"><?php esc_html_e( 'No theme button colors were detected. Plugin defaults apply until you save custom values below.', 'rsm-content-widgets' ); ?></p>
				<?php endif; ?>
			</td>
		</tr>
	</table>

	<h3 class="rsm-settings-section-title"><?php esc_html_e( 'New Button Defaults', 'rsm-content-widgets' ); ?></h3>
	<p class="description"><?php esc_html_e( 'Pre-fill the builder when you create a new button. Saved values here override theme detection.', 'rsm-content-widgets' ); ?></p>

	<table class="form-table" role="presentation">
		<?php
		$color_fields = array(
			'bg_color'         => __( 'Background', 'rsm-content-widgets' ),
			'bg_hover_color'   => __( 'Background hover', 'rsm-content-widgets' ),
			'text_color'       => __( 'Text', 'rsm-content-widgets' ),
			'text_hover_color' => __( 'Text hover', 'rsm-content-widgets' ),
		);
		foreach ( $color_fields as $key => $label ) :
			?>
			<tr>
				<th scope="row"><label for="rsmspark_buttons_default_<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $label ); ?></label></th>
				<td>
					<input type="text" class="rsm-color-picker" id="rsmspark_buttons_default_<?php echo esc_attr( $key ); ?>" name="rsmspark_buttons_settings[new_button_defaults][<?php echo esc_attr( $key ); ?>]" value="<?php echo esc_attr( (string) ( $effective[ $key ] ?? '' ) ); ?>" />
				</td>
			</tr>
		<?php endforeach; ?>
		<tr>
			<th scope="row"><label for="rsmspark_buttons_default_shape"><?php esc_html_e( 'Shape', 'rsm-content-widgets' ); ?></label></th>
			<td>
				<select id="rsmspark_buttons_default_shape" name="rsmspark_buttons_settings[new_button_defaults][shape]">
					<option value="rectangle" <?php selected( $effective['shape'], 'rectangle' ); ?>><?php esc_html_e( 'Rectangle', 'rsm-content-widgets' ); ?></option>
					<option value="rounded" <?php selected( $effective['shape'], 'rounded' ); ?>><?php esc_html_e( 'Rounded', 'rsm-content-widgets' ); ?></option>
					<option value="pill" <?php selected( $effective['shape'], 'pill' ); ?>><?php esc_html_e( 'Pill', 'rsm-content-widgets' ); ?></option>
				</select>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="rsmspark_buttons_default_hover_effect"><?php esc_html_e( 'Hover effect', 'rsm-content-widgets' ); ?></label></th>
			<td>
				<select id="rsmspark_buttons_default_hover_effect" name="rsmspark_buttons_settings[new_button_defaults][hover_effect]">
					<?php foreach ( array( 'none', 'lift', 'scale', 'glow', 'lift-glow' ) as $effect ) : ?>
						<option value="<?php echo esc_attr( $effect ); ?>" <?php selected( $effective['hover_effect'], $effect ); ?>><?php echo esc_html( ucfirst( str_replace( '-', ' + ', $effect ) ) ); ?></option>
					<?php endforeach; ?>
				</select>
			</td>
		</tr>
	</table>

	<?php submit_button( __( 'Save Settings', 'rsm-content-widgets' ) ); ?>
</form>
