<?php

class AIOC_ImageCarouselItem extends ET_Builder_Module {

	public $slug       = 'aioc_image_carousel_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__( 'Image Carousel Item', 'all-in-one-carousel-for-divi' );
		$this->advanced_setting_title_text  = esc_html__( 'Image', 'all-in-one-carousel-for-divi' );
        $this->child_title_var              = 'title';
		$this->main_css_element 			= '.aioc_image_carousel %%order_class%%';
		if ( is_archive() ) {
            $this->main_css_element = '%%order_class%%';
        }
		add_filter( 'et_builder_processed_range_value', array( $this, 'aioc_builder_processed_range_value' ), 10, 3 );
	}

	public function get_settings_modal_toggles() {
		return array(
			'general'  => array(
				'toggles' => array(
					'main_content' => array(
						'title' => esc_html__( 'Content', 'all-in-one-carousel-for-divi' ),
					),
					'image' => array(
						'title' => esc_html__( 'Image', 'all-in-one-carousel-for-divi' ),
					),
				),
			),
			'advanced' => array(
				'toggles' => array(
					'text_settings' => array(
						'title' => esc_html__( 'Content', 'all-in-one-carousel-for-divi' ),
						'sub_toggles'   => array(
                            'title' => array(
                                'name' => 'Title',
                            ),
                            'subtitle' => array(
                                'name' => 'Subtitle',
                            ),
                        ),
                        'tabbed_subtoggles' => true,
					),
					'image' => array(
						'title' => esc_html__( 'Image', 'all-in-one-carousel-for-divi' ),
					),
					'content_bg_settings' => array(
						'title' => esc_html__( 'Content Background', 'all-in-one-carousel-for-divi' ),
					),
				),
			),
		);
	}

	public function get_advanced_fields_config() {
		return array(
			'fonts' => array(
				'title' => array(
					'label'          => esc_html__( 'Title', 'all-in-one-carousel-for-divi' ),
					'font_size'      => array(
						'default' => '16px',
						'range_settings'   => array(
							'min'  => '1',
							'max'  => '100',
							'step' => '1',
						),
						'validate_unit'    => true,
					),
					'line_height'    => array(
						'default_on_front' => '1.2em',
						'range_settings'   => array(
							'min'  => '0.1',
							'max'  => '10',
							'step' => '0.1',
						),
					),
					'letter_spacing' => array(
						'default_on_front' => '',
						'range_settings'   => array(
							'min'  => '0',
							'max'  => '10',
							'step' => '1',
						),
						'validate_unit'    => true,
					),
					'header_level'  => array(
						'default'   => 'h4',
					),
					'css'            => array(
						'main' => "{$this->main_css_element} .aioc_slide_title",
						'important' => 'all',
					),
					'hide_text_align' => true,
					'tab_slug' => 'advanced',
					'toggle_slug' => 'text_settings',
					'sub_toggle' => 'title',
				),
				'subtitle' => array(
					'label'          => esc_html__( 'Subtitle', 'all-in-one-carousel-for-divi' ),
					'font_size'      => array(
						'default' => '16px',
						'range_settings'   => array(
							'min'  => '1',
							'max'  => '100',
							'step' => '1',
						),
						'validate_unit'    => true,
					),
					'line_height'    => array(
						'default_on_front' => '1.2em',
						'range_settings'   => array(
							'min'  => '0.1',
							'max'  => '10',
							'step' => '0.1',
						),
					),
					'letter_spacing' => array(
						'default_on_front' => '0px',
						'range_settings'   => array(
							'min'  => '0',
							'max'  => '10',
							'step' => '1',
						),
						'validate_unit'    => true,
					),
					'header_level'  => array(
						'default'   => 'h5',
					),
					'css'            => array(
						'main' => "{$this->main_css_element} .aioc_slide_subtitle",
						'important' => 'all',
					),
					'hide_text_align' => true,
					'tab_slug' => 'advanced',
					'toggle_slug' => 'text_settings',
					'sub_toggle' => 'subtitle',
				),
			),
			'margin_padding' => array(
				'css' => array(
					'main'      => '%%order_class%%',
					'important' => 'all',
				),
			),
			'slide_content_margin_padding' => array(
                'image' => array(
                    'margin_padding' => array(
                        'css' => array(
                        	'margin' 	=> '%%order_class%% .aioc_slide_image_wrapper',
                            'padding'    => '%%order_class%% .aioc_slide_image_wrapper',
                            'important'  => 'all',
                        ),
                    ),
                ),
                'content' => array(
                    'margin_padding' => array(
                        'css' => array(
                        	'margin' 	=> '%%order_class%% .aioc_slide_inner_content_wrapper',
                            'padding'    => '%%order_class%% .aioc_slide_inner_content_wrapper',
                            'important'  => 'all',
                        ),
                    ),
                ),
            ),
			'max_width' => array(
				'default' => array(
					'css' => array(
						'main'             => '%%order_class%%',
						'module_alignment' => '%%order_class%%',
					),
				),
			),
			'height' => array(
				'css' => array(
					'main' => '%%order_class%%',
					'important' => 'all',
				),
			),
			'borders' => array(
				'image'  => array(
					'css'          => array(
						'main' => array(
							'border_radii'  => '.aioc_image_carousel %%order_class%% .aioc_slide_image',
							'border_styles' => '.aioc_image_carousel %%order_class%% .aioc_slide_image',
							'important'     => 'all',
						),
					),
					'label_prefix' => esc_html__( 'Image', 'all-in-one-carousel-for-divi' ),
					'tab_slug'     => 'advanced',
					'toggle_slug'  => 'image',
				),
				'default' => array(
					'css' => array(
						'main' => array(
							'border_styles' => '%%order_class%% .aioc_slide_wrapper',
							'border_radii'  => '%%order_class%% .aioc_slide_wrapper',
						),
						'important' => 'all',
					),
				),
			),
			'box_shadow' => array(
				'default' => array(
					'css' => array(
						'main' => '%%order_class%% .aioc_slide_wrapper',
					),
				),
			),
			'background' => array(
				'css' => array(
					'main' => '%%order_class%% .aioc_slide_wrapper',
				),
			),
			'text' => false,
			'filters' => false,
			'text_shadow' => false,
		);
	}

	public function get_fields() {
		return array_merge(
			array(
				'title' => array(
					'label'           		=> esc_html__( 'Title', 'all-in-one-carousel-for-divi' ),
					'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.', 'all-in-one-carousel-for-divi' ),
				),
				'subtitle' => array(
					'label'           		=> esc_html__( 'Subtitle', 'all-in-one-carousel-for-divi' ),
					'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 subtitle.', 'all-in-one-carousel-for-divi' ),
				),
				'image' => array(
					'label'                 => esc_html__( 'Image', 'all-in-one-carousel-for-divi' ),
					'type'                  => 'upload',
					'option_category'       => 'basic_option',
					'upload_button_text'    => esc_attr__( 'Upload an image', 'all-in-one-carousel-for-divi' ),
					'choose_text'           => esc_attr__( 'Choose an Image', 'all-in-one-carousel-for-divi' ),
					'update_text'           => esc_attr__( 'Set As Image', 'all-in-one-carousel-for-divi' ),
					'dynamic_content'  		=> 'image',
					'tab_slug'              => 'general',
					'toggle_slug'           => 'image',
					'description'           => esc_html__( 'Upload an image to display.', 'all-in-one-carousel-for-divi' ),
				),
				'image_alt' => array(
					'label'                 => esc_html__( 'Image Alt Text', 'all-in-one-carousel-for-divi' ),
					'type'                  => 'text',
					'option_category'       => 'basic_option',
					'tab_slug'              => 'general',
					'toggle_slug'           => 'image',
					'description'           => esc_html__( 'Here you can input the text to be used for the image as HTML ALT text.', 'all-in-one-carousel-for-divi' ),
				),
				'image_custom_padding' => array(
	                'label'                 => esc_html__( 'Image Padding', 'all-in-one-carousel-for-divi' ),
	                'type'                  => 'custom_padding',
	                'option_category'       => 'layout',
	                'mobile_options'        => true,
	                'hover'                 => false,
	                'default'          		=> '||||true|true',
					'default_on_front' 		=> '||||true|true',
	                'tab_slug'              => 'advanced',
	                'toggle_slug'           => 'image',
	                'description'           => esc_html__( 'Padding adds extra space to the inside of the element, increasing the distance between the edge of the element and its inner contents.', 'all-in-one-carousel-for-divi' ),
	            ),
				'content_custom_padding' => array(
	                'label'                 => esc_html__( 'Content Padding', 'all-in-one-carousel-for-divi' ),
	                'type'                  => 'custom_padding',
	                'option_category'       => 'layout',
	                'mobile_options'        => true,
	                'hover'                 => false,
	                'default'          		=> '20px|20px|20px|20px|true|true',
					'default_on_front' 		=> '20px|20px|20px|20px|true|true',
	                'tab_slug'              => 'advanced',
	                'toggle_slug'           => 'margin_padding',
	                'description'           => esc_html__( 'Padding adds extra space to the inside of the element, increasing the distance between the edge of the element and its inner contents.', 'all-in-one-carousel-for-divi' ),
	            ),
				'content_custom_margin' => array(
	                'label'                 => esc_html__( 'Content Margin', 'all-in-one-carousel-for-divi' ),
	                'type'                  => 'custom_padding',
	                'option_category'       => 'layout',
	                'mobile_options'        => true,
	                'hover'                 => false,
	                'default'          		=> '||||true|true',
					'default_on_front' 		=> '||||true|true',
	                'tab_slug'              => 'advanced',
	                'toggle_slug'           => 'margin_padding',
	                'description'           => esc_html__( 'Margin adds extra space to the outside of the element, increasing the distance between the edge of the element and its inner contents.', 'all-in-one-carousel-for-divi' ),
	            ),
	            'content_bg_color' => array(
	                'label'                 => esc_html__( 'Content Background', 'all-in-one-carousel-for-divi' ),
	                'type'                  => 'background-field',
	                'base_name'             => 'content_bg',
	                'context'               => 'content_bg_color',
	                'option_category'       => 'button',
	                'custom_color'          => true,
	                'background_fields'     => $this->generate_background_options( 'content_bg', 'button', 'advanced', 'content_bg_settings', 'content_bg_color' ),
	                'hover'                 => 'tabs',
	                'tab_slug'              => 'advanced',
	                'toggle_slug'           => 'content_bg_settings',
	                'description'           => esc_html__( 'Customize the background style of the content box by adjusting the background color, gradient, and image.', 'all-in-one-carousel-for-divi' ),
	            ),
	        ),
			$this->generate_background_options( 'content_bg', 'skip', 'advanced', 'content_bg_settings', 'content_bg_color' )
		);

	}

	public function render( $attrs, $content, $render_slug ) {
		global $slide_effect, $enable_lightbox, $aioc_disable_lazyload;

		$multi_view            		= et_pb_multi_view_options( $this );
		$image_alt 					= sprintf( esc_html__( '%s', 'all-in-one-carousel-for-divi' ), $this->props['image_alt'] );
		$title_level				= $this->props['title_level'];
		$subtitle_level				= $this->props['subtitle_level'];
		$slide_border_radius  		= '' !== $this->props['border_radii'] ? explode( '|', $this->props['border_radii'] ) : '';
		$processed_title_level 		= et_pb_process_header_level( $title_level, 'h4' );
		$processed_subtitle_level 	= et_pb_process_header_level( $subtitle_level, 'h5' );
		$processed_title_level 		= esc_html( $processed_title_level );
		$processed_subtitle_level 	= esc_html( $processed_subtitle_level );

		$image_class = 'aioc_slide_image';
		if ( 'on' === $aioc_disable_lazyload ) {
			$image_class .= ' no-lazyload skip-lazy';
		}

		$image = $multi_view->render_element( array(
			'tag'      => 'img',
			'attrs'    => array(
				'src'          => '{{image}}',
				'data-mfp-src' => '{{image}}',
				'class'        => esc_attr( $image_class ),
				'alt'          => $image_alt,
			),
			'required' => 'image',
		) );

		$title = $multi_view->render_element( array(
			'tag'      => $processed_title_level,
			'content'  => '{{title}}',
			'attrs'    => array(
				'class' => 'aioc_slide_title',
			),
			'required' => 'title',
		) );

		$subtitle = $multi_view->render_element( array(
			'tag'      => $processed_subtitle_level,
			'content'  => '{{subtitle}}',
			'attrs'    => array(
				'class' => 'aioc_slide_subtitle',
			),
			'required' => 'subtitle',
		) );

		if ( 'on' === $enable_lightbox ) {
			wp_enqueue_script( 'magnific-popup' );
			wp_enqueue_style( 'magnific-popup' );			
		}

		if ( '' !== $image ) {
			$image_wrapper = sprintf(
				'<div class="aioc_slide_image_wrapper">%1$s</div>',
				et_core_intentionally_unescaped( $image, 'html' )
			);
		}

		if ( '' !== $title || '' !== $subtitle ) {
			$content_wrapper = sprintf(
				'<div class="aioc_slide_content_wrapper">
					<div class="aioc_slide_inner_content_wrapper">%1$s%2$s</div>
				</div>',
				et_core_intentionally_unescaped( $title, 'html' ),
				et_core_intentionally_unescaped( $subtitle, 'html' )
			);
		} 

		if ( !isset( $content_wrapper ) ){
			self::set_style( $render_slug, array(
				'selector'    => '%%order_class%% .aioc_slide_image_wrapper',
				'declaration' => 'height: 100%;',
			) );
		}

		
		if ( isset( $image_wrapper ) || isset( $content_wrapper ) ) {
			$slide_wrapper = sprintf(
				'<div class="aioc_slide_wrapper">%1$s%2$s</div>',
				isset( $image_wrapper ) ? et_core_intentionally_unescaped( $image_wrapper, 'html' ) : '',
				isset( $content_wrapper ) ? et_core_intentionally_unescaped( $content_wrapper, 'html' ) : ''
			);
		} else {
			$slide_wrapper = '';
		}

		if ( 'coverflow' === $slide_effect && '' !== $slide_border_radius ){	
			self::set_style( $render_slug, array(
				'selector'    => '%%order_class%% .aioc_slide_inner_content_wrapper',
				'declaration' => sprintf( 'border-radius: %1$s %2$s %3$s %4$s;', 
					$slide_border_radius[1],
					$slide_border_radius[2],
					$slide_border_radius[3],
					$slide_border_radius[4]
				),
			) );
		}

		$args = array(
			'render_slug'	=> $render_slug,
			'props'			=> $this->props,
			'fields'		=> $this->fields_unprocessed,
			'module'		=> $this,
			'backgrounds' 	=> array(
				'content_bg' => array(
					'normal' => "{$this->main_css_element} .aioc_slide_inner_content_wrapper",
					'hover' => "{$this->main_css_element} .aioc_slide_inner_content_wrapper:hover",
	 			),
			),
		);

		AIOC_Helper::process_background( $args );
		$fields = array( 'slide_content_margin_padding' );
		AIOC_Helper::process_advanced_margin_padding_css( $this, $render_slug, $this->margin_padding, $fields );

		$background_layout_class_names = et_pb_background_layout_options()->get_background_layout_class( $this->props );
        $this->add_classname( array(
			$this->get_text_orientation_classname(),
			$background_layout_class_names[0],
			'aioc_slide_carousel_slide',
			'swiper-slide'
		) );

		$file = et_is_builder_plugin_active() ? 'style-dbp' : 'style';
		wp_enqueue_style( 'aioc-image-carousel-item-style', ALL_IN_ONE_CAROUSEL_FOR_DIVI_PATH . 'includes/modules/ImageCarouselItem/' . $file . '.min.css', array(), '1.0.0' );

		return $slide_wrapper;
	}

	/**
	 * Filter multi view value.
	 *
	 * @since 3.27.1
	 *
	 * @see ET_Builder_Module_Helper_MultiViewOptions::filter_value
	 *
	 * @param mixed                                     $raw_value Props raw value.
	 * @param array                                     $args {
	 *                                         Context data.
	 *
	 *     @type string $context      Context param: content, attrs, visibility, classes.
	 *     @type string $name         Module options props name.
	 *     @type string $mode         Current data mode: desktop, hover, tablet, phone.
	 *     @type string $attr_key     Attribute key for attrs context data. Example: src, class, etc.
	 *     @type string $attr_sub_key Attribute sub key that availabe when passing attrs value as array such as styes. Example: padding-top, margin-botton, etc.
	 * }
	 * @param ET_Builder_Module_Helper_MultiViewOptions $multi_view Multiview object instance.
	 *
	 * @return mixed
	 */

	public function multi_view_filter_value( $raw_value, $args, $multi_view ) {
		$name = isset( $args['name'] ) ? $args['name'] : '';
		$mode = isset( $args['mode'] ) ? $args['mode'] : '';

		if ( $raw_value && 'icon' === $name ) {
			$processed_value = html_entity_decode( et_pb_process_font_icon( $raw_value ) );
			if ( '%%1%%' === $raw_value ) {
				$processed_value = '"';
			}

			return $processed_value;
		}

		return $raw_value;
	}

/*
	public function process_padding( $padding ) {
		$padding_array  = explode( '|', $padding );
		$padding_output = array(
			'top'    => intval( $padding_array[0] ) ? intval( $padding_array[0] ) : 20,
			'right'  => intval( $padding_array[1] ) ? intval( $padding_array[1] ) : 10,
			'bottom' => intval( $padding_array[2] ) ? intval( $padding_array[2] ) : 20,
			'left'   => intval( $padding_array[3] ) ? intval( $padding_array[3] ) : 10,
		);
		return $padding_output;
	}
*/
	public function aioc_builder_processed_range_value( $result, $range, $range_string ) {
		if ( false !== strpos( $result, '0calc' ) ) {
			return $range;
		}
		return $result;
	}	

}

new AIOC_ImageCarouselItem;
