<?php

class EL_VideoGalleryItem extends ET_Builder_Module {

	public $slug       = 'el_video_gallery_item';
	public $type       = 'child';
	public $vb_support = 'on';

	protected $module_credits = array(
		'module_uri' => 'https://diviextended.com/product/all-in-one-carousel-for-divi',
		'author'     => 'Elicus',
		'author_uri' => 'http://elicus.com',
	);

	public function init() {
		$this->name 						= esc_html__( 'Video Gallery Item', 'divi-gallery-extended' );
		$this->advanced_setting_title_text  = esc_html__( 'Video', 'divi-gallery-extended' );
		$this->child_title_var              = 'title';
		$this->main_css_element 			= '.el_video_gallery %%order_class%%';
	}

	public function get_settings_modal_toggles() {
		return array(
			'general'  => array(
				'toggles' => array(
					'main_content' => array(
						'title' => esc_html__( 'Content', 'divi-gallery-extended' ),
					),
				)
			),
			'advanced' => array(
				'toggles' => array(
					'overlay_text' => array(
						'title' => esc_html__( 'Title', 'divi-gallery-extended' ),
					),
				)
			)
		);
	}

	public function get_advanced_fields_config() {
		return array(
			'fonts' => array(
				'overlay_title_text' => array(
					'label'               => esc_html__( 'Overlay Title', 'divi-gallery-extended' ),
					'hide_font_size'	  => true,
					'hide_line_height'	  => true,
					'hide_letter_spacing' => true,
					'hide_text_color'     => true,
					'hide_text_align'     => true,
					'hide_text_shadow'    => true,
					'hide_font'           => true,
					'hide_weight'         => true,
					'hide_style'          => true,
					'header_level'   => array(
						'default' => 'h4',
						'default_of_front' => 'h4',
					),
					'css'               => array(
						'main'      => "%%order_class%% .el-video-gallery-item-overlay .el-overlay-title",
					),
					'toggle_slug'       => 'overlay_text',
					'sub_toggle'        => 'title_text',
					'tab_slug'          => 'advanced',
					'depends_on'        => array( 'enable_overlay' ),
					'depends_show_if'   => 'on',
				),
			),
			'text'            => false,
			'link_options'    => false,
			'position_fields' => false,
		);
	}

