<?php
/**
 * Theme settings page and option helpers.
 *
 * @package wuppi
 */

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

/**
 * Return theme settings defaults.
 *
 * @return array<string, int|string>
 */
function wuppi_get_settings_defaults() {
	return array(
		'back_to_top'             => 1,
		'header_height'           => 72,
		'header_nav_density'      => 'compact',
		'header_nav_gap'          => 14,
		'header_desktop_behavior' => 'scroll',
		'header_mobile_behavior'  => 'sticky-glass',
		'header_auto_hide_delay'  => 900,
	);
}

/**
 * Get merged theme settings.
 *
 * @return array<string, int|string>
 */
function wuppi_get_settings() {
	$defaults = wuppi_get_settings_defaults();
	$stored   = get_option( 'wuppi_settings', array() );

	if ( ! is_array( $stored ) ) {
		$stored = array();
	}

	return wp_parse_args( $stored, $defaults );
}

/**
 * Return the capability required to manage Wuppi settings.
 *
 * @return string
 */
function wuppi_get_settings_page_capability() {
	return 'edit_theme_options';
}

/**
 * Register theme settings page.
 */
function wuppi_add_settings_page() {
	$hook_suffix = add_theme_page(
		esc_html__( 'Wuppi Settings', 'wuppi' ),
		esc_html__( 'Wuppi Settings', 'wuppi' ),
		wuppi_get_settings_page_capability(),
		'wuppi-settings',
		'wuppi_render_settings_page'
	);

	if ( $hook_suffix ) {
		add_action( 'load-' . $hook_suffix, 'wuppi_add_settings_screen_help' );
	}
}
add_action( 'admin_menu', 'wuppi_add_settings_page' );

/**
 * Ensure the settings form uses the theme capability instead of manage_options.
 *
 * @return string
 */
function wuppi_filter_settings_page_capability() {
	return wuppi_get_settings_page_capability();
}
add_filter( 'option_page_capability_wuppi_settings_group', 'wuppi_filter_settings_page_capability' );

/**
 * Register contextual help for the Wuppi settings screen.
 */
function wuppi_add_settings_screen_help() {
	if ( ! function_exists( 'get_current_screen' ) ) {
		return;
	}

	$screen = get_current_screen();
	if ( ! $screen ) {
		return;
	}

	$help_url = admin_url( 'themes.php?page=wuppi-help' );
	$content  = '<p>' . esc_html__( 'Use this screen to adjust Wuppi header behavior without changing template content in the Site Editor.', 'wuppi' ) . '</p>';
	$content .= '<ul>';
	$content .= '<li>' . esc_html__( 'Header height and navigation spacing control the overall header proportions.', 'wuppi' ) . '</li>';
	$content .= '<li>' . esc_html__( 'Desktop and mobile header behavior settings control how the header reacts on different screen sizes.', 'wuppi' ) . '</li>';
	$content .= '<li>' . esc_html__( 'Open the full help page for setup guidance covering the Site Editor and WooCommerce.', 'wuppi' ) . '</li>';
	$content .= '</ul>';
	$content .= '<p><a href="' . esc_url( $help_url ) . '">' . esc_html__( 'Open Wuppi Help', 'wuppi' ) . '</a></p>';

	$screen->add_help_tab(
		array(
			'id'      => 'wuppi-settings-help',
			'title'   => esc_html__( 'Wuppi Help', 'wuppi' ),
			'content' => $content,
		)
	);
}

/**
 * Register settings, sections, and fields.
 */
function wuppi_register_settings() {
	register_setting(
		'wuppi_settings_group',
		'wuppi_settings',
		array(
			'type'              => 'array',
			'sanitize_callback' => 'wuppi_sanitize_settings',
			'default'           => wuppi_get_settings_defaults(),
		)
	);

	add_settings_section(
		'wuppi_general_section',
		esc_html__( 'General', 'wuppi' ),
		'wuppi_render_general_section',
		'wuppi-settings'
	);

	add_settings_field(
		'back_to_top',
		esc_html__( 'Back to top button', 'wuppi' ),
		'wuppi_render_back_to_top_field',
		'wuppi-settings',
		'wuppi_general_section'
	);

	add_settings_section(
		'wuppi_header_section',
		esc_html__( 'Header', 'wuppi' ),
		'wuppi_render_header_section',
		'wuppi-settings'
	);

	add_settings_field(
		'header_height',
		esc_html__( 'Header height (px)', 'wuppi' ),
		'wuppi_render_header_height_field',
		'wuppi-settings',
		'wuppi_header_section'
	);

	add_settings_field(
		'header_nav_density',
		esc_html__( 'Navigation density', 'wuppi' ),
		'wuppi_render_nav_density_field',
		'wuppi-settings',
		'wuppi_header_section'
	);

	add_settings_field(
		'header_nav_gap',
		esc_html__( 'Navigation link spacing (px)', 'wuppi' ),
		'wuppi_render_nav_gap_field',
		'wuppi-settings',
		'wuppi_header_section'
	);

	add_settings_field(
		'header_desktop_behavior',
		esc_html__( 'Desktop header behavior', 'wuppi' ),
		'wuppi_render_header_desktop_behavior_field',
		'wuppi-settings',
		'wuppi_header_section'
	);

	add_settings_field(
		'header_mobile_behavior',
		esc_html__( 'Mobile header behavior', 'wuppi' ),
		'wuppi_render_header_mobile_behavior_field',
		'wuppi-settings',
		'wuppi_header_section'
	);

	add_settings_field(
		'header_auto_hide_delay',
		esc_html__( 'Auto-hide reveal delay', 'wuppi' ),
		'wuppi_render_header_auto_hide_delay_field',
		'wuppi-settings',
		'wuppi_header_section'
	);
}
add_action( 'admin_init', 'wuppi_register_settings' );

