<?php
/**
 * BricksExtras Interactions
 *
 * @package BricksExtras
 */

namespace BricksExtras;

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

if ( class_exists( 'BricksExtrasInteractions' ) ) {
	return;
}

/**
 * Class for managing Bricks interactions and custom triggers.
 */
class BricksExtrasInteractions {

	/**
	 * Prefix for option names.
	 *
	 * @var string
	 */
	public static $prefix = '';

	/**
	 * Initialize interactions.
	 *
	 * @param string $prefix Option prefix.
	 * @return void
	 */
	public static function init( $prefix ) {

		self::$prefix                          = $prefix;
		$controls_data                         = \Bricks\Interactions::get_controls_data();
		\Bricks\Interactions::$control_options = $controls_data;

		/* triggers */

		$triggers = array(
			'copy_to_clipboard'  => array(
				'label'    => 'Copy to Clipboard',
				'triggers' => array(
					'x_copy:copied' => esc_html__( 'Copy success', 'bricksextras' ),
					'x_copy:failed' => esc_html__( 'Copy failed', 'bricksextras' ),
					'x_copy:reset'  => esc_html__( 'Button reset', 'bricksextras' ),
					'x_copy:empty'  => esc_html__( 'Copy returned empty', 'bricksextras' ),
				),
			),
			'dynamic_lightbox'   => array(
				'label'    => 'Dynamic Lightbox',
				'triggers' => array(
					'x_lightbox:open'  => esc_html__( 'Lightbox opened', 'bricksextras' ),
					'x_lightbox:close' => esc_html__( 'Lightbox closed', 'bricksextras' ),
				),
			),
			'favorite'           => array(
				'label'    => 'Favorites',
				'triggers' => array(
					'x_favorite:added'                 => esc_html__( 'Item added', 'bricksextras' ),
					'x_favorite:removed'               => esc_html__( 'Item removed', 'bricksextras' ),
					'x_favorite:cleared'               => esc_html__( 'All items cleared', 'bricksextras' ),
					'x_favorite:count-reached_{count}' => esc_html__( 'List count reaches X', 'bricksextras' ),
					'x_favorite:maximum-reached'       => esc_html__( 'List count full (over maximum)', 'bricksextras' ),
				),
			),
			'fluent_form'        => array(
				'label'    => 'Fluent Form',
				'triggers' => array(
					'fluentform_submission_success' => esc_html__( 'Submission success', 'bricksextras' ),
					'fluentform_submission_failed'  => esc_html__( 'Submission failed', 'bricksextras' ),
					'fluentform_reset'              => esc_html__( 'Form reset', 'bricksextras' ),
				),
			),
			'header_row'         => array(
				'label'    => 'Header Extras',
				'triggers' => array(
					'x_sticky_header:active'   => esc_html__( 'Sticky header', 'bricks' ) . ' (' . esc_html__( 'Active', 'bricks' ) . ')',
					'x_sticky_header:inactive' => esc_html__( 'Sticky header', 'bricks' ) . ' (' . esc_html__( 'Inactive', 'bricks' ) . ')',
					'x_hide_header:active'     => esc_html__( 'Header hidden', 'bricksextras' ) . ' (' . esc_html__( 'Active', 'bricks' ) . ')',
					'x_hide_header:inactive'   => esc_html__( 'Header hidden', 'bricksextras' ) . ' (' . esc_html__( 'Inactive', 'bricks' ) . ')',
				),
			),
			'image_hotspots'     => array(
				'label'    => 'Image Hotspots',
				'triggers' => array(
					'x_image_hotspot:selected_{index}' => esc_html__( 'Image hotspot selected', 'bricksextras' ),
				),
			),
			'notification_bar'   => array(
				'label'    => 'Header Notification Bar',
				'triggers' => array(
					'x_notification:show'  => esc_html__( 'Notification Open', 'bricksextras' ),
					'x_notification:close' => esc_html__( 'Notification Dismissed', 'bricksextras' ),
				),
			),
			'lottie'             => array(
				'label'    => 'Lottie',
				'triggers' => array(
					'x_lottie:complete' => esc_html__( 'Animation end', 'bricks' ),
				),
			),
			'media_player'       => array(
				'label'    => 'Media Player',
				'triggers' => array(
					'xmediaplayer-started'         => esc_html__( 'Started', 'bricksextras' ),
					'xmediaplayer-ended'           => esc_html__( 'Ended', 'bricksextras' ),
					'xmediaplayer-pause'           => esc_html__( 'Paused', 'bricks' ),
					'xmediaplayer-resume'          => esc_html__( 'Resumed', 'bricksextras' ),
					'xmediaplayer-x_media_player:switched-media' => esc_html__( 'Switched media', 'bricksextras' ),
					'xmediaplayer-seeked'          => esc_html__( 'Seeked to new time', 'bricksextras' ),
					'xmediaplayer-replay'          => esc_html__( 'Replayed after ending', 'bricksextras' ),
					'xmediaplayer-time-update'     => esc_html__( 'Playback position from start', 'bricksextras' ),
					'xmediaplayer-time-update-end' => esc_html__( 'Playback position from end', 'bricksextras' ),
					'xmediaplayer-watch-time'      => esc_html__( 'Cumulative watch time', 'bricksextras' ),

				),
			),
			'media_playlist'     => array(
				'label'    => 'Media Playlist',
				'triggers' => array(
					'x_media_playlist:active'   => esc_html__( 'Active', 'bricks' ),
					'x_media_playlist:inactive' => esc_html__( 'Inactive', 'bricks' ),
					'x_media_playlist:playing'  => esc_html__( 'Playing', 'bricksextras' ),
					'x_media_playlist:paused'   => esc_html__( 'Paused', 'bricks' ),
					'x_media_playlist:ended'    => esc_html__( 'Ended', 'bricksextras' ),
				),
			),
			'pro_modal_nestable' => array(
				'label'    => 'Modal',
				'triggers' => array(
					'x_modal:open'  => esc_html__( 'Modal opened', 'bricksextras' ),
					'x_modal:close' => esc_html__( 'Modal closed', 'bricksextras' ),
				),
			),

			'pro_alert'          => array(
				'label'    => 'Pro Alert',
				'triggers' => array(
					'x_alert:show'  => esc_html__( 'Alert shown', 'bricksextras' ),
					'x_alert:close' => esc_html__( 'Alert closed', 'bricksextras' ),
				),
			),
			'countdown'          => array(
				'label'    => 'Pro Countdown',
				'triggers' => array(
					'x_countdown:ended' => esc_html__( 'Countdown End', 'bricksextras' ),
				),
			),

			'offcanvas_nestable' => array(
				'label'    => 'Pro Offcanvas',
				'triggers' => array(
					'x_offcanvas:open'  => esc_html__( 'Offcanvas opened', 'bricksextras' ),
					'x_offcanvas:close' => esc_html__( 'Offcanvas closed', 'bricksextras' ),
				),
			),
			'page_tour'          => array(
				'label'    => 'Page Tour',
				'triggers' => array(
					'x_page_tour:completed' => esc_html__( 'Completed', 'bricksextras' ),
					'x_page_tour:cancelled' => esc_html__( 'Cancelled', 'bricksextras' ),
					'x_page_tour:started'   => esc_html__( 'Started', 'bricksextras' ),
				),
			),
			'page_tour_step'     => array(
				'label'    => 'Page Tour Step',
				'triggers' => array(
					'x_page_tour_step:shown'  => esc_html__( 'Step shown', 'bricksextras' ),
					'x_page_tour_step:hidden' => esc_html__( 'Step hidden', 'bricksextras' ),
				),
			),
			'popover'            => array(
				'label'    => 'Popover',
				'triggers' => array(
					'x_popover:show' => esc_html__( 'Popover opened', 'bricksextras' ),
					'x_popover:hide' => esc_html__( 'Popover closed', 'bricksextras' ),
				),
			),
			'pro_accordion'      => array(
				'label'    => 'Pro Accordion',
				'triggers' => array(
					'x_accordion:expand'         => esc_html__( 'Accordion expand', 'bricksextras' ),
					'x_accordion:expand_{index}' => esc_html__( 'Specific item expand', 'bricksextras' ),
				),
			),
			'pro_slider'         => array(
				'label'    => 'Pro Slider',
				'triggers' => array(
					'x_slider:active-slide' => esc_html__( 'Slider moved to specific slide', 'bricksextras' ),
					'x_slider:moved'        => esc_html__( 'Slider moved', 'bricksextras' ),
					'x_slider:move'         => esc_html__( 'Slider move', 'bricksextras' ),
				),
			),

			'pro_tabs'           => array(
				'label'    => 'Pro Tabs',
				'triggers' => array(
					'x_tabs:accordion'                => esc_html__( 'Switched to accordion', 'bricksextras' ),
					'x_tabs:tabs'                     => esc_html__( 'Switched to tabs', 'bricksextras' ),
					'x_tabs_accordion:expand'         => esc_html__( 'Switch to new item', 'bricksextras' ),
					'x_tabs_accordion:collapse'       => esc_html__( 'Accordion collapse', 'bricksextras' ),
					'x_tabs_accordion:expand_{index}' => esc_html__( 'Switch to specific item', 'bricksextras' ),
				),
			),
			'read_more_less'     => array(
				'label'    => 'Read More / Less',
				'triggers' => array(
					'x_readmore:collapse'  => esc_html__( 'Collapsing', 'bricksextras' ),
					'x_readmore:expand'    => esc_html__( 'Expanding', 'bricksextras' ),
					'x_readmore:collapsed' => esc_html__( 'Collapsed', 'bricks' ),
					'x_readmore:expanded'  => esc_html__( 'Expanded', 'bricks' ),
				),
			),
			'slide_menu'         => array(
				'label'    => 'Slide Menu',
				'triggers' => array(
					'x_slide_menu:expand'   => esc_html__( 'Menu expanded', 'bricksextras' ),
					'x_slide_menu:collapse' => esc_html__( 'Menu collapse', 'bricksextras' ),
				),
			),
			'table_of_contents'  => array(
				'label'    => 'Table of Contents',
				'triggers' => array(
					'x_toc:link-clicked' => esc_html__( 'Heading link clicked', 'bricksextras' ),
				),
			),
			'toggle_switch'      => array(
				'label'    => 'Toggle Switch',
				'triggers' => array(
					'x_toggle_switch:change' => esc_html__( 'Switched', 'bricksextras' ),
					'x_toggle_switch:toggled_{label number}' => esc_html__( 'Switched to', 'bricksextras' ),
				),
			),

			'ws_forms'           => array(
				'label'    => 'WS Forms',
				'triggers' => array(
					'wsf-submit-success' => esc_html__( 'Submission success', 'bricksextras' ),
					'wsf-submit-error'   => esc_html__( 'Submission error', 'bricksextras' ),
					'wsf-reset-complete' => esc_html__( 'Form reset', 'bricksextras' ),
					'wsf-validate-fail'  => esc_html__( 'Validate fail', 'bricksextras' ),
				),
			),

		);

		foreach ( $triggers as $element => $trigger ) {
			self::add_element_interactions( $element, $trigger['label'], $trigger['triggers'] );
		}

		/* extras options */

		$bricks_extras_options = array();

		$bricks_extras_options['xMediaPlayerTimeFormat'] = array(
			'label'       => esc_html__( 'Time Measurement', 'bricksextras' ),
			'placeholder' => esc_html__( 'Minutes', 'bricks' ) . ' / ' . esc_html__( 'Seconds', 'bricks' ),
			'type'        => 'select',
			'inline'      => true,
			'options'     => array(
				'seconds'    => esc_html__( 'Minutes', 'bricks' ) . ' / ' . esc_html__( 'Seconds', 'bricks' ),
				'percentage' => esc_html__( 'Percentage', 'bricks' ),
			),
			'unit'        => false,
			'required'    => array(
				'trigger',
				'=',
				array(
					'xmediaplayer-time-update',
					'xmediaplayer-watch-time',
					'xmediaplayer-time-update-end',
					'xmediaplayer-watch-time-percentage',
				),
			),
		);

		$bricks_extras_options['xMediaPlayerTimePercentage'] = array(
			'label'    => esc_html__( 'Percentage', 'bricks' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				array(
					'trigger',
					'=',
					array(
						'xmediaplayer-time-update',
						'xmediaplayer-watch-time',
						'xmediaplayer-time-update-end',
						'xmediaplayer-watch-time-percentage',
					),
				),
				array( 'xMediaPlayerTimeFormat', '=', 'percentage' ),
			),
		);

		$bricks_extras_options['xMediaPlayerTimeMinutes'] = array(
			'label'    => esc_html__( 'Minutes', 'bricksextras' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				array(
					'trigger',
					'=',
					array(
						'xmediaplayer-time-update',
						'xmediaplayer-watch-time',
						'xmediaplayer-time-update-end',
						'xmediaplayer-watch-time-percentage',
					),
				),
				array( 'xMediaPlayerTimeFormat', '!=', 'percentage' ),
			),
		);

		$bricks_extras_options['xMediaPlayerTime'] = array(
			'label'    => esc_html__( 'Seconds', 'bricksextras' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				array(
					'trigger',
					'=',
					array(
						'xmediaplayer-time-update',
						'xmediaplayer-watch-time',
						'xmediaplayer-time-update-end',
						'xmediaplayer-watch-time-percentage',
					),
				),
				array( 'xMediaPlayerTimeFormat', '!=', 'percentage' ),
			),
		);

		$bricks_extras_options['xMediaPlayerFullscreenAction'] = array(
			'label'       => esc_html__( 'Allow action while player is fullscreen', 'bricksextras' ),
			'type'        => 'select',
			'placeholder' => esc_html__( 'True', 'bricks' ),
			'options'     => array(
				'true'  => esc_html__( 'True', 'bricks' ),
				'false' => esc_html__( 'False', 'bricks' ),
				'exit'  => esc_html__( 'Force exit fullscreen then allow', 'bricksextras' ),
				'wait'  => esc_html__( 'Wait for exit fullscreen then allow', 'bricksextras' ),
			),
			'unit'        => false,
			'required'    => array(
				array(
					'trigger',
					'=',
					array(
						'xmediaplayer-started',
						'xmediaplayer-ended',
						'xmediaplayer-pause',
						'xmediaplayer-resume',
						'xmediaplayer-seeked',
						'xmediaplayer-time-update',
						'xmediaplayer-watch-time',
						'xmediaplayer-time-update-end',
					),
				),
			),
		);

		$bricks_extras_options['xFavoriteCountReachedCount'] = array(
			'label'    => esc_html__( 'Count', 'bricks' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				'trigger',
				'=',
				array(
					'x_favorite:count-reached_{count}',
				),
			),
		);

		$bricks_extras_options['xInteractionsDelay'] = array(
			'label'       => esc_html__( 'Delay', 'bricks' ) . ' (ms)',
			'type'        => 'number',
			'unit'        => false,
			'placeholder' => 0,
			'required'    => array(
				'trigger',
				'=',
				array(
					'xmediaplayer-started',
					'xmediaplayer-ended',
					'xmediaplayer-pause',
					'xmediaplayer-time-update',
					'xmediaplayer-watch-time',
					'xmediaplayer-time-update-end',
					'xmediaplayer-x_media_player:switched-media',
					'x_modal:open',
					'x_modal:close',
					'x_alert:show',
					'x_alert:close',
					'x_lightbox:open',
					'x_lightbox:close',
					'x_offcanvas:open',
					'x_offcanvas:close',
					'fluentform_submission_success',
					'fluentform_submission_failed',
					'fluentform_reset',
					'x_favorite:maximum-reached',
					'x_favorite:cleared',
					'x_favorite:removed',
					'x_favorite:added',
					'x_favorite:count-reached_{count}',
					'wsf-submit-success',
					'wsf-submit-error',
					'wsf-reset-complete',
					'wsf-validate-fail',
					'x_sticky_header:active',
					'x_sticky_header:inactive',
					'x_hide_header:active',
					'x_hide_header:inactive',
					'x_toggle_switch:change',
					'x_toggle_switch:toggled_{label number}',
					'x_copy:copied',
					'x_copy:reset',
					'x_copy:failed',
					'x_copy:empty',
					'x_notification:show',
					'x_notification:close',
					'x_countdown:ended',
					'x_tabs:accordion',
					'x_tabs:tabs',
					'x_tabs:switch',
					'x_tabs_accordion:collapse',
					'x_tabs_accordion:expand',
					'x_tabs_accordion:expand_{index}',
					'x_toc:link-clicked',
					'x_slide_menu:expand',
					'x_slide_menu:collapse',
					'x_readmore:collapse',
					'x_readmore:expand',
					'x_readmore:collapsed',
					'x_readmore:expanded',
					// 'x_tabs:tab_'
					'x_slider:active-slide',
					'x_slider:move',
					'x_slider:moved',
					'x_accordion:expand_{index}',
					'x_accordion:expand',
					'x_image_hotspot:selected_{index}',
					'x_popover:show',
					'x_popover:hide',
				),
			),
		);

		$bricks_extras_options['xToggleSwitchLabelNumber'] = array(
			'label'    => esc_html__( 'Label Number', 'bricksextras' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				'trigger',
				'=',
				array(
					'x_toggle_switch:toggled_{label number}',
				),
			),
		);

		$bricks_extras_options['xSliderNumber'] = array(
			'label'    => esc_html__( 'Slide Index', 'bricksextras' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				'trigger',
				'=',
				array(
					'x_slider:active-slide',
				),
			),
		);

		$bricks_extras_options['xAccordionItemIndex'] = array(
			'label'    => esc_html__( 'Accordion Item Index', 'bricksextras' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				'trigger',
				'=',
				array(
					'x_accordion:expand_{index}',
				),
			),
		);

		$bricks_extras_options['xAccordionTabsItemIndex'] = array(
			'label'    => esc_html__( 'Item Index', 'bricksextras' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				'trigger',
				'=',
				array(
					'x_tabs_accordion:expand_{index}',
				),
			),
		);

		$bricks_extras_options['xImageHotspotsIndex'] = array(
			'label'    => esc_html__( 'Hotspot Index', 'bricksextras' ),
			'type'     => 'number',
			'unit'     => false,
			'required' => array(
				'trigger',
				'=',
				array(
					'x_image_hotspot:selected_{index}',
				),
			),
		);

		$bricks_fields = \Bricks\Interactions::$control_options['fields'];
		$pos           = array_search( 'action', array_keys( $bricks_fields ), true );

		\Bricks\Interactions::$control_options['fields'] = array_slice( $bricks_fields, 0, $pos, true ) + $bricks_extras_options + array_slice( $bricks_fields, $pos, null, true );
	}


	/**
	 * Add interactions for each element if activated.
	 *
	 * @param string $element Element identifier.
	 * @param string $label Element label.
	 * @param array  $triggers Array of trigger events.
	 * @return void
	 */
	private static function add_element_interactions( $element, $label, $triggers ) {

		if ( get_option( self::$prefix . $element ) ) {

			\Bricks\Interactions::$control_options['fields']['trigger']['options'][ $element . 'GroupTitle' ] = $label . ' (' . esc_html__( 'Extras', 'bricks' ) . ')';

			foreach ( $triggers as $key => $event ) {
				\Bricks\Interactions::$control_options['fields']['trigger']['options'][ $key ] = $event;
			}
		}
	}
}
