<?php if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


if ( ! empty( $param['menu_1']['item_type'] ) ) {
	$count_i = count( $param['menu_1']['item_type'] );
} else {
	$count_i = 0;
}


if ( $count_i > 0 ) {
	$position = isset( $param['menu'] ) ? '-' . $param['menu'] : ' -left';
	$align    = isset( $param['align'] ) ? ' --' . $param['align'] : ' --center';
	$shadow   = ! empty( $param['shadow'] ) ? ' -shadow' : '';
	$list     = ! empty( $param['connect'] ) ? ' -list-show' : '';

	$menu_add_classes = 'side-menu ' . $position . $align . $shadow . $list;

	$menu = '<div class="' . esc_attr( $menu_add_classes ) . '" id="side-menu-' . absint( $id ) . '">';

	$connect_visible = ( ! empty( $param['connect'] ) && ! empty( $param['hold_open'] ) ) ? ' -visible' : '';

	$menu .= '<ul class="sm-list' . esc_attr( $connect_visible ) . '">';

	for ( $i = 0; $i < $count_i; $i ++ ) {

		$open = ! empty( $param['menu_1']['hold_open'][ $i ] ) ? ' -visible' : '';
		$menu .= '<li class="sm-item' . esc_attr( $open ) . '">';

		if ( ! empty( $param['menu_1']['item_custom'][ $i ] ) ) {
			$img_link = $param['menu_1']['item_custom_link'][ $i ];
			$icon     = '<span class="sm-icon"><img src="' . esc_url( $img_link ) . '"></span>';
		} else {
			$icon_class = $param['menu_1']['item_icon'][ $i ];
			$icon       = '<span class="sm-icon ' . esc_attr( $icon_class ) . '"></span>';
		}

		// Update to version 4.0
		$link_type = isset($param['menu_1']['item_type'][ $i ]) ? $param['menu_1']['item_type'][ $i ] : '';
		$class_id = '';
		if ($link_type === 'id') {
			$class_id = !empty($param['menu_1']['item_modal'][ $i ]) ? $param['menu_1']['item_modal'][ $i ] : '';
			$param['menu_1']['button_id'][ $i ] = $class_id;
			$param['menu_1']['item_type'][ $i ] = 'link';
		}
		elseif ($link_type === 'class'){
			$class_id = !empty($param['menu_1']['item_modal'][ $i ]) ? $param['menu_1']['item_modal'][ $i ] : '';
			$param['menu_1']['button_class'][ $i ] = $class_id;
			$param['menu_1']['item_type'][ $i ] = 'link';
		}

		$button_class = $param['menu_1']['button_class'][ $i ];
		$class_add    = ! empty( $button_class ) ? ' class="' . $button_class . '"' : "";
		$button_id    = $param['menu_1']['button_id'][ $i ];
		$id_add       = ! empty( $button_id ) ? ' id="' . $button_id . '"' : "";
		$link_param   = esc_html( $id_add . $class_add );

		$tooltip_text = $param['menu_1']['item_tooltip'][ $i ];


		$tooltip = ! empty( $tooltip_text ) ? '<span class="sm-label">' . esc_attr( $tooltip_text ) . '</span>' : '';

		$item_type = $param['menu_1']['item_type'][ $i ];

		switch ( $item_type ) {
			case 'link':
				$target = ! empty( $param['menu_1']['new_tab'][ $i ] ) ? '_blank' : '_self';
				$link   = ! empty( $param['menu_1']['item_link'][ $i ] ) ? $param['menu_1']['item_link'][ $i ] : '#';
				$menu   .= '<a href="' . esc_url( $link ) . '" target="' . esc_attr( $target ) . '" '
				           . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu   .= $icon . $tooltip;
				$menu   .= '</a>';
				break;
			case 'share':
				$share_service = mb_strtolower( $param['menu_1']['item_share'][ $i ] );
				$menu          .= '<a href="#" data-btn-type="' . esc_attr( $share_service ) . '" '
				                  . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu          .= $icon . $tooltip;
				$menu          .= '</a>';
				break;
			case 'print':
				$menu .= '<a href="#" data-btn-type="print" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'totop':
				$menu .= '<a href="#" data-btn-type="toTop" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'goback':
				$menu .= '<a href="#" data-btn-type="goback" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'goforward':
				$menu .= '<a href="#" data-btn-type="goforward" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'smoothscroll':
				$link = ! empty( $param['menu_1']['item_link'][ $i ] ) ? $param['menu_1']['item_link'][ $i ] : '#';
				$menu .= '<a href="' . esc_url( $link ) . '" data-btn-type="scroll" ' . wp_specialchars_decode( $link_param,
						'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'login':
				$link = wp_login_url( $param['menu_1']['item_link'][ $i ] );
				$menu .= '<a href="' . esc_url( $link ) . '"' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'logout':
				$link = wp_logout_url( $param['menu_1']['item_link'][ $i ] );
				$menu .= '<a href="' . esc_url( $link ) . '" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'register':
				$link = wp_registration_url();
				$menu .= '<a href="' . esc_url( $link ) . '" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'lostpassword':
				$link = wp_lostpassword_url( $param['menu_1']['item_link'][ $i ] );
				$menu .= '<a href="' . esc_url( $link ) . '" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'telephone':
				$link = 'tel:' . $param['menu_1']['item_link'][ $i ];
				$menu .= '<a href="' . esc_url( $link ) . '" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'email':
				$email   = $param['menu_1']['item_link'][ $i ];
				$link    = is_email( $email ) ? 'mailto:' . antispambot( $email ) : $email;
				$tooltip = $param['menu_1']['item_tooltip'][ $i ];
				$tooltip = is_email( $tooltip ) ? antispambot( $tooltip ) : $tooltip;
				$open    = ! empty( $param['menu_1']['hold_open'][ $i ] ) ? ' -visible' : '';
				$tooltip = ! empty( $tooltip ) ? '<span class="sb-label' . esc_attr( $open ) . '">'
				                                 . esc_attr( $tooltip ) . '</span>' : '';
				$menu    .= '<a href="' . esc_url( $link ) . '" ' . wp_specialchars_decode( $link_param, 'double' )
				            . '>';
				$menu    .= $icon . $tooltip;
				$menu    .= '</a>';
				break;
			case 'modal':
				$link = '#' . $param['menu_1']['item_modal'][ $i ];
				$menu .= '<a href="' . esc_url( $link ) . '" ' . wp_specialchars_decode( $link_param, 'double' ) . '>';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
			case 'shiftnav':
				$menu .= '<a href="' . esc_url( $link ) . '" ' . wp_specialchars_decode( $link_param, 'double' )
				         . ' data-shiftnav-target="shiftnav-main">';
				$menu .= $icon . $tooltip;
				$menu .= '</a>';
				break;
		}

		$menu .= '</li>';
	}
	$menu .= '</ul>';
	$menu .= '</div>';
}


