<?php

class DPAB_AdvancedBlurbs extends ET_Builder_Module {

	public $slug = 'et_pb_advanced_blurb';
	public $vb_support = 'on';
	protected $module_credits = array(
		'module_uri' => 'http://www.diviplugins.com/advanced-blurbs-plugin/',
		'author'     => 'DiviPlugins',
		'author_uri' => 'http://diviplugins.com',
	);

	public function init() {
		$this->name             = __( 'DP Advanced Blurb', 'dpab-dp-advanced-blurbs' );
		$this->main_css_element = '%%order_class%%';
		$this->fields_defaults  = array(
			'icon_font_size_last_edited' => array(
				'on|desktop',
				'add_default_setting'
			),
			'icon_font_size_tablet'      => array( '96px', 'add_default_setting' ),
			'icon_font_size_phone'       => array( '96px', 'add_default_setting' ),
		);
	}

	public function get_settings_modal_toggles() {
		return array(
			'general'  => array(
				'toggles' => array(
					'image'        => __( 'Image & Icon', 'dpab-dp-advanced-blurbs' ),
					'main_content' => __( 'Content', 'dpab-dp-advanced-blurbs' ),
					'link'         => __( 'Icon & Image Link', 'dpab-dp-advanced-blurbs' ),
				),
			),
			'advanced' => array(
				'toggles' => array(
					'icon_settings' => __( 'Image & Icon', 'dpab-dp-advanced-blurbs' ),
				),
			),
		);
	}

	public function get_advanced_fields_config() {
		return array(
			'fonts'   => array(
				'header' => array(
					'label'        => __( 'Blurb Title', 'dpab-dp-advanced-blurbs' ),
					'css'          => array(
						'main'      => "{$this->main_css_element} .dp_pb_ab_title, {$this->main_css_element} .dp_pb_ab_title a",
						'important' => 'all',
					),
					"line_height"  => array( "default" => "1em", ),
					"font_size"    => array( "default" => "18px", ),
					'header_level' => array(
						'default' => 'h4',
					),
				),
				'body'   => array(
					'label'       => __( 'Blurb Content', 'dpab-dp-advanced-blurbs' ),
					'css'         => array(
						'main'      => "{$this->main_css_element} .dp_pb_blurb_container",
						'important' => 'all',
					),
					"line_height" => array( "default" => "1em", ),
					"font_size"   => array( "default" => "14px", ),
				),
			),
			'filters' => false
		);
	}

	public function get_custom_css_fields_config() {
		return array(
			'blurb_outer'   => array(
				'label'    => __( 'Outer Container', 'dpab-dp-advanced-blurbs' ),
				'selector' => '.dp_pb_blurb_content',
			),
			'blurb_image'   => array(
				'label'    => __( 'Image & Icon Container', 'dpab-dp-advanced-blurbs' ),
				'selector' => '.dp_pb_main_blurb_image',
			),
			'blurb_icon'    => array(
				'label'    => __( 'Icon', 'dpab-dp-advanced-blurbs' ),
				'selector' => '.dp-pb-icon',
			),
			'blurb_img'     => array(
				'label'    => __( 'Image', 'dpab-dp-advanced-blurbs' ),
				'selector' => '.dp_pb_blurb_image',
			),
			'blurb_content' => array(
				'label'    => __( 'Content Container', 'dpab-dp-advanced-blurbs' ),
				'selector' => '.dp_pb_blurb_container',
			),
			'blurb_title'   => array(
				'label'    => __( 'Title', 'dpab-dp-advanced-blurbs' ),
				'selector' => '.dp_pb_ab_title',
			),
		);
	}

