<?php

defined( 'ABSPATH' ) or die;

use Tech360\Utils as Utils;

add_shortcode( 'contact-info', 'contact_info' );

function contact_info( $atts ) {

	$show_icons = is_array( $atts ) ? in_array( 'show_icons', $atts ) : false;
	$show_as_list = is_array( $atts ) ? in_array( 'show_as_list', $atts ) : false;

	$atts = shortcode_atts( array(
		'show' => ''
	), $atts );

	if( ! $atts[ 'show' ] ) {
		return;
	}

	$mods = explode( ' ', Utils::trim_all( $atts[ 'show' ] ) );
	$args = [
		'show_icons' => $show_icons,
		'show_as_list' => $show_as_list
	];

	ob_start();
	render_contact_info( $mods, $args );
	return ob_get_clean();

}

function render_contact_info( $mods, $args ) {

	if( $args[ 'show_as_list' ] ) {
		$wrapper_tag = 'ul';
		$elm_tag = 'li';
	} else {
		$wrapper_tag = 'div';
		$elm_tag = 'p';
	}

	$icons = [
		'phone_number' => '<i class="fas fa-fw fa-phone-alt"></i>',
		'toll_free_number' => '<i class="fas fa-fw fa-phone-volume"></i>',
		'email' => '<i class="far fa-fw fa-envelope-open"></i>',
		'office_address' => '<i class="fas fa-fw fa-map-marker"></i>',
	];

	foreach( $mods as $mod_index => $mod ) {
		if( ( 'social_links' != $mod ) && ( ! get_option( $mod ) ) ) {
			unset( $mods[ $mod_index ] );
		}
		if( ( 'social_links' == $mod ) && ( ! Utils::the_social_links( false ) ) ) {
			unset( $mods[ $mod_index ] );
		}
	}

	echo sprintf( '<%s class="%s">', $wrapper_tag, 'contact-info' );

	foreach( $mods as $mod ) {

		if( 'social_links' == $mod ) {
			echo sprintf( '<%s class="%s">', $elm_tag, 'social-links-wrapper' );
		} else {
			echo sprintf( '<%s>', $elm_tag );
		}

		if( $args[ 'show_icons' ] && array_key_exists( $mod, $icons ) ) {
			echo $icons[ $mod ];
		}
		switch( $mod ) {
			case 'phone_number':
			case 'toll_free_number':
				Utils::render_link( get_option( $mod ), 'tel' );
				break;
			case 'email':
				Utils::render_link( get_option( $mod ), 'mailto' );
				break;
			case 'social_links':
				Utils::the_social_links();
				break;
			default:
				echo nl2br( get_option( $mod ) );
		}
		echo sprintf( '</%s>', $elm_tag );
	}

	echo sprintf( '</%s>', $wrapper_tag );

}
