<?php
add_action(
	'elementor/element/before_section_start',
	function( $element, $section_id, $args ) {

		$type = get_class( $element );

		switch ( $type ) {
			case 'Elementor\Element_Column':
			case 'Elementor\Element_Section':
				$show_before_id = 'section_advanced';
				break;
			default:
				// Section '_section_style' is common to all widgets, registered in Widget_Common::register_controls().
				$show_before_id = '_section_style';
		}

		if ( $show_before_id === $section_id ) {
			ignition_elementor_tilt_controls( $element );
		}

	},
	10,
	3
);

function ignition_elementor_tilt_controls( $element ) {
	$element->start_controls_section(
		'ignition_tilt',
		[
			'tab'   => \Elementor\Controls_Manager::TAB_ADVANCED,
			'label' => __( 'Tilt Effect', 'ignition-elementor-widgets' ),
		]
	);

	$element->add_control(
		'ignition_enable_tilt',
		[
			'label'        => __( 'Enable Tilt', 'ignition-elementor-widgets' ),
			'type'         => \Elementor\Controls_Manager::SWITCHER,
			'default'      => '',
			'label_on'     => __( 'On', 'ignition-elementor-widgets' ),
			'label_off'    => __( 'Off', 'ignition-elementor-widgets' ),
			'return_value' => 'yes',
			'separator'    => 'none',
		]
	);

	$element->add_control(
		'ignition_reverse_tilt',
		[
			'label'        => __( 'Reverse Tilt Direction', 'ignition-elementor-widgets' ),
			'type'         => \Elementor\Controls_Manager::SWITCHER,
			'default'      => false,
			'label_on'     => __( 'On', 'ignition-elementor-widgets' ),
			'label_off'    => __( 'Off', 'ignition-elementor-widgets' ),
			'return_value' => true,
			'separator'    => 'none',
			'condition'    => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_tilt_max',
		[
			'label'      => __( 'Max tilt rotation', 'ignition-elementor-widgets' ),
			'type'       => \Elementor\Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min'  => 0,
					'max'  => 90,
					'step' => 1,
				],
			],
			'default'    => [
				'size' => 35,
			],
			'condition'  => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_start_x',
		[
			'label'      => __( 'Starting tilt on the X axis', 'ignition-elementor-widgets' ),
			'type'       => \Elementor\Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min'  => 0,
					'max'  => 90,
					'step' => 1,
				],
			],
			'default'    => [
				'size' => 0,
			],
			'condition'  => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_start_y',
		[
			'label'      => __( 'Starting tilt on the Y axis', 'ignition-elementor-widgets' ),
			'type'       => \Elementor\Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min'  => 0,
					'max'  => 90,
					'step' => 1,
				],
			],
			'default'    => [
				'size' => 0,
			],
			'condition'  => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_perspective',
		[
			'label'      => __( 'Transform perspective, the lower the more extreme the tilt gets.', 'ignition-elementor-widgets' ),
			'type'       => \Elementor\Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min'  => 500,
					'max'  => 2000,
					'step' => 50,
				],
			],
			'default'    => [
				'size' => 1000,
			],
			'condition'  => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_scale',
		[
			'label'      => __( 'Scale on hover.', 'ignition-elementor-widgets' ),
			'type'       => \Elementor\Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min'  => 50,
					'max'  => 200,
					'step' => 10,
				],
			],
			'default'    => [
				'size' => 100,
			],
			'condition'  => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_elementor_tilt_speed',
		[
			'label'      => __( 'Speed of the enter/exit transition.', 'ignition-elementor-widgets' ),
			'type'       => \Elementor\Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min'  => 100,
					'max'  => 1000,
					'step' => 50,
				],
			],
			'default'    => [
				'size' => 300,
			],
			'condition'  => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_axis',
		[
			'label'     => __( 'Choose an axis to disable', 'ignition-elementor-widgets' ),
			'type'      => \Elementor\Controls_Manager::SELECT,
			'default'   => '',
			'options'   => [
				''  => __( 'None', 'ignition-elementor-widgets' ),
				'y' => __( 'Disable X axis', 'ignition-elementor-widgets' ),
				'x' => __( 'Disable Y axis', 'ignition-elementor-widgets' ),
			],
			'condition' => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->add_control(
		'ignition_gyroscope',
		[
			'label'        => __( 'Enable/Disable device orientation detection', 'ignition-elementor-widgets' ),
			'type'         => \Elementor\Controls_Manager::SWITCHER,
			'default'      => true,
			'label_on'     => __( 'On', 'ignition-elementor-widgets' ),
			'label_off'    => __( 'Off', 'ignition-elementor-widgets' ),
			'return_value' => true,
			'separator'    => 'none',
			'condition'    => [
				'ignition_enable_tilt' => 'yes',
			],
		]
	);

	$element->end_controls_section();
}

add_action( 'elementor/frontend/widget/before_render', 'ignition_elementor_tilt_before_render' );
add_action( 'elementor/frontend/section/before_render', 'ignition_elementor_tilt_before_render' );
add_action( 'elementor/frontend/column/before_render', 'ignition_elementor_tilt_before_render' );

function ignition_elementor_tilt_before_render( \Elementor\Element_Base $element ) {
	$settings = $element->get_settings();

	if ( ! isset( $settings['ignition_enable_tilt'] ) || 'yes' !== $settings['ignition_enable_tilt'] ) {
		return;
	}

	$scale = floatval( $settings['ignition_scale']['size'] / 100 );
	$element->add_render_attribute( '_wrapper', 'data-tilt' );
	$element->add_render_attribute( '_wrapper', 'data-tilt-reverse', $settings['ignition_reverse_tilt'] );
	$element->add_render_attribute( '_wrapper', 'data-tilt-max', $settings['ignition_tilt_max']['size'] );
	$element->add_render_attribute( '_wrapper', 'data-tilt-startX', $settings['ignition_start_x']['size'] );
	$element->add_render_attribute( '_wrapper', 'data-tilt-startY', $settings['ignition_start_y']['size'] );
	$element->add_render_attribute( '_wrapper', 'data-tilt-perspective', $settings['ignition_perspective']['size'] );
	$element->add_render_attribute( '_wrapper', 'data-tilt-scale', $scale );
	$element->add_render_attribute( '_wrapper', 'data-tilt-speed', $settings['ignition_elementor_tilt_speed']['size'] );
	$element->add_render_attribute( '_wrapper', 'data-tilt-axis', $settings['ignition_axis'] );
	$element->add_render_attribute( '_wrapper', 'data-tilt-gyroscope', $settings['ignition_gyroscope'] );
}
