<?php

	/**
	 * Color and Opacity
	 *
	 * Main options:
	 *  name            => a name of the control
	 *  value           => a value to show in the control
	 *  default         => a default value of the control if the "value" option is not specified
	 *
	 * @package core
	 * @since 1.0.0
	 */

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

if ( ! class_exists( 'Wbcr_FactoryForms480_ColorAndOpacityControl' ) ) {
}

class Wbcr_FactoryForms480_ColorAndOpacityControl extends Wbcr_FactoryForms480_ComplexControl {

	public $type = 'color-and-opacity';

	public function __construct( $options, $form, $provider = null ) {
		parent::__construct( $options, $form, $provider );

		if ( ! isset( $options['color'] ) ) {
			$options['color'] = [];
		}

		$options['color'] = array_merge(
			$options['color'],
			[
				'name'         => $this->options['name'] . '__color',
				'default'      => isset( $this->options['default'] )
					? $this->options['default']['color']
					: '#1e8cbe',
				'pickerTarget' => '.factory-control-' . $this->options['name'] . ' .factory-picker-target',
			]
		);

		if ( ! isset( $options['opacity'] ) ) {
			$options['opacity'] = [];
		}

		$options['opacity'] = array_merge(
			$options['opacity'],
			[
				'name'    => $this->options['name'] . '__opacity',
				'default' => isset( $this->options['default'] )
					? $this->options['default']['opacity']
					: 100,
				'units'   => '%',
				'range'   => [ 0, 100 ],
				'way'     => 'slider',
			]
		);

		$this->color   = new Wbcr_FactoryForms480_ColorControl( $options['color'], $form, $provider );
		$this->opacity = new Wbcr_FactoryForms480_IntegerControl( $options['opacity'], $form, $provider );

		$this->innerControls = [ $this->color, $this->opacity ];
	}

	/**
	 * Shows the html markup of the control.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function html() {
		?>
			<div <?php $this->attrs(); ?>>
				<div class="factory-control-row">
					<div class="factory-color-wrap">
					<?php $this->color->html(); ?>
					</div>
					<div class="factory-opacity-wrap">
					<?php $this->opacity->html(); ?>
					</div>
				</div>
				<div class="factory-picker-target"></div>
			</div>
		<?php
	}
}