/**
 * Sanitize theme settings values.
 *
 * @param mixed $input Settings input.
 * @return array<string, int|string>
 */
function wuppi_sanitize_settings( $input ) {
	$defaults = wuppi_get_settings_defaults();
	$output   = $defaults;

	if ( ! is_array( $input ) ) {
		return $output;
	}

	$output['back_to_top'] = isset( $input['back_to_top'] ) && absint( $input['back_to_top'] ) ? 1 : 0;

	$header_height = isset( $input['header_height'] ) ? absint( $input['header_height'] ) : (int) $defaults['header_height'];
	$output['header_height'] = min( 120, max( 56, $header_height ) );

	$density = isset( $input['header_nav_density'] ) ? sanitize_key( (string) $input['header_nav_density'] ) : (string) $defaults['header_nav_density'];
	$output['header_nav_density'] = in_array( $density, array( 'compact', 'comfortable' ), true ) ? $density : $defaults['header_nav_density'];

	$nav_gap = isset( $input['header_nav_gap'] ) ? absint( $input['header_nav_gap'] ) : (int) $defaults['header_nav_gap'];
	$output['header_nav_gap'] = min( 48, max( 0, $nav_gap ) );

	$header_behavior = isset( $input['header_desktop_behavior'] ) ? sanitize_key( (string) $input['header_desktop_behavior'] ) : (string) $defaults['header_desktop_behavior'];
	$output['header_desktop_behavior'] = in_array( $header_behavior, array( 'scroll', 'fixed-glass', 'fixed-auto-hide' ), true ) ? $header_behavior : $defaults['header_desktop_behavior'];

	$mobile_behavior = isset( $input['header_mobile_behavior'] ) ? sanitize_key( (string) $input['header_mobile_behavior'] ) : (string) $defaults['header_mobile_behavior'];
	$output['header_mobile_behavior'] = in_array( $mobile_behavior, array( 'sticky-glass', 'static' ), true ) ? $mobile_behavior : $defaults['header_mobile_behavior'];

	$auto_hide_delay = isset( $input['header_auto_hide_delay'] ) ? absint( $input['header_auto_hide_delay'] ) : (int) $defaults['header_auto_hide_delay'];
	$output['header_auto_hide_delay'] = in_array( $auto_hide_delay, array( 600, 900, 1400 ), true ) ? $auto_hide_delay : (int) $defaults['header_auto_hide_delay'];

	return $output;
}

/**
 * Render general settings section intro.
 */
function wuppi_render_general_section() {
	echo '<p>' . esc_html__( 'Global theme features.', 'wuppi' ) . '</p>';
}

/**
 * Render back to top field.
 */
function wuppi_render_back_to_top_field() {
	$settings = wuppi_get_settings();
	$checked  = ! empty( $settings['back_to_top'] );

	// Hidden field ensures a value is always submitted, even when the checkbox is unchecked.
	printf(
		'<input type="hidden" name="wuppi_settings[back_to_top]" value="0" /><label><input type="checkbox" name="wuppi_settings[back_to_top]" value="1"%1$s /> %2$s</label>',
		checked( $checked, true, false ),
		esc_html__( 'Show a fixed button that scrolls back to the top of the page.', 'wuppi' )
	);
}

/**
 * Render header settings section intro.
 */
function wuppi_render_header_section() {
	echo '<p>' . esc_html__( 'Control header sizing, navigation spacing, and desktop/mobile header behavior used by the theme header template part.', 'wuppi' ) . '</p>';
}

/**
 * Render header height field.
 */
function wuppi_render_header_height_field() {
	$settings = wuppi_get_settings();
	$value    = (int) $settings['header_height'];

	printf(
		'<input type="number" class="small-text" min="56" max="120" step="1" name="wuppi_settings[header_height]" value="%1$d" /> <span>%2$s</span>',
		$value,
		esc_html__( 'Minimum sticky header height.', 'wuppi' )
	);
}

/**
 * Render navigation density field.
 */
