<?php
/**
 * Coming Soon & Maintenance – admin form helpers.
 *
 * @package ComingSoonMaintenance
 */

namespace ComingSoonMaintenance;

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

final class AdminHelpers {

	/**
	 * Normalize a hex color for picker + text inputs.
	 *
	 * @return array{picker: string, hex: string}
	 */
	public static function normalizeHexColor( string $raw, string $fallback ): array {
		if ( ! preg_match( '/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/', $raw ) ) {
			$raw = $fallback;
		}
		$picker = strlen( $raw ) > 7 ? substr( $raw, 0, 7 ) : $raw;

		return [
			'picker' => $picker,
			'hex'    => $raw,
		];
	}

	/**
	 * Render a color field row matching Login Page Customizor.
	 */
	public static function colorField(
		string $module_id,
		string $key,
		string $value,
		string $default,
		string $input_id = ''
	): void {
		$input_id = $input_id !== '' ? $input_id : $module_id . '_' . $key;
		$colors   = self::normalizeHexColor( $value, $default );
		?>
		<div class="wpps-color-field-row">
			<input
				type="color"
				id="<?php echo esc_attr( $input_id ); ?>"
				name="<?php echo esc_attr( $key ); ?>"
				value="<?php echo esc_attr( $colors['picker'] ); ?>"
				class="wp-powersuite-advanced-setting wpps-color-picker-circle"
				data-setting-key="<?php echo esc_attr( $key ); ?>"
			/>
			<div class="wpps-color-code-wrap">
				<input
					type="text"
					class="wpps-color-code-input regular-text wp-powersuite-advanced-setting"
					data-setting-key="<?php echo esc_attr( $key ); ?>"
					data-color-input="<?php echo esc_attr( $input_id ); ?>"
					data-default-color="<?php echo esc_attr( $default ); ?>"
					value="<?php echo esc_attr( $colors['hex'] ); ?>"
					style="width: 100px; padding-right: 25px;"
					placeholder="<?php echo esc_attr( $default ); ?>"
					maxlength="9"
				/>
				<button type="button" class="wpps-color-reset button-link" data-default-color="<?php echo esc_attr( $default ); ?>">
					<span class="dashicons dashicons-undo" style="font-size: 16px; width: 16px; height: 16px;"></span>
				</button>
			</div>
		</div>
		<?php
	}

	/**
	 * Optional color field — empty value means theme default on frontend.
	 */
	public static function optionalColorField(
		string $module_id,
		string $key,
		string $value,
		string $placeholder_default,
		string $input_id = ''
	): void {
		$text_id    = $input_id !== '' ? $input_id : $module_id . '_' . $key;
		$picker_id  = $text_id . '_picker';
		$has_value  = $value !== '' && preg_match( '/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/', $value );
		$picker_val = $has_value ? ( strlen( $value ) > 7 ? substr( $value, 0, 7 ) : $value ) : ( strlen( $placeholder_default ) > 7 ? substr( $placeholder_default, 0, 7 ) : $placeholder_default );
		?>
		<div class="wpps-color-field-row wpps-color-field-row--optional">
			<input
				type="color"
				id="<?php echo esc_attr( $picker_id ); ?>"
				value="<?php echo esc_attr( $picker_val ); ?>"
				class="wpps-color-picker-circle"
			/>
			<div class="wpps-color-code-wrap">
				<input
					type="text"
					id="<?php echo esc_attr( $text_id ); ?>"
					name="<?php echo esc_attr( $key ); ?>"
					class="wpps-color-code-input regular-text wp-powersuite-advanced-setting"
					data-setting-key="<?php echo esc_attr( $key ); ?>"
					data-color-input="<?php echo esc_attr( $picker_id ); ?>"
					data-default-color=""
					value="<?php echo esc_attr( $has_value ? $value : '' ); ?>"
					style="width: 100px; padding-right: 25px;"
					placeholder="<?php echo esc_attr( $placeholder_default ); ?>"
					maxlength="9"
				/>
				<button type="button" class="wpps-color-reset button-link" data-default-color="" title="<?php esc_attr_e( 'Clear', 'wp-powersuite' ); ?>">
					<span class="dashicons dashicons-undo" style="font-size: 16px; width: 16px; height: 16px;"></span>
				</button>
			</div>
			<p class="description" style="margin: 4px 0 0;"><?php esc_html_e( 'Leave blank to use the template default.', 'wp-powersuite' ); ?></p>
		</div>
		<?php
	}
}