	public function get_fields() {
		return array(
			'title' => array(
				'label'           => esc_html__( 'Title', 'divi-gallery-extended' ),
				'type'            => 'text',
				'option_category' => 'basic_option',
				'tab_slug'        => 'general',
				'toggle_slug'     => 'main_content',
				'description'     => esc_html__( 'Here you can input the text to be used for the title.', 'divi-gallery-extended' ),
			),
			'caption' => array(
				'label'           => esc_html__( 'Caption', 'divi-gallery-extended' ),
				'type'         	  => 'text',
				'option_category' => 'basic_option',
				'tab_slug'        => 'general',
				'toggle_slug'     => 'main_content',
				'description'     => esc_html__( 'Here you can input the text to be used for the caption.', 'divi-gallery-extended' ),
			),
			'src' => array(
				'label'              => esc_html__( 'Video MP4 File Or YouTube/Vimeo URL', 'divi-gallery-extended' ),
				'type'               => 'upload',
				'option_category'    => 'basic_option',
				'data_type'          => 'video',
				'upload_button_text' => esc_attr__( 'Upload a video', 'divi-gallery-extended' ),
				'choose_text'        => esc_attr__( 'Choose a Video MP4 File', 'divi-gallery-extended' ),
				'update_text'        => esc_attr__( 'Set As Video', 'divi-gallery-extended' ),
				'description'        => esc_html__( 'Upload your desired video in .MP4 format, or type in the URL to the video you would like to display', 'divi-gallery-extended' ),
				'tab_slug'			 => 'general',
				'toggle_slug'        => 'main_content',
			),
			'src_webm' => array(
				'label'              => esc_html__( 'Video WEBM File', 'divi-gallery-extended' ),
				'type'               => 'upload',
				'option_category'    => 'basic_option',
				'data_type'          => 'video',
				'upload_button_text' => esc_attr__( 'Upload a video', 'divi-gallery-extended' ),
				'choose_text'        => esc_attr__( 'Choose a Video WEBM File', 'divi-gallery-extended' ),
				'update_text'        => esc_attr__( 'Set As Video', 'divi-gallery-extended' ),
				'description'        => esc_html__( 'Upload the .WEBM version of your video here. All uploaded videos should be in both .MP4 .WEBM formats to ensure maximum compatibility in all browsers.', 'divi-gallery-extended' ),
				'tab_slug'			 => 'general',
				'toggle_slug'        => 'main_content',
			),
			'image_src' => array(
				'label'              => esc_html__( 'Preview Image', 'divi-gallery-extended' ),
				'type'               => 'upload',
				'option_category'    => 'basic_option',
				'upload_button_text' => et_builder_i18n( 'Upload an image' ),
				'choose_text'        => esc_attr__( 'Choose an Image', 'divi-gallery-extended' ),
				'update_text'        => esc_attr__( 'Set As Image', 'divi-gallery-extended' ),
				'additional_button'  => sprintf(
					'<input type="button" class="button et-pb-video-image-button" value="%1$s" />',
					esc_attr__( 'Generate Image From Video', 'divi-gallery-extended' )
				),
				'additional_button_type'  => 'generate_image_url_from_video',
				'additional_button_attrs' => array(
					'video_source' => 'src',
				),
				'classes'             => 'et_pb_video_preview',
				'description'         => esc_html__( 'Upload your desired image, or type in the URL to the image you would like to display as a preview of your video. You can also generate a still image from your video.', 'divi-gallery-extended' ),
				'tab_slug'			  => 'general',
				'toggle_slug'         => 'main_content',
				'dynamic_content'     => 'image',
			),
			/*'category' => array(
				'label'        	  => esc_html__( 'Category', 'divi-gallery-extended' ),
				'type'            => 'text',
				'option_category' => 'basic_option',
				'tab_slug'        => 'general',
				'toggle_slug'     => 'main_content',
				'description'     => esc_html__( 'Here you can input the category to be used for the filter.', 'divi-gallery-extended' ),
			),*/
		);
	}

