<?php

if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
	/**
	 * Extended class to integrate testimonial slider with
	 * visual composer
	 */
    class WPBakeryShortCode_themesflat_iconbox_slider extends WPBakeryShortCodesContainer {
    }
}
/**
 * Register shortcode into Visual Composer
 */
add_action( 'vc_before_init', 'themesflat_iconbox_shortcode_params' );

/**
 * Register parameters for iconbox shortcode
 * 
 * @return  void
 */
function themesflat_iconbox_shortcode_params() {
	$icons_params = themesflat_map_icons('icon','Icon for box');
	$icons_params2 = themesflat_map_icons('readmore','Icon for readmore');
	$readmore = array (
		array(
				'type' => 'textfield',
				'heading' => esc_html__( 'Read More Link', 'fo' ),
				'icon'        => themesflat_icon,
				'param_name' => 'link',
				'description' => esc_html__( 'Enter custom url for read more button', 'fo' )
				),

			array(
				'type' => 'textfield',
				'icon'        => 'themesflat-shortcode',
				'heading' => esc_html__( 'Read More Text', 'fo' ),
				'param_name' => 'text',
				'description' => esc_html__( 'Enter custom text for read more button', 'fo' ),
				'value' => esc_html__( 'Read More', 'fo' )
				),	
		);
	
		vc_map( array(
		'name'                    => esc_html__( 'Themesflat: Iconbox Slider', 'fo' ),
		'base'                    => 'themesflat_iconbox_slider',
		'as_parent'               => array( 'only' => 'themesflat_iconbox' ), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
		'icon'        => themesflat_icon,
		'content_element'         => true,
		'show_settings_on_create' => false,
		'category'                => esc_html__( 'fo', 'fo' ),
		'params'                  => array(			
			array(
				'type' => 'textfield',
				'heading' => esc_html__( 'Margin', 'fo' ),
				'param_name' => 'margin',
				'value' => '30',
				'description' => esc_html__( 'Margin item for slide', 'fo' )
			),

			array(
				'type' => 'textfield',
				'heading' => esc_html__( 'Slides Per View', 'fo' ),
				'param_name' => 'slides_per_view',
				'value' => '2',
				'description' => esc_html__( 'The number of items you want to see on the screen.', 'fo' )
			),

		array(
				'type' => 'checkbox',
				'heading' => esc_html__( 'Autoplay', 'fo' ),
				'param_name' => 'autoplay',
				'value' => array( esc_html__( 'Yes, Please', 'fo' ) => 1 )
			),
			array(
				'type' => 'checkbox',
				'heading' => esc_html__( 'Show Pagination Control', 'fo' ),
				'param_name' => 'show_control',
				'value' => array( esc_html__( 'Yes, Please', 'fo' ) => 1 )
			),
			array(
				'type' => 'checkbox',
				'heading' => esc_html__( 'Show prev/next buttons', 'fo' ),
				'param_name' => 'show_direction',
				'value' => array( esc_html__( 'Yes, Please', 'fo' ) => 1 )
			),			
			array(
				'type' => 'textfield',
				'heading' => esc_html__( 'Extra Class Name', 'fo' ),
				'param_name' => 'class',
				'description' => esc_html__( 'Your Custom Class', 'fo' )
			),
		
		),
		'js_view' => 'VcColumnView'
	) );


	vc_map( array(
		'base'        => 'themesflat_iconbox',
		'name'        => esc_html__( 'Themesflat: Icon Box', 'fo' ),
		'icon'        => themesflat_icon,
		'category'    => esc_html__( 'fo', 'fo' ),
		'params'      => array_merge($icons_params,$icons_params2,$readmore,array(
			// Title
			array(
				'type'             => 'textfield',
				'heading'          => esc_html__( 'Title', 'fo' ),
				'param_name'       => 'title',
				'edit_field_class' => 'vc_col-md-6 vc_column'
				),

			// Sub Title
			array(
				'type'             => 'textfield',
				'heading'          => esc_html__( 'Sub Title', 'fo' ),
				'param_name'       => 'subd',
				'edit_field_class' => 'vc_col-md-6 vc_column'
				),

			array(
				'type'       => 'dropdown',
				'heading'    => esc_html__( 'Title Element Tag', 'fo' ),
				'param_name' => 'tag',
				'value'      => array(
					'h5' => 'h5',
					'h3' => 'h3',
					'h2' => 'h2',					
					'h4' => 'h4',
					'h6' => 'h6'
					)
				),

			array(
				'type'       => 'textarea_html',
				'heading'    => esc_html__( 'Content', 'fo' ),
				'param_name' => 'content'
				),

			array(
				'type'       => 'attach_image',
				'heading'    => esc_html__( 'Image', 'fo' ),
				'param_name' => 'image',
				'description' => esc_html__( 'Select image to replace the icon', 'fo' )
				),

			array(
				'type' => 'checkbox',
				'heading' => esc_html__( 'Use Image as Icon', 'fo' ),
				'param_name' => 'as_icon',
				'description' => esc_html__( 'Replace Icon font with image', 'fo' ),
				'value' => array( esc_html__( 'Yes, Please', 'fo' ) => 'yes' )
			),


			array(
				'type'       => 'dropdown',
				'heading'    => esc_html__( 'Icon Position', 'fo' ),
				'param_name' => 'icon_position',
				'value' => array(
					esc_html__( 'Top', 'fo' ) => 'top',
					esc_html__( 'Left', 'fo' ) => 'left',
					esc_html__( 'Left Inline', 'fo' ) => 'inline-left',
					esc_html__( 'Right Inline', 'fo' ) => 'inline-right',
					esc_html__( 'Right', 'fo' ) => 'right'
					)
				),

			array(
				'type'       => 'dropdown',
				'heading'    => esc_html__( 'Icon Style', 'fo' ),
				'param_name' => 'icon_style',
				'value' => array(
					esc_html__( 'Default', 'fo' )         => 'transparent',
					esc_html__( 'Circle', 'fo' )          => 'circle',
					esc_html__( 'Circle Outline', 'fo' )  => 'circle-outlined',
					esc_html__( 'Square', 'fo' )          => 'square',
					esc_html__( 'Square Outline', 'fo' )  => 'square-outlined'
					)
				),

			array(
				'type'       => 'textfield',
				'heading'    => esc_html__( 'Extra Class', 'fo' ),
				'param_name' => 'class'
				),

			array(
				'type' => 'colorpicker',
				'heading'    => esc_html__( 'Icon Background Color', 'fo' ),
				'param_name' => 'icon-background',
				'group' => esc_html__( 'Design Options', 'fo' )
				),
			array(
				'type' => 'colorpicker',
				'heading'    => esc_html__( 'Icon Color', 'fo' ),
				'param_name' => 'icon-color',
				'group' => esc_html__( 'Design Options', 'fo' )
				),
			array(
				'type' => 'textfield',
				'heading'    => esc_html__( 'Icon Font Size', 'fo' ),
				'param_name' => 'icon-font-size',
				'group' => esc_html__( 'Design Options', 'fo' )
				),
		
			array(
				'type' => 'textfield',
				'heading'    => esc_html__( 'Icon Size', 'fo' ),
				'param_name' => 'icon-size',
				'group' => esc_html__( 'Design Options', 'fo' ),
				'description' => esc_html__('Icon Size In px','fo')
				),

			array(
				'type' => 'textfield',
				'heading'    => esc_html__( 'Title Font Size', 'fo' ),
				'param_name' => 'title-font-size',
				'group' => esc_html__( 'Design Options', 'fo' )
				),

			array(
				'type' => 'textfield',
				'heading'    => esc_html__( 'Title Font Weight', 'fo' ),
				'param_name' => 'title-font-weight',
				'group' => esc_html__( 'Design Options', 'fo' )
				),

			array(
				'type' => 'css_editor',
				'param_name' => 'css',
				'group' => esc_html__( 'Design Options', 'fo' )
				),
			array(
				'type'       => 'textfield',
				'param_name' => 'default_id',
				'group' => esc_html__( 'Design Options', 'fo' ),
				'value' => 'themesflat_'.current_time('timestamp'),
				'std' => 'themesflat_'.current_time('timestamp')
				)
			))
		) );

}

