<?php

class AIOC_LogoCarouselItem extends ET_Builder_Module {

	public $slug       = 'aioc_logo_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__( 'Logo Carousel Item', 'all-in-one-carousel-for-divi' );
		$this->advanced_setting_title_text = esc_html__( 'Logo', 'all-in-one-carousel-for-divi' );
		$this->child_title_var             = 'title';
		$this->main_css_element            = '.aioc_logo_carousel %%order_class%%';
	}

	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' ),
					),
				),
			),
			'advanced' => array(
				'toggles' => array(
					'text_settings' => array(
						'title'             => esc_html__( 'Content', 'all-in-one-carousel-for-divi' ),
						'sub_toggles'       => array(
							'title'   => array(
								'name' => esc_html__( 'Title', 'all-in-one-carousel-for-divi' ),
							),
							'content' => array(
								'name' => esc_html__( 'Content', 'all-in-one-carousel-for-divi' ),
							),
						),
						'tabbed_subtoggles' => true,
					),
				),
			),
		);
	}

	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,
					),
					'text_align'     => array(
						'default' => 'center',
					),
					'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',
					),
					'tab_slug'       => 'advanced',
					'toggle_slug'    => 'text_settings',
					'sub_toggle'     => 'title',
				),
				'content' => array(
					'label'          => esc_html__( 'Content', 'all-in-one-carousel-for-divi' ),
					'font_size'      => array(
						'default'        => '14px',
						'range_settings' => array(
							'min'  => '1',
							'max'  => '100',
							'step' => '1',
						),
						'validate_unit'  => true,
					),
					'text_align'     => array(
						'default' => 'center',
					),
					'line_height'    => array(
						'default'        => '1.5em',
						'range_settings' => array(
							'min'  => '0.1',
							'max'  => '10',
							'step' => '0.1',
						),
					),
					'letter_spacing' => array(
						'default'        => '',
						'range_settings' => array(
							'min'  => '0',
							'max'  => '10',
							'step' => '1',
						),
						'validate_unit'  => true,
					),
					'css'            => array(
						'main'      => "{$this->main_css_element} .aioc_slide_content",
						'important' => 'all',
					),
					'tab_slug'       => 'advanced',
					'toggle_slug'    => 'text_settings',
					'sub_toggle'     => 'content',
				),
			),
			'borders'        => array(
				'default' => array(
					'css' => array(
						'main'      => array(
							'border_styles' => '%%order_class%%',
							'border_radii'  => '%%order_class%%',
						),
						'important' => 'all',
					),
				),
			),
			'margin_padding' => array(
				'css' => array(
					'main'      => "{$this->main_css_element}",
					'important' => 'all',
				),
			),
			'box_shadow'     => array(
				'default' => array(
					'css' => array(
						'main' => '%%order_class%%',
					),
				),
			),
			'background'     => array(
				'css' => array(
					'main' => '%%order_class%% .aioc-logo-carousel-item',
				),
			),
			'link_options'   => false,
			'text'           => false,
			'filters'        => false,
			'text_shadow'    => false,
		);
	}

	public function get_fields() {
		return array_merge(
			array(
				'image' => array(
					'label'              => esc_html__( 'Logo Image', 'all-in-one-carousel-for-divi' ),
					'type'               => 'upload',
					'option_category'    => 'basic_option',
					'upload_button_text' => esc_attr__( 'Upload a Logo', 'all-in-one-carousel-for-divi' ),
					'choose_text'        => esc_attr__( 'Choose an Logo', 'all-in-one-carousel-for-divi' ),
					'update_text'        => esc_attr__( 'Set As Logo', 'all-in-one-carousel-for-divi' ),
					'dynamic_content'    => 'image',
					'tab_slug'           => 'general',
					'toggle_slug'        => 'main_content',
					'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'     => 'main_content',
					'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' ),
				),
				'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' ),
				),
				'content' => array(
					'label'           => esc_html__( 'Content', 'all-in-one-carousel-for-divi' ),
					'type'            => 'tiny_mce',
					'option_category' => 'basic_option',
					'toggle_slug'     => 'main_content',
					'description'     => esc_html__( 'Here you can input the text to be used for the content.', 'all-in-one-carousel-for-divi' ),
				),
				'link_image' => array(
					'label'           => esc_html__( 'Link Image', 'all-in-one-carousel-for-divi' ),
					'type'            => 'yes_no_button',
					'option_category' => 'basic_option',
					'options'         => array(
						'off' => esc_html__( 'No', 'all-in-one-carousel-for-divi' ),
						'on'  => esc_html__( 'Yes', 'all-in-one-carousel-for-divi' ),
					),
					'default'         => 'off',
					'tab_slug'        => 'general',
					'toggle_slug'     => 'main_content',
					'description'     => esc_html__( 'Here you can choose whether or not display button.', 'all-in-one-carousel-for-divi' ),
				),
				'link_image_url' => array(
					'label'            => esc_html__( 'URL', 'all-in-one-carousel-for-divi' ),
					'type'             => 'text',
					'option_category'  => 'basic_option',
					'default'          => '',
					'default_on_front' => '',
					'dynamic_content'  => 'url',
					'show_if'          => array(
						'link_image' => 'on',
					),
					'tab_slug'         => 'general',
					'toggle_slug'      => 'main_content',
					'description'      => esc_html__( 'Here you can input the URL that you want to open when the user clicks on the button.', 'all-in-one-carousel-for-divi' ),
				),
				'link_new_window' => array(
					'label'            => esc_html__( 'Link Target', 'all-in-one-carousel-for-divi' ),
					'type'             => 'select',
					'option_category'  => 'configuration',
					'options'          => array(
						'off' => esc_html__( 'In The Same Window', 'all-in-one-carousel-for-divi' ),
						'on'  => esc_html__( 'In The New Tab', 'all-in-one-carousel-for-divi' ),
					),
					'default'          => 'off',
					'default_on_front' => 'off',
					'show_if'          => array(
						'link_image' => 'on',
					),
					'tab_slug'         => 'general',
					'toggle_slug'      => 'main_content',
					'description'      => esc_html__( 'Here you can choose whether or not the link opens in a new window.', 'all-in-one-carousel-for-divi' ),
				),
			)
		);
	}

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

		global $aioc_logo_carousel_args;

		$multi_view = et_pb_multi_view_options( $this );

		$image_classes = 'aioc-logo-image';
		if ( ! empty( $aioc_logo_carousel_args['disable_lazyload'] ) && 'on' === $aioc_logo_carousel_args['disable_lazyload'] ) {
			$image_classes .= ' no-lazyload skip-lazy';
		}

		$image_alt = $this->props['image_alt'];
		$image     = $multi_view->render_element( array(
			'tag'      => 'img',
			'attrs'    => array(
				'src'          => '{{image}}',
				'data-mfp-src' => '{{image}}',
				'class'        => $image_classes,
				'alt'          => $image_alt,
			),
			'required' => 'image',
		) );

		// Link images.
		if ( ! empty( $this->props['link_image'] ) && 'on' === $this->props['link_image'] && ! empty( $this->props['link_image_url'] ) ) {
			$image = sprintf(
				'<a href="%1$s"%3$s>%2$s</a>',
				esc_url( $this->props['link_image_url'] ),
				et_core_intentionally_unescaped( $image, 'html' ),
				( 'on' === $this->props['link_new_window'] ) ? ' target="_blank"' : ''
			);
		}

		$title = '';
		if ( 'on' === $aioc_logo_carousel_args['show_title'] ) {
			$title = $multi_view->render_element( array(
				'tag'     => 'h4',
				'content' => '{{title}}',
				'attrs'   => array(
					'class' => 'aioc_slide_title',
				),
			) );
		}

		$content = '';
		if ( 'on' === $aioc_logo_carousel_args['show_content'] ) {
			$content = $multi_view->render_element( array(
				'tag'     => 'div',
				'content' => '{{content}}',
				'attrs'   => array(
					'class' => 'aioc_slide_content',
				),
			) );
		}

		$content_wrap = '';
		if ( ! empty( $title ) || ! empty( $content ) ) {
			$content_wrap = sprintf(
				'<div class="aioc-logo-carousel-content">%1$s %2$s</div>',
				et_core_intentionally_unescaped( $title, 'html' ),
				et_core_intentionally_unescaped( $content, 'html' )
			);
		}

		// Check if image.
		$output = '';
		if ( ! empty( $image ) ) {
			$output = sprintf(
				'<div class="aioc-logo-carousel-item">
					%1$s %2$s
				</div>',
				et_core_intentionally_unescaped( $image, 'html' ),
				et_core_intentionally_unescaped( $content_wrap, 'html' )
			);
		}

		// Add module classes.
		$this->add_classname( array(
			$this->get_text_orientation_classname(),
			'swiper-slide',
		) );

		return $output;
	}

}

new AIOC_LogoCarouselItem();
