<?php

if ( ! function_exists( 'ginevra_core_add_video_holder_shortcode' ) ) {
	/**
	 * Function that add shortcode into shortcodes list for registration
	 *
	 * @param array $shortcodes
	 *
	 * @return array
	 */
	function ginevra_core_add_video_holder_shortcode( $shortcodes ) {
		$shortcodes[] = 'GinevraCore_Video_Holder_Shortcode';
		
		return $shortcodes;
	}
	
	add_filter( 'ginevra_core_filter_register_shortcodes', 'ginevra_core_add_video_holder_shortcode' );
}

if ( class_exists( 'GinevraCore_Shortcode' ) ) {
	class GinevraCore_Video_Holder_Shortcode extends GinevraCore_Shortcode {
		
		public function map_shortcode() {
			$this->set_shortcode_path( GINEVRA_CORE_SHORTCODES_URL_PATH . '/video-holder' );
			$this->set_base( 'ginevra_core_video_holder' );
			$this->set_name( esc_html__( 'Video Holder', 'ginevra-core' ) );
			$this->set_description( esc_html__( 'Shortcode that adds video holder element', 'ginevra-core' ) );
			$this->set_category( esc_html__( 'Ginevra Core', 'ginevra-core' ) );
			$this->set_scripts(
				array(
					'jquery-appear' => array(
						'registered' => true,
					),
				)
			);
			$this->set_option( array(
				'field_type' => 'text',
				'name'       => 'custom_class',
				'title'      => esc_html__( 'Custom Class', 'ginevra-core' ),
			) );
			$this->set_option( array(
				'field_type' => 'text',
				'name'       => 'video_source',
				'title'      => esc_html__( 'Link to Self Hosted Video', 'ginevra-core' ),
			) );
			$this->set_option( array(
				'field_type'  => 'image',
				'name'        => 'video_poster',
				'title'       => esc_html__( 'Poster Image', 'ginevra-core' ),
				'description' => esc_html__( 'Fallback poster image for the video', 'ginevra-core' ),
			) );
			$this->set_option( array(
				'field_type'    => 'select',
				'name'          => 'enable_appear',
				'title'         => esc_html__( 'Enable Appear Animation', 'ginevra-core' ),
				'options'       => ginevra_core_get_select_type_options_pool( 'no_yes' ),
				'default_value' => 'no'
			) );
            $this->set_option(
                array(
                    'field_type'    => 'select',
                    'name'          => 'appear_from',
                    'title'         => esc_html__( 'Appear From', 'ginevra-core' ),
                    'options'       => array(
                        'appear-left'  => esc_html__( 'Left', 'ginevra-core' ),
                        'appear-fade'  => esc_html__( 'Fade', 'ginevra-core' ),
                    ),
                    'default_value' => 'appear-left',
                    'dependency'    => array(
                        'show' => array(
                            'enable_appear' => array(
                                'values' => 'yes',
                                'default_value' => '',
                            )
                        ),
                    ),
                )
            );
            $this->set_option(
                array(
                    'field_type'    => 'select',
                    'name'          => 'appear_delay',
                    'title'         => esc_html__( 'Appear Delay', 'ginevra-core' ),
                    'description'   => esc_html__( 'Set option if content is placed on top of the page', 'ginevra-core' ),
                    'dependency'    => array(
                        'show' => array(
                            'enable_appear' => array(
                                'values' => 'yes',
                                'default_value' => '',
                            )
                        ),
                    ),
                    'options'       => ginevra_core_get_select_type_options_pool( 'no_yes', false ),
                    'default_value' => 'no'
                )
            );
		}

        public static function call_shortcode( $params ) {
            $html = qode_framework_call_shortcode( 'ginevra_core_video_holder', $params );
            $html = str_replace( "\n", '', $html );

            return $html;
        }
		
		public function render( $options, $content = null ) {
			parent::render( $options );
			$atts = $this->get_atts();
			
			$atts['holder_classes']		= $this->get_holder_classes( $atts );

			return ginevra_core_get_template_part( 'shortcodes/video-holder', 'templates/video-holder', '', $atts );
		}
		
		private function get_holder_classes( $atts ) {
			$holder_classes = $this->init_holder_classes();
			
			$holder_classes[] = 'qodef-video-holder';
			$holder_classes[] = ! empty ( $atts['enable_appear'] ) && $atts['enable_appear'] == 'yes' ? 'qodef--has-appear' : '';
            $holder_classes[] = ! empty ( $atts['appear_from'] ) ? 'qodef--' . $atts['appear_from'] : '';
            $holder_classes[] = ! empty ( $atts['appear_delay'] ) && $atts['appear_delay'] == 'yes' ? 'qodef--appear-delay' : '';
			
			return implode( ' ', $holder_classes );
		}
	}
}
