<?php
/**
 * Field Renderer
 *
 * Renders Settings API field types for the Theme Options UI.
 *
 * @package Kutby
 * @since   1.0.0
 */

declare(strict_types=1);

namespace Kutby\Fields;

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

/**
 * Class Field
 */
final class Field {

	/**
	 * Render a field by type.
	 *
	 * @param array<string, mixed> $args Field arguments.
	 * @return void
	 */
	public static function render( array $args ): void {
		$type = $args['type'] ?? 'text';

		match ( $type ) {
			'textarea'  => self::textarea( $args ),
			'checkbox'  => self::checkbox( $args ),
			'select'    => self::select( $args ),
			'color'     => self::color( $args ),
			'number'    => self::number( $args ),
			'url'       => self::url( $args ),
			'media'     => self::media( $args ),
			'toggle'    => self::toggle( $args ),
			default     => self::text( $args ),
		};
	}

	/**
	 * Text input.
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function text( array $args ): void {
		printf(
			'<input type="text" class="kutby-field kutby-field--text" id="%1$s" name="%2$s" value="%3$s" %4$s />',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' ),
			esc_attr( (string) ( $args['value'] ?? '' ) ),
			! empty( $args['placeholder'] ) ? 'placeholder="' . esc_attr( (string) $args['placeholder'] ) . '"' : ''
		);
		self::description( $args );
	}

	/**
	 * URL input.
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function url( array $args ): void {
		printf(
			'<input type="url" class="kutby-field kutby-field--url" id="%1$s" name="%2$s" value="%3$s" />',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' ),
			esc_url( (string) ( $args['value'] ?? '' ) )
		);
		self::description( $args );
	}

	/**
	 * Number input.
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function number( array $args ): void {
		printf(
			'<input type="number" class="kutby-field kutby-field--number" id="%1$s" name="%2$s" value="%3$s" min="%4$s" max="%5$s" step="%6$s" />',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' ),
			esc_attr( (string) ( $args['value'] ?? '' ) ),
			esc_attr( (string) ( $args['min'] ?? '0' ) ),
			esc_attr( (string) ( $args['max'] ?? '9999' ) ),
			esc_attr( (string) ( $args['step'] ?? '1' ) )
		);
		self::description( $args );
	}

	/**
	 * Textarea.
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function textarea( array $args ): void {
		printf(
			'<textarea class="kutby-field kutby-field--textarea" id="%1$s" name="%2$s" rows="%3$d">%4$s</textarea>',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' ),
			(int) ( $args['rows'] ?? 4 ),
			esc_textarea( (string) ( $args['value'] ?? '' ) )
		);
		self::description( $args );
	}

	/**
	 * Checkbox.
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function checkbox( array $args ): void {
		printf(
			'<label class="kutby-field kutby-field--checkbox"><input type="checkbox" id="%1$s" name="%2$s" value="1" %3$s /> <span>%4$s</span></label>',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' ),
			checked( ! empty( $args['value'] ), true, false ),
			esc_html( (string) ( $args['label'] ?? '' ) )
		);
		self::description( $args );
	}

	/**
	 * Toggle switch (checkbox styled).
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function toggle( array $args ): void {
		printf(
			'<label class="kutby-toggle"><input type="checkbox" id="%1$s" name="%2$s" value="1" %3$s /><span class="kutby-toggle__track" aria-hidden="true"></span><span class="kutby-toggle__label">%4$s</span></label>',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' ),
			checked( ! empty( $args['value'] ), true, false ),
			esc_html( (string) ( $args['label'] ?? '' ) )
		);
		self::description( $args );
	}

	/**
	 * Select dropdown.
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function select( array $args ): void {
		$options = is_array( $args['options'] ?? null ) ? $args['options'] : array();
		$current = (string) ( $args['value'] ?? '' );

		printf(
			'<select class="kutby-field kutby-field--select" id="%1$s" name="%2$s">',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' )
		);

		foreach ( $options as $val => $label ) {
			printf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( (string) $val ),
				selected( $current, (string) $val, false ),
				esc_html( (string) $label )
			);
		}

		echo '</select>';
		self::description( $args );
	}

	/**
	 * Color picker (native + WP color picker ready).
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function color( array $args ): void {
		printf(
			'<input type="text" class="kutby-field kutby-field--color kutby-color-picker" id="%1$s" name="%2$s" value="%3$s" data-default-color="%3$s" />',
			esc_attr( $args['id'] ?? '' ),
			esc_attr( $args['name'] ?? '' ),
			esc_attr( (string) ( $args['value'] ?? '#000000' ) )
		);
		self::description( $args );
	}

	/**
	 * Media attachment picker (stores attachment ID).
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function media( array $args ): void {
		echo \Kutby\Admin\UI\Components::media_upload( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in Components.
			array(
				'id'            => $args['id'] ?? '',
				'name'          => $args['name'] ?? '',
				'value'         => $args['value'] ?? 0,
				'select_label'  => 'اختيار صورة',
				'change_label'  => 'تعديل',
				'remove_label'  => 'حذف',
			)
		);
		self::description( $args );
	}

	/**
	 * Print field description.
	 *
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	private static function description( array $args ): void {
		if ( empty( $args['description'] ) ) {
			return;
		}

		printf(
			'<p class="kutby-field__desc">%s</p>',
			esc_html( (string) $args['description'] )
		);
	}
}