	public function get_fields() {
		$et_accent_color      = et_builder_accent_color();
		$image_icon_placement = array(
			'top' => __( 'Top', 'dpab-dp-advanced-blurbs' ),
		);
		if ( ! is_rtl() ) {
			$image_icon_placement['left'] = __( 'Left', 'dpab-dp-advanced-blurbs' );
		} else {
			$image_icon_placement['right'] = __( 'Right', 'dpab-dp-advanced-blurbs' );
		}
		$fields = array(
			'title'               => array(
				'label'           => __( 'Title', 'dpab-dp-advanced-blurbs' ),
				'type'            => 'text',
				'dynamic_content' => 'text',
				'default'         => __( 'Your Title Goes Here', 'dpab-dp-advanced-blurbs' ),
				'description'     => __( 'The title of your blurb will appear in bold below your blurb image.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'        => 'general',
				'toggle_slug'     => 'main_content',
			),
			'content'             => array(
				'label'           => __( 'Content', 'dpab-dp-advanced-blurbs' ),
				'type'            => 'tiny_mce',
				'dynamic_content' => 'text',
				'description'     => __( 'Input the main text content for your module here.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'        => 'general',
				'toggle_slug'     => 'main_content',
			),
			'url'                 => array(
				'label'           => __( 'Url', 'dpab-dp-advanced-blurbs' ),
				'type'            => 'text',
				'dynamic_content' => 'url',
				'description'     => __( 'If you would like to make your blurb a link, input your destination URL here.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'        => 'general',
				'toggle_slug'     => 'link',
			),
			'url_new_window'      => array(
				'label'           => __( 'Url Opens', 'dpab-dp-advanced-blurbs' ),
				'type'            => 'select',
				'option_category' => 'configuration',
				'options'         => array(
					'off' => __( 'In The Same Window', 'dpab-dp-advanced-blurbs' ),
					'on'  => __( 'In The New Tab', 'dpab-dp-advanced-blurbs' ),
				),
				'default'         => 'off',
				'description'     => __( 'Here you can choose whether or not your link opens in a new window', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'        => 'general',
				'toggle_slug'     => 'link',
			),
			'use_icon'            => array(
				'label'       => __( 'Use Icon', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'yes_no_button',
				'options'     => array(
					'off' => __( 'No', 'dpab-dp-advanced-blurbs' ),
					'on'  => __( 'Yes', 'dpab-dp-advanced-blurbs' ),
				),
				'default'     => 'on',
				'description' => __( 'Here you can choose whether use an icon or an image.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'    => 'general',
				'toggle_slug' => 'image',
			),
			'font_icon'           => array(
				'label'       => __( 'Icon', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'text',
				'default'     => '%%777%%',
				'show_if'     => array( 'use_icon' => 'on' ),
				'description' => __( 'Choose an icon to display with your blurb.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'    => 'general',
				'toggle_slug' => 'image',
			),
			'image'               => array(
				'label'              => __( 'Image', 'dpab-dp-advanced-blurbs' ),
				'type'               => 'upload',
				'upload_button_text' => __( 'Upload an image', 'dpab-dp-advanced-blurbs' ),
				'choose_text'        => __( 'Choose an Image', 'dpab-dp-advanced-blurbs' ),
				'update_text'        => __( 'Set As Image', 'dpab-dp-advanced-blurbs' ),
				'show_if'            => array( 'use_icon' => 'off' ),
				'default'            => "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTA4MCIgaGVpZ2h0PSI1NDAiIHZpZXdCb3g9IjAgMCAxMDgwIDU0MCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxnIGZpbGw9Im5vbmUiIGZpbGwtcnVsZT0iZXZlbm9kZCI+CiAgICAgICAgPHBhdGggZmlsbD0iI0VCRUJFQiIgZD0iTTAgMGgxMDgwdjU0MEgweiIvPgogICAgICAgIDxwYXRoIGQ9Ik00NDUuNjQ5IDU0MGgtOTguOTk1TDE0NC42NDkgMzM3Ljk5NSAwIDQ4Mi42NDR2LTk4Ljk5NWwxMTYuMzY1LTExNi4zNjVjMTUuNjItMTUuNjIgNDAuOTQ3LTE1LjYyIDU2LjU2OCAwTDQ0NS42NSA1NDB6IiBmaWxsLW9wYWNpdHk9Ii4xIiBmaWxsPSIjMDAwIiBmaWxsLXJ1bGU9Im5vbnplcm8iLz4KICAgICAgICA8Y2lyY2xlIGZpbGwtb3BhY2l0eT0iLjA1IiBmaWxsPSIjMDAwIiBjeD0iMzMxIiBjeT0iMTQ4IiByPSI3MCIvPgogICAgICAgIDxwYXRoIGQ9Ik0xMDgwIDM3OXYxMTMuMTM3TDcyOC4xNjIgMTQwLjMgMzI4LjQ2MiA1NDBIMjE1LjMyNEw2OTkuODc4IDU1LjQ0NmMxNS42Mi0xNS42MiA0MC45NDgtMTUuNjIgNTYuNTY4IDBMMTA4MCAzNzl6IiBmaWxsLW9wYWNpdHk9Ii4yIiBmaWxsPSIjMDAwIiBmaWxsLXJ1bGU9Im5vbnplcm8iLz4KICAgIDwvZz4KPC9zdmc+Cg==",
				'description'        => __( 'Upload an image to display at the top of your blurb.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'           => 'general',
				'toggle_slug'        => 'image',
			),
			'icon_placement'      => array(
				'label'       => __( 'Image/Icon Placement', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'select',
				'options'     => $image_icon_placement,
				'default'     => 'top',
				'description' => __( 'Here you can choose where to place the icon.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'icon_settings',
			),
			'icon_color'          => array(
				'label'       => __( 'Icon Color', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'color-alpha',
				'description' => __( 'Here you can define a custom color for your icon.', 'dpab-dp-advanced-blurbs' ),
				'default'     => $et_accent_color,
				'show_if'     => array( 'use_icon' => 'on' ),
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'icon_settings',
			),
			'use_circle'          => array(
				'label'       => __( 'Circle Icon', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'yes_no_button',
				'options'     => array(
					'off' => __( 'No', 'dpab-dp-advanced-blurbs' ),
					'on'  => __( 'Yes', 'dpab-dp-advanced-blurbs' ),
				),
				'default'     => 'off',
				'show_if'     => array( 'use_icon' => 'on' ),
				'description' => __( 'Here you can choose whether icon set above should display within a circle.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'icon_settings',
			),
			'circle_color'        => array(
				'label'       => __( 'Circle Color', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'color-alpha',
				'show_if'     => array( 'use_circle' => 'on', 'use_icon' => 'on' ),
				'description' => __( 'Here you can define a custom color for the icon circle.', 'dpab-dp-advanced-blurbs' ),
				'default'     => $et_accent_color,
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'icon_settings',
			),
			'use_circle_border'   => array(
				'label'       => __( 'Show Circle Border', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'yes_no_button',
				'options'     => array(
					'off' => __( 'No', 'dpab-dp-advanced-blurbs' ),
					'on'  => __( 'Yes', 'dpab-dp-advanced-blurbs' ),
				),
				'default'     => 'off',
				'show_if'     => array( 'use_circle' => 'on', 'use_icon' => 'on' ),
				'description' => __( 'Here you can choose whether if the icon circle border should display.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'icon_settings',
			),
			'circle_border_color' => array(
				'label'       => __( 'Circle Border Color', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'color-alpha',
				'default'     => $et_accent_color,
				'description' => __( 'Here you can define a custom color for the icon circle border.', 'dpab-dp-advanced-blurbs' ),
				'show_if'     => array(
					'use_circle'        => 'on',
					'use_circle_border' => 'on',
					'use_icon'          => 'on'
				),
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'icon_settings',
			),
			'use_icon_font_size'  => array(
				'label'       => __( 'Use Icon Font Size', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'yes_no_button',
				'options'     => array(
					'off' => __( 'No', 'dpab-dp-advanced-blurbs' ),
					'on'  => __( 'Yes', 'dpab-dp-advanced-blurbs' ),
				),
				'default'     => 'off',
				'show_if'     => array( 'use_icon' => 'on' ),
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'icon_settings',
			),
			'icon_font_size'      => array(
				'label'          => __( 'Icon Font Size', 'dpab-dp-advanced-blurbs' ),
				'type'           => 'range',
				'mobile_options' => true,
				'validate_unit'  => true,
				'default_unit'   => 'px',
				'allow_empty'    => false,
				'range_settings' => array(
					'min'  => '1',
					'max'  => '120',
					'step' => '1',
				),
				'default'        => '96px',
				'responsive'     => true,
				'show_if'        => array(
					'use_icon_font_size' => 'on',
					'use_icon'           => 'on'
				),
				'tab_slug'       => 'advanced',
				'toggle_slug'    => 'icon_settings',
			),
			'image_size'          => array(
				'label'          => __( 'Image Size', 'dpab-dp-advanced-blurbs' ),
				'type'           => 'range',
				'mobile_options' => true,
				'validate_unit'  => true,
				'default_unit'   => 'px',
				'allow_empty'    => false,
				'range_settings' => array(
					'min'  => '1',
					'max'  => '100',
					'step' => '1',
				),
				'default'        => '32px',
				'responsive'     => true,
				'show_if'        => array( 'use_icon' => 'off' ),
				'show_if_not'    => array( 'icon_placement' => 'top' ),
				'tab_slug'       => 'advanced',
				'toggle_slug'    => 'icon_settings',
			),
			'animation'           => array(
				'label'       => __( 'Image/Icon Animation', 'dpab-dp-advanced-blurbs' ),
				'type'        => 'select',
				'options'     => array(
					'off'     => __( 'No Animation', 'dpab-dp-advanced-blurbs' ),
					'bounce'  => __( 'Bounce', 'dpab-dp-advanced-blurbs' ),
					'flash'   => __( 'Flash', 'dpab-dp-advanced-blurbs' ),
					'pulse'   => __( 'Pulse', 'dpab-dp-advanced-blurbs' ),
					'shake'   => __( 'Shake', 'dpab-dp-advanced-blurbs' ),
					'fa-spin' => __( 'Spin/Rotate', 'dpab-dp-advanced-blurbs' ),
					'top'     => __( 'Top To Bottom', 'dpab-dp-advanced-blurbs' ),
					'left'    => __( 'Left To Right', 'dpab-dp-advanced-blurbs' ),
					'right'   => __( 'Right To Left', 'dpab-dp-advanced-blurbs' ),
					'bottom'  => __( 'Bottom To Top', 'dpab-dp-advanced-blurbs' ),
				),
				'default'     => 'off',
				'description' => __( 'This controls the animation style of loading icon.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'    => 'advanced',
				'toggle_slug' => 'animation',
			),
			'trigger'             => array(
				'label'           => __( 'Trigger', 'dpab-dp-advanced-blurbs' ),
				'type'            => 'select',
				'option_category' => 'configuration',
				'options'         => array(
					'onload'   => __( 'On Load', 'dpab-dp-advanced-blurbs' ),
					'hover'    => __( 'On Hover', 'dpab-dp-advanced-blurbs' ),
					'infinite' => __( 'Infinite', 'dpab-dp-advanced-blurbs' ),
				),
				'default'         => 'onload',
				'description'     => __( 'This controls the icon effect on page load, on hover, or continuous.', 'dpab-dp-advanced-blurbs' ),
				'tab_slug'        => 'advanced',
				'toggle_slug'     => 'animation',
			),
		);

		return $fields;
	}

	public function render( $attrs, $content = null, $render_slug ) {
		$props = $this->props;
		if ( 'on' === $props['use_icon_font_size'] ) {
			if ( '' !== $props['icon_font_size_tablet'] || '' !== $props['icon_font_size_phone'] || '' !== $props['icon_font_size'] ) {
				$props['icon_font_size_responsive_active'] = et_pb_get_responsive_status( $props['icon_font_size_last_edited'] );
				$props['icon_font_size_values']            = array(
					'desktop' => $props['icon_font_size'],
					'tablet'  => $props['icon_font_size_responsive_active'] ? $props['icon_font_size_tablet'] : '',
					'phone'   => $props['icon_font_size_responsive_active'] ? $props['icon_font_size_phone'] : '',
				);
				et_pb_generate_responsive_css( $props['icon_font_size_values'], '%%order_class%% .dp-pb-icon', 'font-size', $render_slug );
			}
		}
		if ( 'off' === $props['use_icon'] && $props['icon_placement'] !== 'top' ) {
			if ( '' !== $props['image_size_tablet'] || '' !== $props['image_size_phone'] || '' !== $props['image_size'] ) {
				$props['image_size_responsive_active'] = et_pb_get_responsive_status( $props['image_size_last_edited'] );
				$props['image_size_values']            = array(
					'desktop' => $props['image_size'],
					'tablet'  => $props['image_size_responsive_active'] ? $props['image_size_tablet'] : '',
					'phone'   => $props['image_size_responsive_active'] ? $props['image_size_phone'] : '',
				);
				et_pb_generate_responsive_css( $props['image_size_values'], '%%order_class%% .dp_pb_main_blurb_image', 'width', $render_slug );
			}
		}
		if ( $props['use_icon'] === 'on' ) {
			ET_Builder_Element::set_style( $render_slug, array(
				'selector'    => '%%order_class%% .dp-pb-icon',
				'declaration' => sprintf( 'color: %1$s;', esc_html( $props['icon_color'] ) )
			) );
			if ( 'on' === $props['use_circle'] ) {
				ET_Builder_Element::set_style( $render_slug, array(
					'selector'    => '%%order_class%% .dp-pb-icon',
					'declaration' => sprintf( 'background-color: %1$s;', esc_html( $props['circle_color'] ) )
				) );
				if ( 'on' === $props['use_circle_border'] ) {
					ET_Builder_Element::set_style( $render_slug, array(
						'selector'    => '%%order_class%% .dp-pb-icon',
						'declaration' => sprintf( 'border-color: %1$s;', esc_html( $props['circle_border_color'] ) )
					) );
				}
			}
		}
		if ( is_rtl() && 'left' === $this->get_text_orientation() ) {
			$text_orientation = 'right';
		}
		if ( is_rtl() && 'left' === $props['icon_placement'] ) {
			$props['icon_placement'] = 'right';
		}
		/*
		 * Start output
		 */
		ob_start();
		echo sprintf( '<div class="dp-pb-%1$s et_pb_blurb dp_pb_blurb_position_%2$s">', $props['trigger'], $props['icon_placement'] );
		echo '<div class="dp_pb_blurb_content">';
		echo '<div class="dp_pb_main_blurb_image">';
		if ( $props['url'] !== '' ) {
			echo sprintf( '<a href="%1$s"%2$s>', $props['url'], 'on' === $props['url_new_window'] ? ' target="_blank"' : '' );
		}
		if ( $props['use_icon'] === 'on' ) {
			$icons       = Dp_Advanced_Blurbs_Utils::fa_47();
			$icon_number = str_replace( '%', '', $props['font_icon'] );
			$fa_class    = $icons[ $icon_number ];
			echo sprintf( '<div class="db_icon_container"><i class="fa %1$s dp-pb-icon %2$s%3$s%4$s"></i></div>', $fa_class, esc_attr( "db_pb_animation_{$props['animation']}" ), ( 'on' === $props['use_circle'] ? ' dp-pb-icon-circle' : '' ), ( 'on' === $props['use_circle'] && 'on' === $props['use_circle_border'] ? ' et-pb-icon-circle-border' : '' ) );
		} else {
			if ( $props['image'] != "" ) {
				echo sprintf( '<img class="dp_pb_blurb_image %2$s" src="%1$s" />', $props['image'], esc_attr( "db_pb_animation_{$props['animation']}" ) );
			}
		}
		if ( $props['url'] !== '' ) {
			echo '</a>';
		}
		echo '</div>';
		echo '<div class="dp_pb_blurb_container">';
		if ( $props['url'] !== '' ) {
			echo sprintf( '<%4$s class="dp_pb_ab_title"><a href="%2$s"%3$s>%1$s</a></%4$s>', $props['title'], $props['url'], 'on' === $props['url_new_window'] ? ' target="_blank"' : '', $props['header_level'] );
		} else {
			echo sprintf( '<%2$s class="dp_pb_ab_title">%1$s</%2$s>', $props['title'], $props['header_level'] );
		}
		echo $props['content'];
		echo '</div>';
		echo '</div> <!-- .et_pb_blurb_content -->';
		echo '</div> <!-- .et_pb_blurb -->';

		$this->add_classname( array(
			$this->get_text_orientation_classname()
		) );

		$output = ob_get_clean();

		return $output;
	}

}

new DPAB_AdvancedBlurbs;
?>