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

use \Elementor\Controls_Manager;

/**
 * Sina_Ext_Pro_Water_Ripples Class
 *
 * @since 1.5.0
 */
Class Sina_Ext_Pro_Water_Ripples{
	/**
	 * Instance
	 *
	 * @since 1.5.0
	 * @var Sina_Ext_Pro_Water_Ripples The single instance of the class.
	 */
	private static $_instance = null;

	/**
	 * Instance
	 *
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @since 1.5.0
	 * @return Sina_Ext_Pro_Water_Ripples An Instance of the class.
	 */
	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	public function __construct() {
		add_action( 'elementor/element/section/section_advanced/after_section_end', [$this, 'register_controls'] );
		add_action( 'elementor/element/container/section_layout/after_section_end', [$this, 'register_controls'] );
		add_action( 'elementor/frontend/section/before_render', [$this, 'before_render'] );
		add_action( 'elementor/frontend/container/before_render', [$this, 'before_render'] );
		add_action( 'elementor/frontend/section/after_render', [$this, 'after_render'] );
		add_action( 'elementor/frontend/container/after_render', [$this, 'after_render'] );

		add_action( 'elementor/section/print_template', [$this, 'print_template'], 10, 2 );
		add_action( 'elementor/container/print_template', [$this, 'print_template'], 10, 2 );
	}

	public function register_controls($elems) {
		$elems->start_controls_section(
			'sina_water_ripples',
			[
				'label' => esc_html__('Sina Water Ripples', 'sina-ext-pro'),
				'tab' => Controls_Manager::TAB_ADVANCED,
			]
		);

		$elems->add_control(
			'sina_water_ripples_note',
			[
				'type' => Controls_Manager::RAW_HTML,
				'raw' => esc_html__( 'NOTICE: When you turn enabled or disabled this feature don\'t forget to click the "yes" button to see the actual result!', 'sina-ext-pro' ).'<strong><br>'.esc_html__( 'Also you have to set the background first.', 'sina-ext-pro' ).'</strong>',
				'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning',
			]
		);
		$elems->add_control(
			'sina_is_water_ripples',
			[
				'label' => esc_html__( 'Ripples Enable', 'sina-ext-pro' ),
				'type' => Controls_Manager::SWITCHER,
				'prefix_class'  => 'sina-water-ripples-',
			]
		);
		$elems->add_control(
			'sina_water_ripples_dropRadius',
			[
				'label' => esc_html__( 'Drop Radius', 'sina-ext-pro' ),
				'type' => Controls_Manager::NUMBER,
				'size_units' => [ 'px' ],
				'min' => 1,
				'max' => 100,
				'step' => 1,
				'default' => 20,
				'condition' => [
					'sina_is_water_ripples!' => '',
				],
			]
		);
		$elems->add_control(
			'sina_water_ripples_perturbance',
			[
				'label' => esc_html__( 'Perturbance', 'sina-ext-pro' ),
				'type' => Controls_Manager::NUMBER,
				'size_units' => [ 'px' ],
				'min' => 0.01,
				'max' => 1,
				'step' => 0.01,
				'default' => 0.03,
				'condition' => [
					'sina_is_water_ripples!' => '',
				],
			]
		);
		$elems->add_control(
			'sina_water_ripples_resolution',
			[
				'label' => esc_html__( 'Resolution', 'sina-ext-pro' ),
				'type' => Controls_Manager::NUMBER,
				'size_units' => [ 'px' ],
				'min' => 1,
				'max' => 1024,
				'step' => 1,
				'default' => 256,
				'condition' => [
					'sina_is_water_ripples!' => '',
				],
			]
		);

		$elems->add_control(
			'sina_water_ripples_apply',
			[
				'type' => Controls_Manager::RAW_HTML,
				'raw' => '<div class="elementor-update-preview" style="background: rgba(0,0,0,0);margin-left: 0;margin-right: 0;"><div class="elementor-update-preview-title">Changes Apply?</div><div class="elementor-update-preview-button-wrapper"><button class="elementor-update-preview-button elementor-button elementor-button-success">Yes</button></div></div>',
			]
		);

		$elems->end_controls_section();
	}

	public function after_render($elems) {
		$data = $elems->get_settings_for_display();
		if ( 'yes' == $data['sina_is_water_ripples'] ) {
			wp_enqueue_script( 'water-ripples', SINA_EXT_PRO_URL .'assets/js/water-ripples.min.js', ['jquery'], SINA_EXT_PRO_VERSION, true );
		}
	}

	public function before_render($elems) {
		$data = $elems->get_settings();
		if ( 'yes' == $data['sina_is_water_ripples'] ) {
			$ripples_data = [
				'dropRadius' => $data['sina_water_ripples_dropRadius'],
				'perturbance' => $data['sina_water_ripples_perturbance'],
				'resolution' => $data['sina_water_ripples_resolution']
			];
			$elems->add_render_attribute( '_wrapper', 'data-sina-water-ripples', json_encode( $ripples_data ) );
		}
	}

	public function print_template( $template, $elems ) {
		$old_template = $template;

		ob_start();
		?>
		<#
		var ripplesEffect = {
			dropRadius: settings.sina_water_ripples_dropRadius,
			perturbance: settings.sina_water_ripples_perturbance,
			resolution: settings.sina_water_ripples_resolution
		}
		if ('yes' == settings.sina_is_water_ripples) {
			view.el.setAttribute('data-sina-water-ripples', JSON.stringify(ripplesEffect));
		} #>
		<?php
		$content = ob_get_contents();
		ob_end_clean();

		return $content . $old_template;
	}
}

Sina_Ext_Pro_Water_Ripples::instance();