<?php
/**
 * Small admin UI helpers.
 *
 * @package Vanquish\MultipleAddresses\Com
 */

namespace Vanquish\MultipleAddresses\Com;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Reusable bits of admin markup.
 */
class Html {

	/**
	 * A toggle switch.
	 *
	 * It is a real checkbox underneath, only styled differently: the form submits
	 * exactly as before, and it stays keyboard operable and readable by screen
	 * readers.
	 *
	 * @param string $name    Field name.
	 * @param bool   $checked Whether it is on.
	 * @param string $label   Text shown next to it.
	 * @param string $value   Submitted value.
	 * @param array  $atts    Optional. 'group' names a block of sub-options this
	 *                        toggle reveals (see .vanmuad-suboptions).
	 * @return void
	 */
	public static function toggle( $name, $checked, $label = '', $value = '1', $atts = array() ) {
		$group = isset( $atts['group'] ) ? $atts['group'] : '';
		?>
		<label class="vanmuad-toggle">
			<input
				type="checkbox"
				name="<?php echo esc_attr( $name ); ?>"
				value="<?php echo esc_attr( $value ); ?>"
				<?php if ( '' !== $group ) : ?>
					data-vanmuad-controls="<?php echo esc_attr( $group ); ?>"
				<?php endif; ?>
				<?php checked( (bool) $checked ); ?> />
			<span class="vanmuad-toggle__slider" aria-hidden="true"></span>
			<?php if ( '' !== $label ) : ?>
				<span class="vanmuad-toggle__label"><?php echo esc_html( $label ); ?></span>
			<?php endif; ?>
		</label>
		<?php
	}

	/**
	 * A translatable text input: a single field on a monolingual site, or one field
	 * per active language (each with its flag/code) on a multilingual one, so the
	 * merchant fills every language on the same screen.
	 *
	 * @param string $base_name Base field name.
	 * @param mixed  $stored    Stored value (per-language array or a string).
	 * @param string $default   Default text (placeholder / mono fallback).
	 * @param array  $atts      Optional 'type' and 'class'.
	 * @return void
	 */
	public static function i18n_text( $base_name, $stored, $default = '', $atts = array() ) {
		$type  = isset( $atts['type'] ) ? $atts['type'] : 'text';
		$class = isset( $atts['class'] ) ? $atts['class'] : 'regular-text';

		foreach ( Locale::field_variants( $base_name, $stored, $default ) as $variant ) {
			echo '<p class="vanmuad-i18n-row">';

			// The language flag and code. Escaped at the point of output, not
			// trusted to arrive safe: whatever badge() does today, this line stays
			// safe tomorrow.
			echo wp_kses_post( Locale::badge( $variant ) );

			printf(
				'<input type="%1$s" name="%2$s" value="%3$s" placeholder="%4$s" class="%5$s" />',
				esc_attr( $type ),
				esc_attr( $variant['name'] ),
				esc_attr( $variant['value'] ),
				esc_attr( $variant['placeholder'] ),
				esc_attr( $class )
			);

			echo '</p>';
		}
	}

	/**
	 * A multi-select backed by WooCommerce's own enhanced select (Select2).
	 *
	 * A bare <select multiple> is close to unusable — it needs ctrl+click and gives
	 * no feedback about what is selected — so this reuses the searchable, tag-style
	 * control WooCommerce already ships and loads on its settings screens.
	 *
	 * @param string $name        Field name (without the trailing []).
	 * @param array  $options     value => label.
	 * @param array  $selected    Selected values.
	 * @param string $placeholder Placeholder text.
	 * @return void
	 */
	public static function enhanced_multiselect( $name, array $options, array $selected, $placeholder = '' ) {
		$selected = array_map( 'strval', $selected );

		printf(
			'<select name="%1$s[]" multiple="multiple" class="wc-enhanced-select" data-placeholder="%2$s" style="width:400px;max-width:100%%;">',
			esc_attr( $name ),
			esc_attr( $placeholder )
		);

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

		echo '</select>';
	}

	/**
	 * A customer picker backed by WooCommerce's AJAX customer search.
	 *
	 * Single-select: it names one customer, unlike the multi-selects above.
	 *
	 * @param string $name        Field name.
	 * @param string $placeholder Placeholder text.
	 * @return void
	 */
	public static function customer_search( $name, $placeholder = '' ) {
		printf(
			'<select name="%1$s" class="wc-customer-search" data-placeholder="%2$s" data-allow_clear="true" data-action="woocommerce_json_search_customers" style="width:320px;max-width:100%%;"></select>',
			esc_attr( $name ),
			esc_attr( $placeholder )
		);
	}

	/**
	 * A product picker backed by WooCommerce's AJAX product search.
	 *
	 * Far better than asking the merchant to type comma-separated IDs: they search
	 * by name and never have to look an id up.
	 *
	 * @param string $name        Field name (without the trailing []).
	 * @param int[]  $product_ids Selected product ids.
	 * @param string $placeholder Placeholder text.
	 * @return void
	 */
	public static function product_search( $name, array $product_ids, $placeholder = '' ) {
		printf(
			'<select name="%1$s[]" multiple="multiple" class="wc-product-search" data-placeholder="%2$s" data-action="woocommerce_json_search_products_and_variations" style="width:400px;max-width:100%%;">',
			esc_attr( $name ),
			esc_attr( $placeholder )
		);

		foreach ( $product_ids as $product_id ) {
			$product = wc_get_product( $product_id );

			if ( ! $product ) {
				continue;
			}

			printf(
				'<option value="%1$s" selected="selected">%2$s</option>',
				esc_attr( $product_id ),
				esc_html( wp_strip_all_tags( $product->get_formatted_name() ) )
			);
		}

		echo '</select>';
	}
}
