<?php
/**
 * Shortcode Builder Class
 * Handles shortcode preview functionality
 *
 * @package Blog Designer Pack 
 * @since 1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

class BDP_Shortcode_Generator {

	function __construct() {
	}

	/**
	 * Render Fields HTML
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render( $args = array() ) {
		
		$upgrade_link = add_query_arg( array('page' => 'bdp-about-pricing'), admin_url('admin.php') );
		
		if ( !empty($args) ) {

			$temp_dependency = array();			
			
			// HTML start
			echo '<div id="bdp-shrt-accordion" class="bdp-shrt-accordion">';
			
			foreach ($args as $key => $value) {
				
				$section_title 	= isset( $value['title'] ) 		? $value['title'] 			: '';
				$section_params	= !empty( $value['params'] )	? (array) $value['params'] 	: '';

				if( ! $section_params ) {
					continue;
				}

				echo '<div class="bdp-shrt-acc-header bdp-shrt-acc-header-'.$key.'">'.$section_title.'</div>';
				echo '<div class="bdp-shrt-acc-cnt bdp-shrt-acc-cnt-'.$key.'">';
				if($key == 'premium' ){ echo '<div class="bdp-shrt-pro-wrap"><a class="button button-primary button-large bdp-shrt-pro-button" href="'.esc_url($upgrade_link).'">Upgrade to Pro</a></div>'; }

				foreach ($value['params'] as $param_key => $param_val) {

					// If field name is not there then return
					if( empty($param_val['name']) ) {
						continue;
					}

					$param_val['allow_empty'] 	= !empty( $param_val['allow_empty'] )	? 1		: 0;
					$param_val['heading'] 		= !empty( $param_val['heading'] )		? $param_val['heading']			: '';
					$param_val['name']  		= !empty( $param_val['name'] )			? $param_val['name']			: '';
					$param_val['value'] 		= isset( $param_val['value'] ) 			? $param_val['value']			: '';
					$param_val['desc']  		= !empty( $param_val['desc'] ) 			? $param_val['desc']			: '';
					$param_val['refresh_time']  = !empty( $param_val['refresh_time'] ) 	? $param_val['refresh_time']	: '';
					$param_val['id']    		= !empty( $param_val['id'] )			? $param_val['id']				: 'bdp-'.$param_val['name'];
					$param_val['class'] 		= !empty( $param_val['class'] ) 		? 'bdp-'.$param_val['name'].' '.$param_val['class'] : 'bdp-'.$param_val['name'];

					// Dependency
					if( !empty($param_val['dependency']) && $param_val['dependency']['element'] ) {				    	

						if( isset($param_val['dependency']['value_not_equal_to']) ) {
							$temp_dependency[ $param_val['dependency']['element'] ]['hide'][ $param_val['name'] ] 	= (array)$param_val['dependency']['value_not_equal_to'];
						} else {
							$temp_dependency[ $param_val['dependency']['element'] ]['show'][ $param_val['name'] ] 	= (array)$param_val['dependency']['value'];
						}
					}

					echo '<div class="bdp-customizer-row" data-type="'.$param_val['type'].'">';
						$this->render_field_label( $param_val );

						if( !empty( $param_val['type'] ) && (method_exists( $this, 'render_field_'.$param_val['type'] )) ) {
							call_user_func( array($this, 'render_field_'.$param_val['type']), $param_val );
						} else {
							call_user_func( array($this, 'render_field_text'), $param_val );
						}

						$this->render_field_desc( $param_val );
					echo '</div><!-- end .bdp-customizer-row -->';
				}
				echo '</div><!-- end .bdp-shrt-acc-cnt -->';
			}
			echo '</div><!-- end .bdp-shrt-accordion -->';

			// Dependency Value
			if( $temp_dependency ) {
				echo '<div class="bdp-cust-conf bdp-cust-dependency" data-dependency="'.htmlspecialchars( json_encode( $temp_dependency ) ).'"></div>';
			}
		} else {
			echo '<p>Sorry, No Shortcode Parameter Found.</p>';			
		}
	}

	/**
	 * Render Field Label
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_label( $args ) {
?>

		<?php if( $args['heading'] ) { ?>
		<label class="bdp-shrt-lbl" for="<?php echo esc_attr( $args['id'] ); ?>"><?php echo esc_html( $args['heading'] ); ?></label>
		<?php } ?>

<?php }

	/**
	 * Render Field Description
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_desc( $args ) {
?>

		<?php if( $args['desc'] ) { ?>
		<span class="description"><?php echo wp_kses_post( $args['desc'] ); ?></span>
		<?php } ?>

<?php }

	/**
	 * Render Text Field
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_text( $args ) {
?>

		<input type="text" id="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" value="<?php echo esc_attr( $args['value'] ); ?>" data-default="<?php echo esc_attr( $args['value'] ); ?>" <?php if( $args['allow_empty'] ) { echo 'data-empty="'.esc_attr( $args['allow_empty'] ).'"'; } ?> <?php if( $args['refresh_time'] ) { echo 'data-timeout="'.esc_attr( $args['refresh_time'] ).'"'; } ?> />

<?php }

	/**
	 * Render Number Field
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_number( $args ) {

		$min			= !empty($args['min'])	? $args['min'] 	: 0;
		$max			= !empty($args['max'])	? $args['max'] 	: '';
		$step			= !empty($args['step'])	? $args['step'] : '';
?>		
		<input type="number" id="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" value="<?php echo esc_attr( $args['value'] ); ?>" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min ); ?>" max="<?php echo esc_attr( $max ); ?>" data-default="<?php echo esc_attr( $args['value'] ); ?>" <?php if( $args['refresh_time'] ) { echo 'data-timeout="'.esc_attr( $args['refresh_time'] ).'"'; } ?> />

<?php }

	/**
	 * Render Select Field
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_dropdown( $args ) {

		$default 		= !empty($args['default']) 	? (array)$args['default'] 	: array();
		$args['value'] 	= !empty($args['value']) 	? (array)$args['value'] 	: array();

		if( empty($default) ) {
			$default[] = key( $args['value'] );
		}
?>

		<select id="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" <?php echo (!empty( $args['multi'] )) ? 'multiple' : ''; ?> data-default="<?php echo esc_attr( implode(',', $default) ); ?>" <?php if( $args['refresh_time'] ) { echo 'data-timeout="'.esc_attr( $args['refresh_time'] ).'"'; } ?>>
			<?php if( $args['value'] && is_array($args['value']) ) {
				foreach ($args['value'] as $select_key => $select_value) { ?>

					<option <?php echo (in_array($select_key, $default)) ? 'selected' : ''; ?> value="<?php echo esc_attr( $select_key ); ?>"><?php echo esc_html( $select_value ); ?></option>

			<?php } } ?>
		</select>

<?php }

	/**
	 * Render Radio Field
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_radio( $args ) {

		$default 		= !empty($args['default']) 	? $args['default'] 		: '';
		$args['value'] 	= !empty($args['value']) 	? (array)$args['value'] : '';

		if( $args['value'] && is_array($args['value']) ) {
			foreach ($args['value'] as $select_key => $select_value) { ?>
				<label class="bdp-shrt-field-lbl bdp-cust-radio-lbl" for="<?php echo esc_attr( $args['id'].'-'.$select_key ); ?>">
					<input type="radio" id="<?php echo esc_attr( $args['id'].'-'.$select_key ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" value="<?php echo esc_attr( $select_key ); ?>" <?php echo ($select_key == $default) ? 'checked' : '' ; ?> />
					<span><?php echo esc_html( $select_value ); ?></span>
				</label>
		<?php } }
	}

	/**
	 * Render Checkbox Field
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_checkbox( $args ) {

		$default 		= !empty($args['default']) 	? (array)$args['default'] 	: array();
		$args['value'] 	= !empty($args['value']) 	? (array)$args['value'] 	: '';

		if( $args['value'] && is_array($args['value']) ) {
			foreach ($args['value'] as $select_key => $select_value) { ?>
				<label class="bdp-shrt-field-lbl bdp-cust-checkbox-lbl" for="<?php echo esc_attr( $args['id'].'-'.$select_key ); ?>">
					<input type="checkbox" id="<?php echo esc_attr( $args['id'].'-'.$select_key ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" value="<?php echo esc_attr( $select_key ); ?>" <?php echo (in_array($select_key, $default)) ? 'checked' : ''; ?> />
					<span><?php echo esc_html( $select_value ); ?></span>
				</label>
		<?php } }
	}

	/**
	 * Render Textarea Field
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_textarea( $args ) {
?>

		<textarea id="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" <?php if( $args['refresh_time'] ) { echo 'data-timeout="'.esc_attr( $args['refresh_time'] ).'"'; } ?>><?php echo esc_textarea( $args['value'] ); ?></textarea>

<?php
	}

	/**
	 * Render Text Field
	 * 
	 * @package Blog Designer Pack 
	 * @since 1.0
	 */
	function render_field_colorpicker( $args ) { ?>

		<input type="text" id="<?php echo esc_attr( $args['id'] ); ?>" class="bdp-cust-color-box <?php echo esc_attr( $args['class'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" value="<?php echo esc_attr( $args['value'] ); ?>" data-default="<?php echo esc_attr( $args['value'] ); ?>" />

<?php }
}