function wuppi_render_nav_density_field() {
	$settings = wuppi_get_settings();
	$value    = (string) $settings['header_nav_density'];
	$options  = array(
		'compact'     => esc_html__( 'Compact', 'wuppi' ),
		'comfortable' => esc_html__( 'Comfortable', 'wuppi' ),
	);

	echo '<select name="wuppi_settings[header_nav_density]">';
	foreach ( $options as $option_value => $label ) {
		printf(
			'<option value="%1$s"%2$s>%3$s</option>',
			esc_attr( $option_value ),
			selected( $value, $option_value, false ),
			esc_html( $label )
		);
	}
	echo '</select>';
}

/**
 * Render navigation gap field.
 */
function wuppi_render_nav_gap_field() {
	$settings = wuppi_get_settings();
	$value    = isset( $settings['header_nav_gap'] ) ? (int) $settings['header_nav_gap'] : 14;

	printf(
		'<input type="number" class="small-text" min="0" max="48" step="1" name="wuppi_settings[header_nav_gap]" value="%1$d" /> <span>%2$s</span>',
		$value,
		esc_html__( 'Horizontal spacing between desktop navigation links.', 'wuppi' )
	);
}

/**
 * Render desktop header behavior field.
 */
function wuppi_render_header_desktop_behavior_field() {
	$settings = wuppi_get_settings();
	$value    = (string) $settings['header_desktop_behavior'];
	$options  = array(
		'scroll'          => esc_html__( 'Scroll with page', 'wuppi' ),
		'fixed-glass'     => esc_html__( 'Fixed with blur on scroll', 'wuppi' ),
		'fixed-auto-hide' => esc_html__( 'Fixed auto-hide (show while scrolling)', 'wuppi' ),
	);

	echo '<select name="wuppi_settings[header_desktop_behavior]">';
	foreach ( $options as $option_value => $label ) {
		printf(
			'<option value="%1$s"%2$s>%3$s</option>',
			esc_attr( $option_value ),
			selected( $value, $option_value, false ),
			esc_html( $label )
		);
	}
	echo '</select>';
	echo '<p class="description">' . esc_html__( 'Applied on desktop only. Mobile uses the mobile header behavior setting below.', 'wuppi' ) . '</p>';
}

/**
 * Render mobile header behavior field.
 */
function wuppi_render_header_mobile_behavior_field() {
	$settings = wuppi_get_settings();
	$value    = isset( $settings['header_mobile_behavior'] ) ? (string) $settings['header_mobile_behavior'] : 'sticky-glass';
	$options  = array(
		'sticky-glass' => esc_html__( 'Sticky with glass effect', 'wuppi' ),
		'static'       => esc_html__( 'Static', 'wuppi' ),
	);

	echo '<select name="wuppi_settings[header_mobile_behavior]">';
	foreach ( $options as $option_value => $label ) {
		printf(
			'<option value="%1$s"%2$s>%3$s</option>',
			esc_attr( $option_value ),
			selected( $value, $option_value, false ),
			esc_html( $label )
		);
	}
	echo '</select>';
	echo '<p class="description">' . esc_html__( 'Applied below 782px wide screens.', 'wuppi' ) . '</p>';
}

/**
 * Render header auto-hide delay field.
 */
function wuppi_render_header_auto_hide_delay_field() {
	$settings = wuppi_get_settings();
	$value    = isset( $settings['header_auto_hide_delay'] ) ? (int) $settings['header_auto_hide_delay'] : 900;
	$options  = array(
		600  => esc_html__( 'Fast', 'wuppi' ),
		900  => esc_html__( 'Default', 'wuppi' ),
		1400 => esc_html__( 'Slow', 'wuppi' ),
	);

	echo '<select name="wuppi_settings[header_auto_hide_delay]">';
	foreach ( $options as $option_value => $label ) {
		printf(
			'<option value="%1$d"%2$s>%3$s</option>',
			(int) $option_value,
			selected( $value, (int) $option_value, false ),
			esc_html( $label )
		);
	}
	echo '</select>';
	echo '<p class="description">' . esc_html__( 'Used only when Desktop header behavior is set to Fixed auto-hide.', 'wuppi' ) . '</p>';
}

/**
 * Render theme settings page.
 */
function wuppi_render_settings_page() {
	if ( ! current_user_can( wuppi_get_settings_page_capability() ) ) {
		return;
	}

	$help_url = admin_url( 'themes.php?page=wuppi-help' );
	?>
	<div class="wrap">
		<h1 class="wp-heading-inline"><?php echo esc_html__( 'Wuppi Settings', 'wuppi' ); ?></h1>
		<a class="page-title-action" href="<?php echo esc_url( $help_url ); ?>"><?php echo esc_html__( 'Help', 'wuppi' ); ?></a>
		<hr class="wp-header-end">
		<form action="options.php" method="post">
			<?php
			settings_fields( 'wuppi_settings_group' );
			do_settings_sections( 'wuppi-settings' );
			submit_button();
			?>
		</form>
	</div>
	<?php
}