function themesflat_iconbox_css($atts) {
	$style[] = $atts['css'];
	if (!empty($atts['css'])) {
		if (function_exists('vc_shortcode_custom_css_class')) {
			$vcclass = vc_shortcode_custom_css_class( $atts['css'], '' );
		}
	}
	else {
		$vcclass = $atts['default_id']; 
	}
	$style[] = sprintf('
		.%1$s.themesflat_iconbox .iconbox-icon {
			width: %1$spx;
			height: %1$spx;
			background-color: %2$s;
		}',$vcclass,$atts['icon-size'],$atts['icon-background']);
	$style['iconbox-icon'] = sprintf('
			width: %1$spx;
			height: %1$spx;
			background-color: %2$s;
		',$atts['icon-size'],$atts['icon-background']);
	$style['iconbox_title'] = sprintf('
			font-size: %1$spx;
			font-weight: %2$s;
		',str_replace('px','',$atts['title-font-size']),$atts['title-font-weight']);
	$style['icon_span'] = sprintf('
			line-height: %1$spx;
			color: %2$s;
			font-size: %3$spx;
		',str_replace('px','',str_replace('px','',$atts['icon-size'])),$atts['icon-color'],str_replace('px','',$atts['icon-font-size']));
	return $style;
}

add_filter( 'themesflat/shortcode/themesflat_iconbox_class', 'themesflat_iconbox_shortcode_class', 10, 3 );

function themesflat_iconbox_shortcode_class( $classes, $atts, $tag = '' ) {
	$classes[] = $atts['icon_position'];
	$classes[] = $atts['icon_style'];	
	if (!empty($atts['css'])) {
		if (function_exists('vc_shortcode_custom_css_class')) {
			$vcclass = vc_shortcode_custom_css_class( $atts['css'], ' ' );
		}
	}
	else {
		$date = date_create(); 
		$vcclass = $atts['default_id']; 
	}
	$classes[] =$vcclass;
	return $classes;
}

add_shortcode( 'themesflat_iconbox_slider', 'themesflat_shortcode_iconbox_slider' );
add_shortcode( 'themesflat_iconbox', 'themesflat_shortcode_iconbox' );

/**
 * Iconbox shortcode handle
 * 
 * @param   array  $atts  Shortcode attributes
 * @return  void
 */
function themesflat_shortcode_iconbox( $atts, $content = null ) {
	$atts = vc_map_get_attributes( 'themesflat_iconbox', $atts );
	extract (apply_filters( 'themesflat/shortcode/themesflat_iconbox_atts',$atts));
	$icon_name = themesflat_shortcode_icon_name('icon_',$icon_type);
	$icon_name2 = themesflat_shortcode_icon_name('readmore_',$readmore_type);
	$icon_value = !empty($icon_name) ? $atts[$icon_name] : '';
	$icon_value2 = !empty($icon_name2) ? $atts[$icon_name2] : '';
	$style = themesflat_iconbox_css($atts);
	$class = apply_filters( 'themesflat/shortcode/themesflat_iconbox_class', array( 'themesflat_iconbox', $atts['class'] ), $atts );
	$icon_inner = false;
		$image_html = '';
	
	if ( ! empty( $image ) ) {
		$image_html = '';
		if (is_numeric( $image ) && $image_src = wp_get_attachment_image_src( $image, 'full' ) ) {
				$img_src = array_shift( $image_src );
				$alt  = (! empty($title)) ? $title : basename($image);
			if ( $as_icon != 'yes') {
				$image_html = sprintf( '<div class="iconbox-image"><img src="%s" alt="%s" /></div>', esc_url( $img_src ), esc_attr( $alt ) );
				if ($icon_type != 'none') {
					$icon_inner = sprintf( '<span class="%s"></span>', $icon_value);
				}
			}
			else {
				$icon_inner = sprintf( '<span><img src="%s" alt="%s" /></span>', esc_url( $img_src ), esc_attr( $alt ));
			}
		}
		
	}
	elseif ( $icon_type != 'none' ) {

		$icon_inner = sprintf( '<span class="%s" style="%s"></span>', $icon_value,esc_attr($style['icon_span']));
	}
	else {
		$icon_inner = false;
	}

	$box_icon = $icon_inner ? sprintf( '<div class="iconbox-icon" style="%2$s"><div class="icon">%1$s</div></div>', $icon_inner,esc_attr($style['iconbox-icon']) ) : '';
	$box_readmore = '';

	if ( ! empty( $link ) && ! empty( $text ) ) {
		$box_readmore = sprintf( '
				<p><a class="themesflat-button no-background" href="%1$s">%2$s<i class="readmore-icon %3$s"></i></a></p>', esc_url( $link ),  $text,$icon_value2);
	}

	$d = ! empty ($link) ? sprintf( '
				<a href="%s">%s</a>
			', esc_url( $link ), esc_html( $title ) ) : $title;


	if ( ! empty( $subd ) ) {
		$subd = sprintf( '
			<div class="sub-title">
				%s
			</div>', esc_html( $subd ) );
	}	

	if (! empty ($title)) {
		$title_html = sprintf('<%2$s class="title" style="%3$s">%1$s</%2$s>', wp_kses_post($d),esc_attr($tag),esc_attr($style['iconbox_title']));
	}
	

	return sprintf( '<div class="%2$s" >
		%8$s
		%1$s
		<div class="iconbox-content">
			%3$s
			%7$s
			%5$s
			%6$s
		</div>
	</div>', wp_kses_post($box_icon), esc_attr( implode( ' ', $class ) ),  wp_kses_post($title_html), $tag, wpb_js_remove_wpautop ($content,true), wp_kses_post($box_readmore), wp_kses_post($subd),wp_kses_post($image_html));
}


/**
 * This function will be use to handle imagebox slider
 * shortcode
 * 
 * @param   string  $atts     Shortcode attributes
 * @param   string  $content  Shortcode content
 * @return  string
 */
function themesflat_shortcode_iconbox_slider( $atts, $content = null ) {
	$atts = vc_map_get_attributes( 'themesflat_iconbox_slider', $atts );
	extract (apply_filters( 'themesflat/shortcode/themesflat_iconbox_slider_atts',$atts));
	$config = $atts;

	unset( $config['class'] );
	unset( $config['css'] );

	$class = apply_filters( 'themesflat/shortcode/themesflat_iconbox_slider_class', array( 'themesflat-icon-slider','themesflat_enable_slider', $atts['class'] ), $atts );

	// Enqueue shortcode assets
	wp_enqueue_script( 'themesflat-carousel' );
	
	return sprintf( '
		<div class="%s" data-margin="%s" data-slides_per_view="%s" data-autoplay="%s" data-show_control="%s" data-show_direction="%s">			
			%s				
		</div>
	', implode( ' ', $class ), esc_attr( $atts['margin'] ) , esc_attr( $atts['slides_per_view'] ), esc_attr( $atts['autoplay'] ), esc_attr( $atts['show_control'] ), esc_attr( $atts['show_direction'] ),  do_shortcode( $content ) );
}