	public function render( $attrs, $content, $render_slug ) {

		global $dge_video_gallery_props;

		$main_defaults = array(
			'show_title'     => 'off',
			'title_area'	 => 'lightbox',
			'show_caption'   => 'off',
			'caption_area'	 => 'lightbox',
			'click_trigger'  => 'lightbox',
			'enable_overlay' => 'on'
		);

		$main_props = wp_parse_args( $dge_video_gallery_props, $main_defaults );

		// Check if no video uploaded.
		if ( empty( $this->props['src'] ) && empty( $this->props['src_webm'] ) ) {
			return '';
		}

		// Normalize youtube URL.
		if ( false !== et_pb_validate_youtube_url( esc_url( $this->props['src'] ) ) ) {
			$this->props['src'] = et_pb_normalize_youtube_url( esc_url( $this->props['src'] ) );
		}

		// Get provider data.
		$oembed			= _wp_oembed_get_object();
		$provider		= $oembed->get_data( $this->props['src'] );
		$provider_name	= isset( $provider->provider_name ) ? $provider->provider_name : 'custom-upload';

		// Get video URL.
		$video_url = ! empty( $this->props['src'] ) ? $this->props['src'] : $this->props['src_webm'];

		// Image src.
		$video_size = ! empty( $main_props['video_size'] ) ? $main_props['video_size'] : 'ratio-16-9';
		$image_src  = ! empty( $this->props['image_src'] ) ? $this->props['image_src'] : '';

		$video_image = '';
		if ( 'auto' === $video_size && ! empty( $image_src ) ) {
			$video_image = sprintf( '<div class="el-video-gallery-image"><img src="%1$s" alt="" /></div>', esc_url( $image_src ) );
		}

		// Title.
		$title = '';
		if ( 'on' === $main_props['show_title'] && ! empty( $this->props['title'] ) ) {
			$title_level = ! empty( $this->props['overlay_title_text_level'] ) ? $this->props['overlay_title_text_level'] : 'h4';
			$title = sprintf( 
				'<%1$s class="el-overlay-title">%2$s</%1$s>',
				esc_html( $title_level ),
				esc_html( $this->props['title'] )
			);
		}

		// Title.
		$caption = '';
		if ( 'on' === $main_props['show_caption'] && ! empty( $this->props['caption'] ) ) {
			$caption = sprintf( '<div class="el-overlay-caption">%1$s</div>', esc_html( $this->props['caption'] ) );
		}

		// Overlay.
		$overlay = '';
		if ( 'on' === $main_props['enable_overlay'] || 'lightbox' === $main_props['click_trigger'] ) {
			$overlay_content  = '';
			if ( 'overlay' === $main_props['title_area'] || 'overlay' == $main_props['caption_area'] ) {
				$overlay_content = sprintf(
					'<div class="el-video-overlay-content">%1$s %2$s</div>',
					( 'overlay' === $main_props['title_area'] ) ? et_core_intentionally_unescaped( $title, 'html' ) : '',
					( 'overlay' === $main_props['caption_area'] ) ? et_core_intentionally_unescaped( $caption, 'html' ) : ''
				);
			}
			
			$overlay = sprintf(
				'<div class="el-video-gallery-item-overlay">
					<span class="et-pb-icon"></span>
					%1$s
				</div>',
				et_core_intentionally_unescaped( $overlay_content, 'html' )
			);
		}

		$check_content = array( '' );
		$content       = '';
		if ( ( ! empty( $title ) && 'overlay' !== $main_props['title_area'] ) ||
			( ! empty( $caption ) && 'overlay' !== $main_props['caption_area'] ) ) {
			$content = sprintf(
				'<div class="el-video-gallery-content">%1$s %2$s</div>',
				( 'overlay' !== $main_props['title_area'] ) ? et_core_intentionally_unescaped( $title, 'html' ) : '',
				( 'overlay' !== $main_props['caption_area'] ) ? et_core_intentionally_unescaped( $caption, 'html' ) : ''
			);
		}

		$output = sprintf(
			'<div class="el-video-gallery-item">
				<a href="%1$s" data-provider="%2$s">
					<div class="el-video-gallery-item-preview">%5$s %3$s</div>
				</a>
				%4$s
			</div>',
			str_replace( '/shorts/', '/watch/?v=', esc_url( $video_url ) ),
			esc_attr( strtolower( $provider_name ) ),
			et_core_intentionally_unescaped( $overlay, 'html' ),
			et_core_intentionally_unescaped( $content, 'html' ),
			et_core_intentionally_unescaped( $video_image, 'html' )
		);

		if ( 'auto' !== $video_size && ! empty( $image_src ) ) {
			self::set_style( $render_slug, array(
				'selector'    => '%%order_class%% .el-video-gallery-item-preview',
				'declaration' => sprintf( 'background-image: url(%1$s);', esc_url( $image_src ) )
			) );
		}

		return et_core_intentionally_unescaped( $output, 'html' );
	}

	protected function _render_module_wrapper( $output = '', $render_slug = '' ) {
		$wrapper_settings    = $this->get_wrapper_settings( $render_slug );
		$slug                = $render_slug;
		$outer_wrapper_attrs = $wrapper_settings['attrs'];      /**
		* Filters the HTML attributes for the module's outer wrapper. The dynamic portion of the
		* filter name, '$slug', corresponds to the module's slug.
		*
		* @since 3.23 Add support for responsive video background.
		* @since 3.1
		*
		* @param string[]           $outer_wrapper_attrs
		* @param ET_Builder_Element $module_instance
		*/
		$outer_wrapper_attrs = apply_filters( "et_builder_module_{$slug}_outer_wrapper_attrs", $outer_wrapper_attrs, $this );
		return sprintf(
			'<div%1$s>
				%2$s
			</div>',
			et_html_attrs( $outer_wrapper_attrs ),
			$output
		);
	}
}

new EL_VideoGalleryItem;
