<?php
namespace Qazana;

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

/**
 * Box shadow control.
 *
 * A base control for creating box shadows control. Displays input fields for
 * horizontal shadow, vertical shadow, shadow blur, shadow spread, shadow color.
 *
 * Creating new control in the editor (inside `Widget_Base::_register_controls()`
 * method):
 *
 *    $this->add_control(
 *    	'box_shadow',
 *    	[
 *    		'label' => __( 'Box Shadow', 'plugin-domain' ),
 *    		'type' => Controls_Manager::BOX_SHADOW,
 *    		'default' => [
 *    			'color' => 'rgba(0,0,0,.5)',
 *    		],
 *    		'selectors' => [
 *    			'{{WRAPPER}} .your-class' => 'box-shadow: {{HORIZONTAL}}px {{VERTICAL}}px {{BLUR}}px {{SPREAD}}px {{COLOR}};',
 *    		],
 *    	]
 *    );
 *
 * PHP usage (inside `Widget_Base::render()` method):
 *
 *    echo '<div class="your-class"> ... </div>';
 *
 * JS usage (inside `Widget_Base::_content_template()` method):
 *
 *    <div class="your-class"> ... </div>
 *
 * @since 1.0.0
 *
 * @param string $label       Optional. The label that appears above of the
 *                            field. Default is empty.
 * @param string $description Optional. The description that appears below the
 *                            field. Default is empty.
 * @param string $placeholder Optional. The field placeholder that appears when
 *                            the field has no values. Default is empty.
 * @param array  $default     {
 *     Optional. Defautl box shadow values.
 *
 *     @type int    $horizontal Optional. Horizontal shadow. Default is 0.
 *     @type int    $vertical   Optional. Vertical shadow. Default is 0.
 *     @type int    $blur       Optional. Shadow blur. Default is 10.
 *     @type int    $spread     Optional. Shadow spread. Default is 0.
 *     @type string $color      Optional. Shadow color. Available values are
 *                              `rgb`, `rgba`, `hex` or `format`. Default is
 *                              `rgba(0,0,0,0.5)`.
 * }
 * @param string $separator   Optional. Set the position of the control separator.
 *                            Available values are 'default', 'before', 'after'
 *                            and 'none'. 'default' will position the separator
 *                            depending on the control type. 'before' / 'after'
 *                            will position the separator before/after the
 *                            control. 'none' will hide the separator. Default
 *                            is 'default'.
 * @param bool   $show_label  Optional. Whether to display the label. Default is
 *                            true.
 * @param bool   $label_block Optional. Whether to display the label in a
 *                            separate line. Default is false.
 *
 * @return array {
 *     Box shadow values.
 *
 *     @type int    $horizontal Horizontal shadow.
 *     @type int    $vertical   Vertical shadow.
 *     @type int    $blur       Shadow blur.
 *     @type int    $spread     Shadow spread.
 *     @type string $color      Shadow color.
 * }
 */
class Control_Box_Shadow extends Control_Base_Multiple {

	/**
	 * Retrieve box shadow control type.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Control type.
	 */
	public function get_type() {
		return 'box_shadow';
	}

	/**
	 * Retrieve box shadow control default value.
	 *
	 * Get the default value of the box shadow control. Used to return the
	 * default values while initializing the box shadow control.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return array Control default value.
	 */
	public function get_default_value() {
		return [
			'horizontal' => 0,
			'vertical' => 0,
			'blur' => 10,
			'spread' => 0,
			'color' => 'rgba(0,0,0,0.5)',
		];
	}

	/**
	 * Retrieve box shadow control sliders.
	 *
	 * Get the sliders of the box shadow control. Sliders are used while
	 * rendering the control output in the editor.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return array Control sliders.
	 */
	public function get_sliders() {
		return [
			'blur' => [
				'label' => __( 'Blur', 'qazana' ),
				'min' => 0,
				'max' => 100,
			],
			'spread' => [
				'label' => __( 'Spread', 'qazana' ),
				'min' => -100,
				'max' => 100,
			],
			'horizontal' => [
				'label' => __( 'Horizontal', 'qazana' ),
				'min' => -100,
				'max' => 100,
			],
			'vertical' => [
				'label' => __( 'Vertical', 'qazana' ),
				'min' => -100,
				'max' => 100,
			],
		];
	}

	/**
	 * Render box shadow control output in the editor.
	 *
	 * Used to generate the control HTML in the editor using Underscore JS
	 * template. The variables for the class are available using `data` JS
	 * object.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function content_template() {
		?>
		<#
		var defaultColorValue = '';

		if ( data.default.color ) {
			if ( '#' !== data.default.color.substring( 0, 1 ) ) {
				defaultColorValue = '#' + data.default.color;
			} else {
				defaultColorValue = data.default.color;
			}

			defaultColorValue = ' data-default-color=' + defaultColorValue; // Quotes added automatically.
		}
		#>
		<div class="qazana-control-field">
			<label class="qazana-control-title"><?php _e( 'Color', 'qazana' ); ?></label>
			<div class="qazana-control-input-wrapper">
				<input data-setting="color" class="qazana-shadow-color-picker" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value', 'qazana' ); ?>" data-alpha="true"{{{ defaultColorValue }}} />
			</div>
		</div>
		<?php
		foreach ( $this->get_sliders() as $slider_name => $slider ) :
			$control_uid = $this->get_control_uid( $slider_name );
			?>
			<div class="qazana-shadow-slider">
				<label for="<?php echo $control_uid; ?>" class="qazana-control-title"><?php echo $slider['label']; ?></label>
				<div class="qazana-control-input-wrapper">
					<div class="qazana-slider" data-input="<?php echo $slider_name; ?>"></div>
					<div class="qazana-slider-input">
						<input id="<?php echo $control_uid; ?>" type="number" min="<?php echo $slider['min']; ?>" max="<?php echo $slider['max']; ?>" data-setting="<?php echo $slider_name; ?>"/>
					</div>
				</div>
			</div>
		<?php endforeach; ?>
		<?php
	}
}
