<?php

namespace AutomateWoo\Fields;

use AutomateWoo\Clean;

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

/**
 * @class Select
 */
class Select extends Field {

	/** @var string */
	protected $name = 'select';

	/** @var string */
	protected $type = 'select';

	/** @var mixed */
	protected $default_option;

	/** @var bool */
	public $multiple = false;

	/** @var array */
	protected $options = [];

	/** @var string */
	public $dynamic_options_reference_field_name;


	/**
	 * @param bool $show_placeholder
	 */
	public function __construct( $show_placeholder = true ) {
		parent::__construct();

		$this->set_title( __( 'Select', 'automatewoo' ) );

		if ( $show_placeholder ) {
			$this->set_placeholder( __( '[Select]', 'automatewoo' ) );
		}
	}


	/**
	 * @param array $options
	 * @return $this
	 */
	public function set_options( $options ) {
		$this->options = $options;
		return $this;
	}


	/**
	 * @return array
	 */
	public function get_options() {
		return $this->options;
	}


	/**
	 * @param mixed $option
	 * @return $this
	 */
	public function set_default( $option ) {
		$this->default_option = $option;
		return $this;
	}


	/**
	 * @param bool $multi
	 * @return $this
	 */
	public function set_multiple( $multi = true ) {
		$this->multiple = $multi;
		return $this;
	}


	/**
	 * @param string $reference_field_name
	 * @return $this
	 */
	public function set_dynamic_options_reference( $reference_field_name ) {
		$this->dynamic_options_reference_field_name = $reference_field_name;
		return $this;
	}


	/**
	 * @return bool
	 */
	public function has_dynamic_options() {
		return isset( $this->dynamic_options_reference_field_name );
	}


	/**
	 * @param array|string|bool $value
	 * @return void
	 */
	public function render( $value = false ) {

		$value = $this->sanitize_value( $value );

		if ( $this->has_dynamic_options() ) {
			$this->add_data_attr( 'automatewoo-dynamic-select' );
			$this->add_data_attr( 'automatewoo-dynamic-select-reference', $this->dynamic_options_reference_field_name );
		}

		if ( $this->multiple ) {
			if ( ! $value ) {
				$value = $this->default_option ? $this->default_option : [];
			}

			$this->render_multiple( (array) $value );
		} else {
			if ( empty( $value ) && $this->default_option ) {
				$value = $this->default_option;
			}

			$this->render_single( (string) $value );
		}
	}


	/**
	 * Render a single select box.
	 *
	 * @param string $value
	 */
	protected function render_single( $value ) {
		?>

		<select name="<?php echo esc_attr( $this->get_full_name() ); ?>"
				data-name="<?php echo esc_attr( $this->get_name() ); ?>"
				class="<?php echo esc_attr( $this->get_classes() ); ?>"
				<?php $this->output_extra_attrs(); ?>
				<?php echo ( $this->get_required() ? 'required' : '' ); ?>
		>

			<?php if ( $this->get_placeholder() ) : ?>
				<option value=""><?php echo esc_html( $this->get_placeholder() ); ?></option>
			<?php endif; ?>

			<?php foreach ( $this->get_options() as $opt_name => $opt_value ) : ?>
				<?php if ( is_array( $opt_value ) ) : ?>
					<optgroup label="<?php echo esc_attr( $opt_name ); ?>">
						<?php foreach ( $opt_value as $opt_sub_name => $opt_sub_value ) : ?>
							<option value="<?php echo esc_attr( $opt_sub_name ); ?>" <?php selected( $value, $opt_sub_name ); ?>><?php echo esc_html( $opt_sub_value ); ?></option>
						<?php endforeach ?>
					</optgroup>
				<?php else : ?>
					<option value="<?php echo esc_attr( $opt_name ); ?>" <?php selected( $value, $opt_name ); ?>><?php echo esc_html( $opt_value ); ?></option>
				<?php endif; ?>
			<?php endforeach; ?>

		</select>

		<?php
	}


	/**
	 * Render a multi-select box.
	 *
	 * @param array $values
	 */
	protected function render_multiple( $values ) {
		?>
		<select name="<?php echo esc_attr( $this->get_full_name() ); ?>[]"
				data-name="<?php echo esc_attr( $this->get_name() ); ?>"
				class="<?php echo esc_attr( $this->get_classes() ); ?> wc-enhanced-select"
				multiple="multiple"
				data-placeholder="<?php echo esc_attr( $this->get_placeholder() ); ?>"
			<?php $this->output_extra_attrs(); ?>
		>

			<?php foreach ( $this->get_options() as $opt_name => $opt_value ) : ?>
				<option value="<?php echo esc_attr( $opt_name ); ?>"
					<?php echo in_array( $opt_name, $values ) ? 'selected="selected"' : ''; // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict -- Option keys may be int while saved values are strings; loose match is intentional. ?>
					><?php echo esc_html( $opt_value ); ?></option>
			<?php endforeach; ?>

		</select>

		<script type="text/javascript">
			jQuery( 'body' ).trigger( 'wc-enhanced-select-init' );
		</script>

		<?php
	}


	/**
	 * Sanitizes the value of the field.
	 *
	 * @since 4.4.0
	 *
	 * @param array|string $value
	 *
	 * @return array|string
	 */
	public function sanitize_value( $value ) {
		if ( $this->multiple ) {
			return Clean::recursive( $value );
		} else {
			return Clean::string( $value );
		}
	}
}
